重写 getValue() ;
/**
*
* <一句话功能简述>
* 方法重写
* @author kWX14579
* @version [版本号, 2009-5-6]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
public class A
{
private int x;
private int y;
/**
* @return 返回 x
*/
public int getX()
{
return x;
}
/**
* @param 对x进行赋值
*/
public void setX(int x)
{
this.x = x;
}
/**
* @return 返回 y
*/
public int getY()
{
return y;
}
/**
* @param 对y进行赋值
*/
public void setY(int y)
{
this.y = y;
}
//方法被重写在子类;
public void getValue()
{
System.out.println("aaa");
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
A a = new A();
B b = new B();
b.setC(1);
A a1 = (A)b; //父类引用(句柄)指向子类;
A a2 = new B(); //父类引用(句柄)指向子类;
//B b1 = (B)a; //父类不能转为子类;
a1.getValue();
a2.getValue();
}
}
public class B extends A
{
private int c ;
/**
* @return 返回 c
*/
public int getC()
{
return c;
}
/**
* @param 对c进行赋值
*/
public void setC(int c)
{
this.c = c;
}
public void getValue()
{
System.out.println("bbbb");
}
}