java基础2

时间:2021-06-12 21:51:56

//第一个程序 用super访问父类中被隐藏的成员变量和被重写的方法

package foxe;

class superClass{
int x;
superClass(){
x=4;
System.out.println("in superClass:x="+x);

}
void aMethod(){
System.out.println("in superClass.aMethod()");
}

}
class subClass extends superClass{
int x;
subClass(){
x=6;
System.out.println("in subClass:x="+x);

}
void aMethod(){
super.aMethod();
System.out.println("in subClass.aMethod()");
System.out.println("super.x="+super.x+",sub.x="+x);
}
}
public class inheritance {

public static void main(String[] args) {
subClass subC=new subClass();
subC.aMethod();

}

}

//独立代码片段 instanceof 使用

public class Cat extends Animal{}

public class Cat extends Animal{}

public class Cat extends Animal{}

public void doSomething(Animal e){

if(e instanceof Cat){

}else if(e instanceof Dog){}

}