java类的继承是的一点问题,帮忙看看
class MyClass2 {
private int data;
public MyClass2(int data) {
this.data = data;
}
public void setData(int data) {
this.data = data;
}
public int getData() {
return data;
}
}
//定义MyClass2的子类SubClass2
class SubClass2 extends MyClass2 {
private int Mydata;
public SubClass2(int data, int Mydata) {
super(data); //调用父类MyClass2的构造方法
this.Mydata = Mydata;
}
public void setMydata(int data, int Mydata) {
super.setData(data);
this.Mydata = Mydata;
}
public String getMydata() {
return "父类的变量:" + super.getData() + " 子类的变量:" + Mydata;
}
}
public class class05
{
public static void main(String[] args)
{
SubClass2 s=new SubClass2(35,18);//这里一定要参数(35,18)吗?为什么?
System.out.println(s.getMydata());
s.setMydata(21,27);
System.out.println(s.getMydata());
}
}