java 继承 多态(override->重写(=覆盖)、overload->重载、polymorphism -> 多态)

.

  
    
package edu.javase.buffer;

class demo1 {
public demo1() {
System.
out .println( " fun1 constend " );
}

public demo1( int i) {
System.
out .println( " fun1 int i constend: " + i);
}

// 重载(同类)
public void fun1() {
System.
out .println( " fun1 " );
}

public void fun1( int i, String s) {
System.
out .println( " fun1 int string " );
}

public void fun1(String s, int i) {
System.
out .println( " fun1 string int " );
}

public int fun1( int i) {
System.
out .println( " fun1 return int " );
return i;
}

public final void finalfun() {
System.
out .println( " fun1 finalfun " );
}
}

class demo2 extends demo1 {
public demo2() {
super();
// 载入父类构造器
System. out .println( " fun1 constend " );
}

public demo2( int i) {
super(i);
// 载入父类构造器
System. out .println( " fun2 int i constend: " + i);
}

// 重载(同类)
public void fun2() {
System.
out .println( " fun2 " );
}

public void fun2( int i, String s) {
System.
out .println( " fun2 int string " );
}

public void fun2(String s, int i) {
System.
out .println( " fun2 string int " );
}

public int fun2( int i) {
System.
out .println( " fun2 return int " );
return i;
}

// 重写 继承关系
// public void fun1() {
// System.out.println("2重写 fun1");
// }
}

class demo3 extends demo2 {
public demo3() {
super();
// 载入父类构造器
System. out .println( " fun1 constend " );
}

public demo3( int i) {
super(i);
// 载入父类构造器
System. out .println( " fun3 int i constend: " + i);
}

// 重载(同类)
public void fun3() {
System.
out .println( " fun3 " );
}

public void fun3( int i, String s) {
System.
out .println( " fun3 int string " );
}

public void fun3(String s, int i) {
System.
out .println( " fun3 string int " );
}

public int fun3( int i) {
System.
out .println( " fun3 return int " );
return i;
}

// 重写 继承关系
public void fun1() {
System.
out .println( " 3重写 fun1 " );
}

public void fun2() {
System.
out .println( " 3重写 fun2 " );
}

}

public class jicheng {
public static void main(String[] args) {
demo1 d1
= new demo1( 1 );
d1.fun1();
System.
out .println( " ~~~~~~~~~~~~~~ " );

demo2 d2
= new demo2( 2 );
d2.fun1();
// demo2继承了父类demo1的所有方法
d2.fun2();
System.
out .println( " ~~~~~~~~~~~~~~ " );

demo3 d3
= new demo3( 3 );
d3.fun1();
// demo2继承了父类demo1和 超级父类的所有方法
d3.fun2();
d3.fun3();
System.
out .println( " ~~~~~~~~~~~~~~ " );

demo1 d11
= new demo2( 2 );
d11.fun1();
// demo2中要重写demo1里面的方法,否则只拥有demo1方法
System. out .println( " ------------ " );

demo1 d111
= new demo3( 3 );
d111.fun1();
// demo3中要重写demo1里面的方法,否则只拥有demo1方法
System. out .println( " ------------ " );

demo2 d22
= new demo3( 2 );
d22.fun1(
1 );
d22.fun1();
d22.fun2(
2 );
d22.fun2();
d22.finalfun();
// demo3中要重写父类里面的方法,否则只拥有所有父类方法,自己的其他方法不能调用
System. out .println( " ------------ " );
}
/* output:
fun1 int i constend:1
fun1
~~~~~~~~~~~~~~
fun1 int i constend:2
fun2 int i constend:2
fun1
fun2
~~~~~~~~~~~~~~
fun1 int i constend:3
fun2 int i constend:3
fun3 int i constend:3
3重写 fun1
3重写 fun2
fun3
~~~~~~~~~~~~~~
fun1 int i constend:2
fun2 int i constend:2
fun1
------------
fun1 int i constend:3
fun2 int i constend:3
fun3 int i constend:3
3重写 fun1
------------
fun1 int i constend:2
fun2 int i constend:2
fun3 int i constend:2
fun1 return int
3重写 fun1
fun2 return int
3重写 fun2
fun1 finalfun
------------

*/// :~
}

.

#

#

#

你可能感兴趣的:(Polymorphism)