-1
我有一个赋值,可以计算形状的面积和周长。返回子类的子类
超类:
public abstract class Shape implements Serializable {
private static final long serialVersionUID = -1231855623100981927L;
public abstract boolean draw();
public abstract String area();
public abstract String perimeter();
public abstract String characteristic();
}
Rectangle类:
public class Rectangle extends Shape {
private double x;
private double y;
public Rectangle() {}
public Rectangle(double x, double y) {
this.x = x;
this.y = y;
}
}
Square类:
public class Square extends Rectangle {
private double x;
public Square() {}
public Square(double side) {
super(side, side);
this.x = side;
}
public Square square(double side){
this.x = side;
return this;
}
}
主要类:
Shape rec = new Rectangle();
我想要的是当矩形的高度和宽度相等时,它将返回类Square
而不是Rectangle
类。这就是我想要的。
你试了一下自己? – nullpointer
你在说什么?我已经尝试过了。 –
@tima哈哈新手在这里 –