抽象類: abstract修飾
抽象方法必須定義在抽象類中,抽象類不能創(chuàng)建對象.
在抽象方法中可以不定義抽象方法,作用是:讓該類不能建立對象.
特點(diǎn)是:
1.定義在抽象類中
2.方法和類都用abstract修飾,在抽象類中的方法不寫abstract也默認(rèn)的是抽象方法.
3.不能用new來創(chuàng)建對象,調(diào)用抽象方法沒意義.
4.抽象類中的方法被使用,必須由子類覆寫其所有的抽象方法后,才能建立子類對象進(jìn)行調(diào)用.
如果子類只覆蓋了部分的抽象方法.那么該子類還是一個抽象類.
5.抽象類不可以被實(shí)例化.
繼承:(extends)
關(guān)鍵字:extends,一般用于類與類之間的所屬關(guān)系.java中是單繼承,多實(shí)現(xiàn)(implements).
讓類與類之間產(chǎn)生關(guān)系,從而才有了多態(tài)的特性.
當(dāng)兩個類有了繼承關(guān)系以后.就可以在子類中調(diào)用父類的方法和屬性,一般使用 super 關(guān)鍵字.
其用法和this關(guān)鍵字類似.super調(diào)用的是父類的,而this是調(diào)用的方法本身的.

