版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、3、類的高級(jí)特性(抽象類、接口等)引入:本章繼續(xù)深入學(xué)習(xí)面向?qū)ο笏枷?。正課:1、抽象類并不是所有的類都是用來(lái)描繪對(duì)象的,如果一個(gè)類中沒(méi)有包含足夠的信息 來(lái)描繪一個(gè)具體的對(duì)象,這樣的類就是抽象類。用abstract修飾。定義抽象類:abstract class類名稱成員變量;方法();/定義一般方法abstract方法();定義抽象方法范例 testabstract :package testabstract;public abstract class Ani malpublic Ani mal(Stri ng aType)type = new Strin g(aType);public Str
2、i ng toStri ng()return "This is a " + type;/ Dummy method to be implemented in the derived classes public abstract void soun d();private String type; package testabstract;public class Cat exte nds Ani malpublic Cat(Stri ng aName)super("Cat"); / Call the base con structor n ame =
3、aName; / Supplied n ame breed = "Unknown" / Default breed valuepublic Cat(Stri ng aName, String aBreed)super("Cat"); / Call the base con structor n ame = aName;/ Supplied n amebreed = aBreed; / Supplied breed/ Return a String full of a cat's detailspublic Stri ng toStri ng()r
4、eturn super.toString() + "nit's " + name + " the " + breed;/ A miaowi ng methodpublic void soun d()System.out.pri ntln ("Miiaooww");private String name; / Name of a catprivate String breed; / Cat breed package testabstract;public class Dog exte nds Ani malpublic Dog
5、(Stri ng aName)super("Dog");con structorn ame = aName;/ Call thebase/ Supplied n amebreed = "Unknown"/ Default breed valuepublic Dog(String aName, String aBreed)/ Call thebasesuper("Dog");con structorn ame = aName;breed = aBreed;/ Supplied n ame/ Supplied breed/ Prese n
6、t a dog's details as a stri ngpublic Stri ng toStri ng()return super.toString() + "nit's " + name + " the " + breed; / A bark ing method public void soun d()System.out.pri ntln ("Woof Woof');/ Name of a Dog/ Dog breedprivate String name;private String breed; pack
7、age testabstract;public class Duck exte nds Ani malpublic Duck(Stri ng aName)super("Duck"); / Call the base con structor n ame = aName; / Supplied n ame breed = "Unknown" / Default breed valuepublic Duck(Stri ng aName, String aBreed)super("Duck"); / Call the base con st
8、ructor n ame = aName;/ Supplied n amebreed = aBreed; / Supplied breed/ Retur n a String full of a duck's detailspublic Stri ng toStri ng()return super.toString() + "nit's " + name + " the " + breed;/ A quack ing methodpublic void soun d()System.out.pri ntl n("Quack q
9、uackquack");private Stri ng n ame;/ Duck n ameprivate String breed; / Duck breed package testabstract;class Spaniel exte nds Dogpublic Spani el(Stri ng aName)super(aName, "Spa ni el");package testabstract;import java.util.Ra ndom;public class TestAbstractpublic static void main(String
10、 args)/ Create an array of three differe nt ani mals An imal theA ni mals = new Dog("Rover", "Poodle"),new Cat("Max", "Abyss in ia n"),new Duck("Dafy","Aylesbury"),new Spa ni el("Fido");Ani mal petChoice;/ Choice of pet/ Ran dom n
11、u mberRandom select = new Random();gen erator/ Make five ran dom choices of petfor(i nt i = 0; i < 5; i+) / Choose a ran dom ani mal as a petpetChoice = theA nimalsselect. nextl nt(theA nimals .len gth);System.out.pri ntln ("nY our choice:n" + petChoice);petChoice.so un d();/ Get the pe
12、t'sreacti on2、接口接口可以被用來(lái)實(shí)現(xiàn)類間多繼承結(jié)構(gòu)。接口內(nèi)部只能定義public 的抽象方法和靜態(tài)的、公有常量,因此所有的方法需要在子類中實(shí)現(xiàn)。接口定義方法:訪問(wèn)權(quán)限in terface接口名稱 exte nds 父接口名 1,父接口名2,./接口體程序范例 testinterface:package test in terface;public class Cat impleme nts PetOutputpublic Cat(Stri ng aName)super("Cat");/ Call the base con structorn ame =
13、 aName; / Supplied n amebreed = "Unknown" / Default breed valuepublic Cat(Stri ng aName, String aBreed)super("Cat");/ Call the base con structorn ame = aName;/ Supplied n amebreed = aBreed; / Supplied breed/ Return a String full of a cat's details public Stri ng toStri ng()re
14、tur n "n It's " + n ame + " the " + breed;/ A miaowi ng methodpublic void soun d()System.out.pri ntln ("Miiaooww");private String name; / Name of a catprivate String breed; / Cat breedpackage test in terface;public class Dog impleme nts PetOutputpublic Dog(Stri ng a
15、Name)n ame = aName;/ Supplied n amebreed = "Unknown" / Default breed valuepublic Dog(String aName, String aBreed) n ame = aName; / Supplied n ame breed = aBreed; / Supplied breed/ Prese nt a dog's details as a stri ngpublic Stri ng toStri ng()return "It's " + n ame + &quo
16、t; the " + breed;/ A bark ing methodpublic void soun d()System.out.pri ntln ("Woof Woof');public String getName()return n ame;private Stri ng n ame; / Name of a dogprivate String breed; / Dog breed package test in terface;public class Duck impleme nts PetOutputpublic Duck(Stri ng aName
17、)super("Duck"); / Call the base con structorn ame = aName;/ Supplied n amebreed = "Unknown" / Default breed valuepublic void layEgg()System.out.println("Egg laid");public Duck(Stri ng aName, String aBreed)super("Duck");/ Call the base con structorn ame = aName
18、;/ Supplied n amebreed = aBreed; / Supplied breed/ Retur n a String full of a duck's details public Stri ng toStri ng()retur n "n It's " + n ame + " the " + breed;/ A quack ing methodpublic void soun d()System.out.pri ntl n("Quack quackquack");private Stri ng n
19、ame; / Duck n ameprivate String breed; / Duck breedpackage test in terface;public in terface PetOutputvoid soun d();package test in terface;public in terface Read1void readNewspaper();package test in terface;/import testabstract.Read;class Spa niel exte nds Dog impleme nts Readl public Spani el(Stri
20、 ng aName)super(aName, "Spa ni el");public void readNewspaper( )System.out.pri ntl n("l can read Newspaper");package test in terface;import java.util.Ra ndom;public class Test In terfacepublic static void main(String args)/ PetOutput thePets = / Dog("Rover", "Poodle"),/new Cat("Max", "Abyss
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 垂體危象與垂體卒中講課課件
- 21、《槐鄉(xiāng)五月》第二課時(shí)
- 初二年級(jí)期中考試家長(zhǎng)會(huì)教學(xué)案例
- 二零二五年網(wǎng)絡(luò)零售商合作協(xié)議樣本2篇
- 新教材高考地理一輪復(fù)習(xí)課時(shí)作業(yè)二十四城鎮(zhèn)化課件新人教版
- 水利工程合同管理制度
- 黃金投資入門(mén)教學(xué)教案
- 九年級(jí)物理全冊(cè)192家庭電路中電流過(guò)大的原因課件新版新人教版
- 《科幻小說(shuō)賞析與寫(xiě)作》 課件 -第四章 “生命奇跡”的重述與復(fù)魅-《弗蘭肯斯坦》
- 二零二五年礦產(chǎn)品資源整合開(kāi)發(fā)合作協(xié)議書(shū)3篇
- 工程管理中心管理制度
- 下套管危害識(shí)別和風(fēng)險(xiǎn)評(píng)估
- 翼狀胬肉病人的護(hù)理
- GB/T 12914-2008紙和紙板抗張強(qiáng)度的測(cè)定
- GB/T 1185-2006光學(xué)零件表面疵病
- ps6000自動(dòng)化系統(tǒng)用戶操作及問(wèn)題處理培訓(xùn)
- 家庭教養(yǎng)方式問(wèn)卷(含評(píng)分標(biāo)準(zhǔn))
- 城市軌道交通安全管理課件(完整版)
- 線纜包覆擠塑模設(shè)計(jì)和原理
- TSG ZF001-2006 安全閥安全技術(shù)監(jiān)察規(guī)程
- 部編版二年級(jí)語(yǔ)文下冊(cè)《蜘蛛開(kāi)店》
評(píng)論
0/150
提交評(píng)論