海大软件1102班 - 云代码空间
—— 你究竟输入什么了导致它崩溃?
class A{
public void fun1(){
System.out.println("A --> public void fun1(){}") ;
}
public void fun2(){
this.fun1() ;
}
};
class B extends A{
public void fun1(){
System.out.println("B --> public void fun1(){}") ;
}
public void fun3(){
System.out.println("B --> public void fun3(){}") ;
}
};
|
class A{
public void fun1(){
System.out.println("A1 --> public void fun1(){}") ;
}
public void fun2(){
this.fun1() ;
}
};
class B extends A{
public void fun1(){
System.out.println("B1 --> public void fun1(){}") ;
}
public void fun3(){
System.out.println("B2 --> public void fun3(){}") ;
}
};
public class Demo10{
public static void main(String args[]){
A a = new B() ;
a.fun1() ;
a.fun2() ;
}
};
|
class A{
public void fun1(){
System.out.println("A1 --> public void fun1(){}") ;
}
public void fun2(){
this.fun1() ;
}
};
class B extends A{
public void fun1(){
System.out.println("B1 --> public void fun1(){}") ;
}
public void fun3(){
System.out.println("B2 --> public void fun3(){}") ;
}
};
public class Demo10{
public static void main(String args[]){
A a = new B() ;
B b = (B)a ;
b.fun1() ;
b.fun2() ;
}
};
|
class A{
public void fun1(){
System.out.println("A1 --> public void fun1(){}") ;
}
public void fun2(){
this.fun1() ;
}
};
class B extends A{
public void fun1(){
System.out.println("B1 --> public void fun1(){}") ;
}
public void fun3(){
System.out.println("B2 --> public void fun3(){}") ;
}
};
public class Demo11{
public static void main(String args[]){
A a = new A() ;
B b = (B)a ;
b.fun2() ;
}
};
|
class A{
public void fun1(){
System.out.println("A1 --> public void fun1(){}") ;
}
public void fun2(){
this.fun1() ;
}
};
class B extends A{
public void fun1(){
System.out.println("B1 --> public void fun1(){}") ;
}
public void fun3(){
System.out.println("B2 --> public void fun3(){}") ;
}
};
class C extends A{
public void fun1(){
System.out.println("C1 --> public void fun1(){}") ;
}
public void fun3(){
System.out.println("C2 --> public void fun3(){}") ;
}
};
public class Demo12{
public static void main(String args[]){
fun(new B()) ;
fun(new C()) ;
}
// 定义一个方法,此方法可以接收A的子类对象
public static void fun(B b){
b.fun2() ;
}
public static void fun(C c){
c.fun2() ;
}
};
|
class A{
public void fun1(){
System.out.println("A1 --> public void fun1(){}") ;
}
public void fun2(){
this.fun1() ;
}
};
class B extends A{
public void fun1(){
System.out.println("B1 --> public void fun1(){}") ;
}
public void fun3(){
System.out.println("B2 --> public void fun3(){}") ;
}
};
class C extends A{
public void fun1(){
System.out.println("C1 --> public void fun1(){}") ;
}
public void fun3(){
System.out.println("C2 --> public void fun3(){}") ;
}
};
public class Demo13{
public static void main(String args[]){
fun(new B()) ;
fun(new C()) ;
}
// 不管有多少个子类,都用父类对象接收,因为可以自动发生向上转型关系
public static void fun(A a){
a.fun2() ;
}
};
|
class A{
public void fun1(){
System.out.println("A1 --> public void fun1(){}") ;
}
public void fun2(){
this.fun1() ;
}
};
class B extends A{
public void fun1(){
System.out.println("B1 --> public void fun1(){}") ;
}
public void fun3(){
System.out.println("B2 --> public void fun3(){}") ;
}
};
class C extends A{
public void fun1(){
System.out.println("C1 --> public void fun1(){}") ;
}
public void fun3(){
System.out.println("C2 --> public void fun3(){}") ;
}
};
public class Demo14{
public static void main(String args[]){
fun(new B()) ;
fun(new C()) ;
}
// 不管有多少个子类,都用父类对象接收,因为可以自动发生向上转型关系
public static void fun(A a){
a.fun2() ;
if(a instanceof B){
B b = (B)a ;
b.fun3() ;
}
if(a instanceof C){
C c = (C)a ;
c.fun3() ;
}
}
};
|