public class A { private int a=1; public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
} public void a()
{
System.out.println("a="+a);
}
public void c()
{
System.out.println("a-1="+(a-1));
}
}
package jicheng; public class B extends A {
private int a=3;
private int b=2; public int get() {
return a;
}
public void setA(int a) {
this.a = a;
}
public int getB() {
return b;
}
public void setB(int b) {
this.b = b;
} @Override
public void a() {
// TODO 自动生成的方法存根
//super.a();
System.out.println("a+b="+(a+b));
}
public void b()
{
System.out.println("b="+b);
} }
package jicheng; public class E {
public static void main(String[] args) {
B i=new B();
i.a();
i.b();
//A a=new A(); //父类的对象
A a=i; //调用的是父类的方法,执行的却是子类的方法,因为子类方法覆盖了父类方法
a.a(); //调用的是父类的方法
a.c(); } }