所以我的程序正在创建四边形,我被告知要为 findArea() 和 findPerimeter() 创建抽象方法,但我需要它们用于 toString() 和 boolean equals 方法吗?
abstract public class Quad {
private int s; //Base top
public Quad(){
s = 1;
}
Quad(int s){
this.s = s;
}
abstract public int findPerimeter();
abstract public int findArea();
abstract public String toString();
abstract public boolean equals(Object o);
public int getS() {
return s;
}
public void setS(int s){
this.s = s;
}
}
class Square extends Quad{
private static int numSquares = 0;
private int s, iD;
public Square(){
super();
s = super.getS();
numSquares ++;
iD = numSquares;
}
public Square(int s){
super(s);
s = super.getS();
numSquares ++;
iD = numSquares;
}
public int getNumSquares(){
return numSquares;
}
public int getID(){
return iD;
}
public void setID(int iD){
this.iD = iD;
}
@Override
public int findArea(){
return (getS()*getS());
}
@Override
public int findPerimeter() {
return (getS())*4;
}
public String toString(){
return "Side length is "+getS()+", Area is "+findArea()+" and Perimeter is "+findPerimeter();
}
public void decreaseNum(){
numSquares --;
}
public boolean equals(Object s){
if (s instanceof Square){
Square temp = (Square)s;
if(getID() == (temp.getID())){
return true;
}
}
return false;
}
}
这是代码,感谢您的阅读和帮助!