有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java我需要使用可比较的界面比较两个形状,以确定哪个比另一个大或小

public abstract class Shape {
protected int height;
protected int width;


public Shape(int height, int width) {
this.height = height;
this.width = width;

}




public final void printArea() {
System.out.println("This " + getName() + " has a height of " +
height + ", a width of " + width + ", and an area of " + 
getArea() + ".");
}



public final void printPerimeter() {
System.out.println("This " + getName() + " has a height of " +
height + ", a width of " + width + ", and a perimeter of " + 
getPerimeter() + ".");
}



protected abstract String getName();
protected abstract double getArea();
protected abstract double getPerimeter();

}
}

这是我的开始代码,我还有另外三个类矩形、直角三角形和正方形,所有这些都有代码,但我首先关注我的shape类,我需要实现可比较的接口Compariable。因为get area方法在每个子类中都被重写。我可以在Shape类中编写一个compareTo()方法,当将任何类型的Shape或子类对象与任何其他对象进行比较时,该方法将正常工作。我需要实现compareTo()方法。那么公共int比较(形状s)正确吗?现在比较的代码是int k=getName()。比较(s.getName())。我需要重写Shape类中从Object继承的toString()方法,并让它返回一个字符串,其中包含当前对象的名称及其区域,格式如下:

名称:区域

我只是需要一些指导


共 (0) 个答案