回顧
一、抽象類
1.抽象類的基本概念
抽象類定義規(guī)則
學習小結(jié)
抽象類的特征:
①抽象類中可以有構(gòu)造方法
抽象類的構(gòu)造方法必須在子類中被調(diào)用前痘,并且先默認調(diào)用父類構(gòu)造方法,再調(diào)用子類的構(gòu)造方法担忧,這個抽象方法無法直接被外部實例化對象直接調(diào)用芹缔。
package com.Javastudy2;
/**
* @author Y.W.
* @date 2017年9月18日 下午9:45:45
* @Description TODO 抽象類中構(gòu)造方法的定義使用
*/
public class P325_13_2 {
public static void main(String[] args) {
Student12 s = new Student12("張三", 20, "學生"); // 創(chuàng)建Student類對象s
System.out.println(s.talk()); // 調(diào)用被覆寫的方法
}
}
abstract class Person23 { // 定義一個抽象類Person
String name;
int age;
String occupation;
public Person23(String name, int age, String occupation) { // 定義構(gòu)造函數(shù)
this.name = name;
this.age = age;
this.occupation = occupation;
}
public abstract String talk(); // 聲明一個抽象方法talk()
}
class Student12 extends Person23 { // 聲明抽象類的子類
public Student12(String name, int age, String occupation) {
// 在這里必須強調(diào)用抽象類中的構(gòu)造方法
super(name, age, occupation);
}
public String talk() { // 覆寫talk()方法
return "學生--》姓名:" + this.name + ",年齡:" + this.age + "瓶盛,職業(yè):" + this.occupation + "最欠!";
}
}
運行結(jié)果:
②抽象類不能使用final定義
③在外部抽象類上無法使用static聲明,但是內(nèi)部抽象類卻可以使用static定義惩猫,使用static定義的內(nèi)部抽象類就是一個外部類芝硬。
package com.Javastudy2;
/**
* @author Y.W.
* @date 2017年9月18日 下午10:04:22
* @Description TODO 驗證static定義的內(nèi)部抽象類
*/
public class P326_13_3 {
public static void main(String[] args) {
Book3.CD cd = new JavaCD(); // 實例化對象
cd.get();
}
}
abstract class Book3 {
public abstract void print(); // 抽象方法
static abstract class CD { // 靜態(tài)內(nèi)部抽象類
public abstract void get(); // 抽象方法
}
}
class JavaCD extends Book3.CD {
public void get() {
System.out.println("java 學習");
}
}
運行結(jié)果:
④抽象類之中可以沒有抽象方法,但即便沒有抽象方法的抽象類也不能夠直接在外部通過關鍵字new實例化轧房。
2拌阴、抽象類應用——模板設計模式
不同的子類可以以不同的方式實現(xiàn)這些抽象方法,從而對剩余的邏輯有不同的實現(xiàn)奶镶,這就是模板方法模式皮官。
三類事物實現(xiàn)如下功能:
機器人:吃飯脯倒、工作;
美女:吃飯捺氢、跑步藻丢、睡覺;
帥哥:吃飯摄乒、工作悠反、跑步、睡覺馍佑。
package com.Javastudy2;
/**
* @author Y.W.
* @date 2017年9月18日 下午10:17:52
* @Description TODO 模板設計模式
*/
public class P327_13_4 {
public static void main(String[] args) {
Action actA = new Robot(); // 機器人行為
actA.command(Action.EAT);
actA.command(Action.WORK);
Action actB = new Woman(); // 美女的行為
actB.command(Action.EAT + Action.SLEEP + Action.RUN);
Action actC = new Man(); // 帥哥的行為
actC.command(Action.EAT + Action.SLEEP + Action.RUN + Action.WORK);
}
}
abstract class Action { // 表示操作行為
public static final int EAT = 1;
public static final int WORK = 2;
public static final int SLEEP = 5;
public static final int RUN = 10;
public abstract void eat(); // 聲明一抽象類
public abstract void sleep(); // 聲明一抽象類
public abstract void run(); // 聲明一抽象類
public abstract void work(); // 聲明一抽象類
public void command(int ch) { // 具體的命令
switch (ch) {
case EAT:
this.eat();
break;
case SLEEP:
this.sleep();
break;
case RUN:
this.run();
break;
case WORK:
this.work();
break;
case EAT + WORK:
this.eat();
this.work();
break;
case EAT + SLEEP + RUN:
this.eat();
this.sleep();
this.run();
break;
case EAT + SLEEP + RUN + WORK:
this.eat();
this.work();
this.run();
this.sleep();
break;
}
}
}
class Robot extends Action { // 定義子類Robot行為
public void eat() { // 覆寫抽象方法
System.out.println("為機器人加燃料斋否。");
}
public void sleep() {
}
public void run() {
}
public void work() {
System.out.println("讓機器人開始工作。");
}
}
class Woman extends Action { // 定義子類Woman行為
public void eat() {
System.out.println("請美女吃飯拭荤。");
}
public void sleep() {
System.out.println("讓美女睡美容覺茵臭。");
}
public void run() {
System.out.println("讓美女跑步健身。");
}
public void work() {
}
}
class Man extends Action { // 定義子類Man行為
public void eat() {
System.out.println("帥哥吃早飯舅世、午飯旦委、晚飯、加餐雏亚。");
}
public void sleep() {
System.out.println("帥哥休息好以恢復精神和體力缨硝。");
}
public void run() {
System.out.println("帥哥通過跑步來鍛煉身體。");
}
public void work() {
System.out.println("帥哥為了和美女幸福地生活在一起罢低,要好好工作查辩。");
}
}
運行結(jié)果:
如果想實現(xiàn)指定操作,只需將方法按照要求覆寫即可网持,相當于父類定義出了一個操作模板宜岛。實際用的時候也可以在servlet程序設計上使用。
二功舀、接口
接口(interface)是一種特殊的類萍倡,結(jié)構(gòu)上和抽象類非常相似,也具有數(shù)據(jù)成員與抽象方法日杈,但與抽象類又有不同遣铝。
1.接口的基本概念
接口里的數(shù)據(jù)成員必須初始化,且數(shù)據(jù)成員均為常量莉擒,常見的是全局變量酿炸。
接口允許定義默認方法,即default方法涨冀,也可以稱作Defender方法填硕,或者虛擬擴展方法(Virtual extension methods)。
語法:
interface 接口名稱 { // 定義接口類
final 數(shù)據(jù)類型 成員名稱 = 常量; // 數(shù)據(jù)成員必須賦初值
abstract 返回值的數(shù)據(jù)類型 方法名稱(參數(shù)···); // 抽象方法,注意在抽象方法里沒有定義方法體
default 返回值的數(shù)據(jù)類型 方法名稱(參數(shù)···){ // 默認方法扁眯,包含方法體
···方法體···
}
}
定義接口例:
interface A { // 定義一個接口
public static final String INFO = "Hello Worlk."; // 全局常量
public abstract void print(); // 抽象方法
}
帶默認方法的接口定義例:
interface B { // 定義一個接口
public static final String INFO = "Hello Worlk."; // 全局常量
public abstract void print(); // 抽象方法
default public void otherprint() { // 帶方法體的默認方法
System.out.println("default menthods!"); // 默認方法的方法體
}
}
思考
出差中壮莹,舟車勞頓,有點累姻檀。也確實有一段時間沒有看書了命满。。绣版。
記于2017年9月18日夜