父类Shape package com.imooc; public abstract class Shape { public abstract void perimeter(int length); public abstract void area(int length); }
子类 package com.imooc; public class Rectangle extends Shape { @Override public void perimeter(int length) { // TODO Auto-generated method stub System.out.println("矩形周长为"+length*4); } @Override public void area(int length) { // TODO Auto-generated method stub System.out.println("矩形面积为"+length*length); } }
子类 package com.imooc; public class Circle extends Shape { @Override public void perimeter(int length) { // TODO Auto-generated method stub System.out.println("圆形周长为"+2*3.14*length); } @Override public void area(int length) { // TODO Auto-generated method stub System.out.println("圆形面积为"+3.14*length*length); } }
package com.imooc; public class Initial { public static void main(String[] args) { Shape s1 = new Rectangle(); Shape s2 = new Circle(); int length = 10; s1.perimeter(length); s1.area(length); s2.perimeter(length); s2.area(length); } }
执行结果: 矩形周长为40 矩形面积为100 圆形周长为62.800000000000004 圆形面积为314.0
那是因为Java输出的时候自动转换成了double