首先,应该从类GeoObjects继承一个类矩形。其次,类方块应该从类矩形继承。
给出了抽象类GeoObjects。
abstract class GeoObjects{ public abstract double Perimeter(); public abstract double Surface(); public static void main (String [] argv){ double width = 4.0, height = 5.0, side= 3.0; GeoObject rectangle = new Rectangle (width, height); GeoObject square= new Square(side); System.out.println ("Perimeter = " + rectangle.Perimeter()); System.out.println ("Surface= " + rectangle.Surface()); System.out.println ("Perimeter= " + square.Perimeter()); System.out.println ("Surface= " + square.Surface()); }}class Rectangle extends GeoObjects{ double width, height, side; Rectangle (double width, double height){ this.width = width; this.height= height; } public double Perimeter (){ return 2*(width+ height); } public double Surface(){ return width* height; }}class Square extends Rectangle { double side; Square (double side){ this.side= side; } public double Perimeter (){ return 4*side; } public double Surface(){ return side*side; }}
我得到的编译器信息是Square构造函数的变量数量与Rectangle中的变量数量不同。
相关分类