子类 接口 问题

来源:百度知道 编辑:UC知道 时间:2024/09/24 13:30:59
interface InterfaceRec {
int getArea ();
}

class Rectangle implements InterfaceRec {
int x,y;
Rectangle (int x, int y) {
this.x=x;
this.y=y;
}
public int getArea () {
return (x*y);
}
}

class Square extends Rectangle implements InterfaceRec {
public int getArea () {
return (x*x);
}
}

public class t {
public static void main (String args[]) {
Rectangle R1=new Rectangle(20,30);
System.out.println("R1 Area = "+R1.getArea);
Square S1=new Square(10,10);
System.out.println("S1 Area = "+S1.getArea);
}
}

继承的时候 有点问题啊 , 不知道什麼事?

首先接口怎么能和类放在一起呢?
你要把接口和类分开,写在两个不同的文件中

如果子类的构造方法中没有调用父类的构造方法,则系统调用父类无参构造方法。
如果子类的构造方法中既没有调用父类的构造方法,而父类又没有无参构造方法,则编译出错。

现在你应该知道怎么改了吧。