(一)关于承袭中成员变量值的成绩1.
class Base{
int x = 10;
void test(){
System.out.println("Base method,x = "+x);
}
}
public class Sub extends Base{
int x = 30;
public static void main(String[] args){
Sub b = new Sub();
System.out.println(b.x);
b.test();
Base b1=new Base();
b1.test();
System.out.println(b1.x);
Base b2=new Sub();
b2.test();
System.out.println(b2.x);
}
}
2.
class Base{
int x = 10;
void test(){
System.out.println("Base method,x = "+x);
}
}
public class Sub extends Base{
int x = 30;
void test(){
System.out.println("Base method,x = "+x);
}
public static void main(String[] args){
Sub b = new Sub();
System.out.println(b.one-t凡兔x);
b.test();
Base b1=new Base();
b1.test();
System.out.println(b1.x);
Base b2=new Sub();
b2.test();
System.out.println(b2.x);
}
}
总结:子类和父类有相反的成员变量时,子类调用自己定义的方法,则运用该子类的成员变量,若子类无重写父类方法,则子类调用该方法时,运用父类的成员变量
(二)关于承袭中静态方法隐藏,实例方法覆盖的成绩3.
class Base{
static void test(){
System.out.println("Base static method");
}
void show(){
System.out.println("Base no static method");
}
}
class Sub extends Base{
static void test(){
System.out.println("Sub static method");
}
void show(){
System.out.println("Sub no static method");
}
}
class App{
public static void main(String[] args){
Base b = new Sub();
b.test(); b.show();
}
}