java 繼承 多態(override->重寫(=覆蓋)、overload->重載、polymorphism -> 多態)

來源:互聯網
上載者:User

.

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
------------

*/// :~
}

.

#

#

#

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.