java 覆盖和隐藏

public class TestOverride{
public static void main(String args[]){
Sup s = new Sub();
Sub ss = new Sub();
s.doit();
s.something(s);
s.something(ss);
s.anything();
ss.something(s);
ss.something(ss);
ss.anything();
}
}

//1. 协变
//2. 继承
//3. 覆盖和隐藏

class Sup{
public void doit(){
System.out.println(“sup”);
}
public void something(Sup sup){
System.out.println(“1”);
}
public double anything(){
System.out.println(“double”);
return 0.0;
}
}

class Sub extends Sup{
public void doit(int i){
System.out.println(“sub”);
}
public void something(Sub sub){
System.out.println(“2”);
}
/*can not pass
public int anything(){
System.out.println(“int”);
return 0;
}
*/
/*简单覆盖
public void something(Sup sup){
System.out.println(“3”);
}
*/

}

你可能感兴趣的:(#,JSE)