java基础
直接上代码:
class A {
public A fun1(){
System.out.println("A--fun1");
return this;
}
public A fun2(){
System.out.println("A--fun2");
return this;
}
}
class B extends A{
public A fun1(){
System.out.println("B--fun1");
return this;
}
public A fun2(){
System.out.println("B--fun2");
return this;
}
public A fun3(){
System.out.println("B--fun3");
return new C();
}
}
class C extends A{
public A fun1(){
System.out.println("C--fun1");
return new B();
}
public A fun4(){
System.out.println("C--fun4");
return new B();
}
public class Test2 {
public static void main(String[] args) {
//同类之间
new A().fun1().fun2();
//父子之间
new B().fun1().fun2();
//兄弟之间的
new C().fun1().fun1();
}
}
运行结果:
A--fun1
A--fun2
B--fun1
B--fun2
C--fun1
B--fun1
能看懂的自己玩哈