解释一下这个程序输出结果的二三行

来源:百度知道 编辑:UC知道 时间:2024/06/05 03:28:14
class A{
int x=1,y=1;
A()
{
x=0;y=0;
}
A(int x,int y)
{
x=x;y=y;
}
void setX(int i)
{
x=i;
}
void setY(int i)
{
y=0;
}
void out()
{
System.out.println("x="+x+"\ty="+y);
}
}
class B extends A
{
int x=2;
float z;
B()
{
super();
}
B(int x,int y)
{
this.x=x;this.y=y;
}
void setY()
{
this.y=-1;
}
void out(){System.out.println("x="+x+"\ty="+y+"\tz="+z);}
}
public class FatherAndSon
{
public static void main(String[] args){
A a1=new A(3,4);
a1.setX(6);
a1.out();
B b1=new B(1,2);
b1.setX(0);
b1.setY();
b1.out();
A b2=new B(5,6);
b2.setX(12);
//b2.setY();
b2.out();
}
}
b1.setX(0);b2.setX(12);的作用是什么

setX()这个方法是B从A类那里继承而来,同时它也继承了A类的x和y。
b1.setX(0);b2.setX(12);是给从A那里继承来的x赋值。
不过在B类中显示x的值时用的是B自身的x,所以看不到b1.setX(0);b2.setX(12)的效果