1 abstract class Employee 2 { 3 PRivate String name; 4 private String id; 5 private double pay; 6 //構(gòu)造函數(shù) 7 Employee(String name,String id,double pay) 8 { 9 this.name = name;10 this.id = id;11 this.pay = pay;12 }13 public abstract void work(); //抽象類.14 15 }16 17 class NormalWorker extends Employee18 { 19 NormalWorker(String name,String id,double pay)20 {21 super(name,id,pay);22 }23 24 public void work()25 {26 System.out.println("NormalWorker Work");27 }28 29 }30 31 class Manager extends Employee32 {33 private double bonus;34 35 Manager(String name,String id,double pay,double bonus)36 {37 super(name,id,pay);38 this.bonus = bonus;39 }40 public void work()41 {42 System.out.println("Manager Work");43 }44 45 }46 class AbstractTest47 {48 public static void main(String args[])49 {50 Manager M = new Manager("lisi","0910",10000,5000);51 M.work();52 53 }54 }View Code多態(tài):向上轉(zhuǎn)型:將子類的對象轉(zhuǎn)給父類的引用.
向下轉(zhuǎn)向;將父類轉(zhuǎn)換成子類類型.
一般使用的是向上轉(zhuǎn)型,向下轉(zhuǎn)型會出現(xiàn)不安全的問題,例如,貓是動物,但是我們不能說動物就是貓.

1 /* 2 多態(tài): 3 人:男人,女人 4 動物:貓,狗 5 貓 m =new 貓(); 6 動物 m = new 貓(); 7 特點(diǎn):提高代碼的拓展. 8 */ 9 abstract class Animal10 {11 abstract void eat();12 }13 14 class Cat extends Animal15 {16 public void eat()17 {18 System.out.println("吃魚");19 }20 public void catchMouse()21 {22 System.out.println("抓老鼠");23 }24 }25 26 class Dog extends Animal27 {28 public void eat()29 {30 System.out.println("啃骨頭");31 }32 public void kanJia()33 {34 System.out.println("看家");35 }36 }37 38 class Pig extends Animal39 {40 public void eat()41 {42 System.out.println("飼料");43 }44 public void swim()45 {46 System.out.println("游泳");47 }48 }49 50 51 52 class DuoTaiDemo53 {54 public static void main(String args[])55 {56 /*57 Cat c = new Cat();58 c.eat();59 Dog d = new Dog();60 d.eat();61 Pig p = new Pig();62 p.eat();63 */64 /*65 function(new Cat());66 function(new Dog());67 function(new Pig());68 */69 //Animal c = new Cat(); //向上轉(zhuǎn)型 70 function(new Cat());71 function(new Dog());72 function(new Pig());73 }74 /*75 public static void function(Cat c)76 {77 c.eat();78 }79 public static void function(Dog d)80 {81 d.eat();82 }83 public static void function(Pig p)84 {85 p.eat();86 }87 */88 public static void function(Animal a)89 {90 a.eat();91 }92 }View Code
1 abstract class Animal 2 { 3 abstract void eat(); 4 } 5 6 class Cat extends Animal 7 { 8 public void eat() 9 {10 System.out.println("吃魚");11 }12 public void catchMouse()13 {14 System.out.println("抓老鼠");15 }16 }17 18 class Dog extends Animal19 {20 public void eat()21 {22 System.out.println("啃骨頭");23 }24 public void kanJia()25 {26 System.out.println("看家");27 }28 }29 30 class Pig extends Animal31 {32 public void eat()33 {34 System.out.println("飼料");35 }36 public void swim()37 {38 System.out.println("游泳");39 }40 }41 42 43 44 class DuoTaiDemo245 {46 public static void main(String args[])47 {48 /*49 Animal a = new Cat(); //向上轉(zhuǎn)型 50 a.eat();51 Cat c = (Cat)a; //向下轉(zhuǎn)型,將父類的引用轉(zhuǎn)成子類類型.52 c.catchMouse();53 */54 function(new Cat());55 function(new Dog());56 function(new Pig());57 }58 59 public static void function(Animal a)60 {61 a.eat();62 if(a instanceof Cat)63 {64 Cat c = (Cat)a;65 c.catchMouse();66 }67 else if(a instanceof Dog)68 {69 Dog d = (Dog)a;70 d.kanJia();71 }72 else if(a instanceof Pig)73 {74 Pig p = (Pig)a;75 p.swim();76 }77 78 }79 }View Code
1 /* 2 練習(xí): 3 基礎(chǔ)班學(xué)生:學(xué)習(xí),睡覺. 4 高級班學(xué)生:學(xué)習(xí),睡覺. 5 */ 6 abstract class Student 7 { 8 public abstract void study(); 9 public void sleep()10 {11 System.out.println("躺著睡.");12 }13 14 }15 16 class DoStudent17 {18 public void doSomething(Student stu)19 {20 stu.study();21 stu.sleep();22 }23 }24 25 class BaseStudent extends Student26 {27 public void study()28 {29 System.out.println("base study");30 }31 public void sleep()32 {33 System.out.println("站著睡");34 }35 }36 37 class AdvStudent extends Student38 {39 public void study()40 {41 System.out.println("adv study");42 }43 }44 45 46 47 48 49 class DuoTaiTest50 {51 public static void main(String args[])52 {53 /*54 BaseStudent bs = new BaseStudent();55 bs.study();56 bs.sleep();57 AdvStudent as = new AdvStudent();58 as.study();59 as.sleep();60 */61 DoStudent ds = new DoStudent();62 ds.doSomething(new BaseStudent());63 ds.doSomething(new AdvStudent());64 }65 }View Code多態(tài)中成員的特點(diǎn):1.成員函數(shù)的特點(diǎn); 在編譯時,參閱引用型變量所屬的類中是否有調(diào)用方法,有的話編譯會通過,沒有的話會編譯失敗
在運(yùn)行時,參閱對象所屬的類中屬否有調(diào)用方法.
面試中可能遇見的問題.問調(diào)用顯示結(jié)果.
成員函數(shù)(非靜態(tài)的)在多態(tài)調(diào)用時,編譯看左邊,運(yùn)行看右邊.
靜態(tài)函數(shù),無論編譯還是運(yùn)行,都看左邊
看了一下之前寫的那篇筆記,感覺都沒什么能寫的了,不知道寫些什么,越往后學(xué)感覺都是得理解的,越學(xué)越難的說,最近又沒什么狀態(tài),只好慢慢來,慢慢學(xué)習(xí)了.
新聞熱點(diǎn)
疑難解答
圖片精選