澶氭€佷腑鐨勯鐩垎鏋愰
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了澶氭€佷腑鐨勯鐩垎鏋愰相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/sys' title='sys'>sys
xtend int == class color 棰樼洰 oid str澶氭€侊紙Polymorphic锛夋杩帮細浜嬬墿瀛樺湪鐨勫绉嶅舰鎬?/p>
澶氭€佺殑鍓嶆彁锛?span style="background-color: #ff0000">a.瑕佹湁缁ф壙鍏崇郴
b.瑕佹湁鏂规硶閲嶅啓 c.瑕佹湁鐖剁被寮曠敤鎸囧悜瀛愮被瀵硅薄澶氭€佷腑鐨勬垚鍛樿闂壒鐐逛箣鎴愬憳鍙橀噺锛?span style="background-color: #ff0000">缂栬瘧鐪嬪乏杈癸紙鐖剁被锛夛紝杩愯鐪嬪乏杈癸紙鐖剁被锛?/span>
澶氭€佷腑鐨勬垚鍛樿闂壒鐐逛箣鎴愬憳鏂规硶锛?span style="background-color: #ff0000">缂栬瘧鐪嬪乏杈癸紙鐖剁被锛夛紝杩愯鐪嬪彸杈癸紙瀛愮被锛?鍔ㄦ€佹€?/span>
鍒嗘瀽澶氭€佷腑鐨勯鐩細
=======A绋嬪簭============
class Fu {
public void show() {
System.out.println("fu show");
}
}
class Zi extends Fu {
public void show() {
System.out.println("zi show");
}
public void method() {
System.out.println("zi method");
}
}
class Test1Demo {
public static void main(String[] args) {
Fu f = new Zi();
f.method();
f.show();
}
}
鐢变簬Fu涓病鏈塵ethod锛堬級鏂规硶锛岀敱姝よ绋嬪簭鍦ㄧ紪璇戠殑鏃跺€欏氨鍑虹幇闂
=======B绋嬪簭============
class A {
public void show() {
show2();
}
public void show2() {
System.out.println("鎴?);
}
}
class B extends A {
public void show2() {
System.out.println("鐖?);
}
}
class C extends B {
public void show() {
super.show();
}
public void show2() {
System.out.println("浣?);
}
}
public class Test2DuoTai {
public static void main(String[] args) {
A a = new B();
a.show();
B b = new C();
b.show();
}
}
绋嬪簭鐨勭粨鏋滐細
鐖?浣?/p>
鍒嗘瀽锛?/p>
A a = new B();
a.show(); 缂栬瘧鐪婤鐨勭埗绫籄鏈塻how锛堬級鏂规硶锛岀紪璇戦€氳繃 杩愯鐪嬪彸杈?瀛愮被娌℃湁show锛堬級鏂规硶 浣嗘槸瀛愮被缁ф壙浜嗙埗绫荤殑鏂规硶 鐢ㄧ埗绫籄 鐨剆how锛堬級鏂规硶锛宻how锛堬級鏂规硶 璋冪爺show2锛堬級锛堝瓙绫诲show2锛堬級閲嶅啓锛夌粨鏋滀负 鐖?/p>
B b = new C();
b.show(); 缂栬瘧鐪嬪乏杈?鐖剁被B鐢变簬缁ф壙浜咥 鎵€鏈夋湁show()鏂规硶锛岀紪璇戦€氳繃 杩愯鐪嬪彸杈瑰瓙绫籹how锛堬級鏂规硶 閲岄潰璋冪敤super.show2锛堬級璋冪敤瀛愮被鐨剆how2锛堬級 缁撴灉涓?浣?/p>
以上是关于澶氭€佷腑鐨勯鐩垎鏋愰的主要内容,如果未能解决你的问题,请参考以下文章
鍒涙柊浜у搧鐨勯渶姹傚垎鏋愶細鏈潵鐨勫浘涔︿細鏄粈涔堟牱瀛愶紵