interface Shape { double area(double givenValue); } class Square implements Shape { public double area(double sideLength) { return sideLength*sideLength; } } class Circle implements Shape { public double area(double r) { return Math.PI*r*r; } } public class Test02 { public static void main(String[] args) { //第一种 写法 Shape square = new Square(); Shape circle = new Circle(); System.out.println(square.area(2)); System.out.println(circle.area(3));
/*第二种写法 Square s = new Square(); Circle c = new Circle(); System.out.println(s.area(2)); System.out.println(c.area(3)); */ } }
用红色字标注的两种方法都可以输出相同的答案,为什么?? 这两种方法有什么不同? 菜鸟勿喷.... |