freepeople性欧美熟妇, 色戒完整版无删减158分钟hd, 无码精品国产vα在线观看DVD, 丰满少妇伦精品无码专区在线观看,艾栗栗与纹身男宾馆3p50分钟,国产AV片在线观看,黑人与美女高潮,18岁女RAPPERDISSSUBS,国产手机在机看影片

正文內容

java語言程序設計(中)清華大學ppt-資料下載頁

2025-01-19 08:30本頁面
  

【正文】 方法中的多態(tài)方法 (續(xù) ) —— 例5_12 清華大學 鄭莉 JAVA語言程序設計 76 class RoundGlyph extends Glyph { int radius = 1。 RoundGlyph(int r) { radius = r。 ((), radius = + radius)。 } void draw() { ((), radius = + radius)。 } } public class PolyConstructors { public static void main(String[] args) { new RoundGlyph(5)。 } } 構造方法中的多態(tài)方法 (續(xù) ) —— 例5_12 構造方法與多態(tài) 清華大學 鄭莉 JAVA語言程序設計 77 ? 運行結果 Glyph() before draw() (), radius = 0 Glyph() after draw() (), radius = 5 ? 說明 – 在 Glyph中, draw()方法是抽象方法,在子類RoundGlyph中對此方法進行了覆蓋。 Glyph的構造方法調用了這個方法 – 從運行的結果可以看到:當 Glyph的構造方法調用 draw()時, radius的值甚至不是默認的初始值1,而是 0 構造方法與多態(tài) 構造方法中的多態(tài)方法 (續(xù) ) —— 例 5_12運行結果 清華大學 鄭莉 JAVA語言程序設計 78 ? 定義構造方法的注意事項 –用盡可能少的動作把對象的狀態(tài)設置好 –如果可以避免,不要調用任何方法 –在構造方法內唯一能夠安全調用的是在基類中具有 final屬性的那些方法(也適用于 private方法,它們自動具有 final屬性)。這些方法不能被覆蓋,所以不會出現(xiàn)上述潛在的問題 構造方法中的多態(tài)方法 構造方法與多態(tài) 清華大學 鄭莉 JAVA語言程序設計 79 內部類 ? 內部類 – 在另一個類或方法的定義中定義的類 – 可訪問其外部類中的所有數(shù)據(jù)成員和方法成員 – 可對邏輯上相互聯(lián)系的類進行分組 – 對于同一個包中的其他類來說,能夠隱藏 – 可非常方便地編寫事件驅動程序 – 聲明方式 ? 命名的內部類:可在類的內部多次使用 ? 匿名內部類:可在 new關鍵字后聲明內部類,并立即創(chuàng)建一個對象 – 假設外層類名為 Myclass, 則該類的內部類名為 ? Myclass$ (c1為命名的內部類名 ) ? Myclass$ (表示類中聲明的第一個匿名內部類 ) 清華大學 鄭莉 JAVA語言程序設計 80 public class Parcel1 { class Contents { //內部類 private int i = 11。 public int value() { return i。 } } class Destination { //內部類 private String label。 Destination(String whereTo) { label = whereTo。 } String readLabel() { return label。 } } public void ship(String dest) { Contents c = new Contents()。 Destination d = new Destination(dest)。 (())。 } 內部類 —— 內部類 清華大學 鄭莉 JAVA語言程序設計 81 public static void main(String[] args) { Parcel1 p = new Parcel1()。 (Tanzania)。 } } ? 說明 – 在 Parcel1類中聲明了兩個內部類 Contents、 Destination – 在 ship方法中生成兩個內部類對象,并調用了內部類中聲明的一個方法 內部類 —— 內部類 清華大學 鄭莉 JAVA語言程序設計 82 ? 外部類的方法可以返回內部類的引用變量 public class Parcel2 { class Contents { private int i = 11。 public int value() { return i。 } } class Destination { private String label。 Destination(String whereTo) { label = whereTo。 } String readLabel() { return label。 } } public Destination to(String s) { return new Destination(s)。 } public Contents cont() { return new Contents()。 } 內部類 —— 內部類 清華大學 鄭莉 JAVA語言程序設計 83 public void ship(String dest) { Contents c = cont()。 Destination d = to(dest)。 (())。 } public static void main(String[] args) { Parcel2 p = new Parcel2()。 (Tanzania)。 Parcel2 q = new Parcel2()。 c = ()。 d =(Borneo)。 } } ? 說明 – to()方法返回內部類 Destination的引用 – cont()方法返回內部類 Contents的引用 內部類 —— 內部類 清華大學 鄭莉 JAVA語言程序設計 84 ? 內部類實現(xiàn)接口 – 可以完全不被看到,而且不能被調用 – 可以方便實現(xiàn)“隱藏實現(xiàn)細則”。你所能得到的僅僅是指向基類 (base class)或者接口的一個引用 ? 例子 abstract class Contents { abstract public int value()。 } interface Destination { String readLabel()。 } 內部類 —— 內部類實現(xiàn)接口 內部類 清華大學 鄭莉 JAVA語言程序設計 85 public class Parcel3 { private class PContents extends Contents { private int i = 11。 public int value() { return i。 } } protected class PDestination implements Destination { private String label。 private PDestination(String whereTo) { label = whereTo。} public String readLabel() { return label。 } } public Destination dest(String s) { return new PDestination(s)。 } public Contents cont() { return new PContents()。 } } 內部類 —— 內部類 清華大學 鄭莉 JAVA語言程序設計 86 class Test { public static void main(String[] args) { Parcel3 p = new Parcel3()。 Contents c = ()。 Destination d = (Tanzania)。 } } ? 說明 – 內部類 PContents實現(xiàn)了抽象了 Contents – 內部類 PDenstination實現(xiàn)了接口 Destination – 外部類 Test中不能聲明對 private的內部類的引用 內部類 —— Parcel3測試 內部類 清華大學 鄭莉 JAVA語言程序設計 87 ? 在方法內定義一個內部類 –為實現(xiàn)某個接口,產生并返回一個引用 –為解決一個復雜問題,需要建立一個類,而又不想它為外界所用 內部類 —— 方法中的內部類 內部類 清華大學 鄭莉 JAVA語言程序設計 88 public class Parcel4 { public Destination dest(String s) { class PDestination implements Destination { private String label。 private PDestination(String whereTo) { label = whereTo。 } public String readLabel() { return label。 } return new PDestination(s)。 } public static void main(String[] args) { Parcel4 p = new Parcel4()。 Destination d = (Tanzania)。 } } 內部類 —— 內部類 清華大學 鄭莉 JAVA語言程序設計 89 public class Parcel5 { private void internalTracking(boolean b) { if(b) { class TrackingSlip { private String id。 TrackingSlip(String s) { id = s。 } String getSlip() { return id。 } } TrackingSlip ts = new TrackingSlip(slip)。 String s = ()。 } } public void track() { internalTracking(true)。 } public static void main(String[] args) { Parcel5 p = new Parcel5()。
點擊復制文檔內容
教學課件相關推薦
文庫吧 www.dybbs8.com
備案圖鄂ICP備17016276號-1