public class ThreeD extends TwoD {
private int z;
public ThreeD(int x, int y,int z) {
super(x, y);
this.z = z;
}
public void display() {
System.out.println("这个空间点的坐标是:"+x+","+y+","+z);
}public class TwoD {
private int x;
private int y;
public TwoD(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public void display() {
System.out.println("这个平面点的坐标是:" + x + "," + y);
}
} public static void main(String[] args) {
TwoD Tpoint = new TwoD(4, 8);
TwoD thpoint = new ThreeD(20,30,40);
show(Tpoint);
show(thpoint);
}
public static void show(TwoD c){
c.display();
}
不知道错在哪里!! 结果就是和我想象的不一样!!!
private int z;
public ThreeD(int x, int y,int z) {
super(x, y);
this.z = z;
}
public void display() {
System.out.println("这个空间点的坐标是:"+x+","+y+","+z);
}public class TwoD {
private int x;
private int y;
public TwoD(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public void display() {
System.out.println("这个平面点的坐标是:" + x + "," + y);
}
} public static void main(String[] args) {
TwoD Tpoint = new TwoD(4, 8);
TwoD thpoint = new ThreeD(20,30,40);
show(Tpoint);
show(thpoint);
}
public static void show(TwoD c){
c.display();
}
不知道错在哪里!! 结果就是和我想象的不一样!!!
多谢大神! 问题解决了! 弱弱的问句, 为什么子类不能继承父类的private的属性啊?
protected 父子访问 你父亲从你要钱你当然愿意了
public 共有属性 你钱太多 没地方用了 所以无所谓给谁
这位大神!就是为什么我在C#中,如果子类继承父类之后,子类可以直接调用父类的private的成员!
而在JAVA中不行 . 难道是语法问题?
而且那个说才C#中子类可以访问父类中的private成员的 不可以的 难道你那个程序可以?
{
private string privates = "";
public string Noprivate = "";
} class Class2:Class1
{
} static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
Class2 cl = new Class2();
cl.Noprivate = "出现不了父类的privates成员";
}