1.抽象类的声明:
1 abstract class Book
2 {
3 String name;//属性
4 int price;
5 public void printName()//普通方法
6 {
7 System.out.println("书名:"+name);
8 }
9 public abstract void printPrice();//抽象方法
10 }
2.抽象类不能直接实例化:
Book book = new Book();//错误的,Book是抽象类
3.抽象方法只需声明,不需实现:
public abstract void printPrice(int price);//抽象方法
4.抽象类的使用:必须有子类。
抽象类的子类必须重写全部的抽象方法后才能被实例化。
1 abstract class Book
2 {
3 String name;//属性
4 int price;
5 String press;
6 public void printName(String name)//普通方法
7 {
8 System.out.print("书名:"+name);
9 }
10 public abstract void printPrice(int price);//抽象方法
11 }
12 class Readers extends Book
13 {
14 public void printPrice(int price)
15 {
16 System.out.println(" 价格:"+price+"元");
17 }
18 }
19 class Writer extends Book
20 {
21 public void printPrice(int price)
22 {
23 System.out.print(" 价格:"+price+"元");
24 }
25 public void printPress(String press)
26 {
27 System.out.print(" 出版社:"+press);
28 }
29 }
30 public class T03
31 {
32 public static void main(String[] args)
33 {
34 Readers r = new Readers();
35 Writer w = new Writer();
36 r.printName("《基督山伯爵》");
37 r.printPrice(20);
38 w.printName("《欧亨利短篇小说集》");
39 w.printPrice(30);
40 w.printPress("新华出版社");
41 }
42 }
输出结果:
书名:《基督山伯爵》 价格:20元
书名:《欧亨利短篇小说集》 价格:30元 出版社:新华出版社
5.外部抽象类不能用static声明,但内部抽象类可以。
1 abstract class Book
2 {
3 public abstract void print();
4 static abstract class Name
5 {
6 public abstract void getName();
7 }
8 }
9 class Readers extends Book.Name
10 {
11 public void getName()
12 {
13 System.out.println("《茶花女》");
14 }
15 }
16 public class T03
17 {
18 public static void main(String[] args)
19 {
20 Book.Name name = new Readers();
21 name.getName();
22 }
23 }
6.抽象类应用——模板设计模式。
举一个例子:
现在有三类事物丈夫、妻子、儿子,这三类事物可以完成的功能如下。
丈夫:吃饭、睡觉、工作、锻炼。
妻子:吃饭、锻炼、睡觉、做家务。
儿子:吃饭、睡觉、学习。

1 abstract class Action
2 {
3 public static int eat;
4 public static int sleep;
5 public static int sport;
6 public abstract void EAT();
7 public abstract void SLEEP();
8 public abstract void SPORT();
9 }
10 class Husband extends Action
11 {
12 public void EAT()
13 {
14 System.out.println("丈夫吃的好,身体才好!");
15 }
16 public void SLEEP()
17 {
18 System.out.println("丈夫休息好才能好好工作。");
19 }
20 public void SPORT()
21 {
22 System.out.println("丈夫早上总要去锻炼身体。");
23 }
24 public void WORK()
25 {
26 System.out.println("丈夫好好工作,家庭才能幸福!");
27 }
28 }
29 class Wife extends Action
30 {
31 public void EAT()
32 {
33 System.out.println("妻子做的菜都非常好吃。");
34 }
35 public void SLEEP()
36 {
37 System.out.println("妻子喜欢睡个美容觉。");
38 }
39 public void SPORT()
40 {
41 System.out.println("妻子早上陪伴丈夫去锻炼身体。");
42 }
43 public void HOUSEWORK()
44 {
45 System.out.println("妻子收拾家务,照顾孩子。");
46 }
47 }
48 class Son extends Action
49 {
50 public void EAT()
51 {
52 System.out.println("儿子非常喜欢吃他妈妈做的菜。");
53 }
54 public void SLEEP()
55 {
56 System.out.println("儿子休息好才能好好学习。");
57 }
58 public void SPORT(){}
59 public void STUDY()
60 {
61 System.out.println("儿子要好好学习,不辜负父母的期望。");
62 }
63 }
64 public class T03
65 {
66 public static void main(String[] args)
67 {
68 Husband h = new Husband();
69 Wife w = new Wife();
70 Son s = new Son();
71 h.EAT();h.SLEEP();h.WORK();h.SPORT();
72 w.EAT();w.SLEEP();w.HOUSEWORK();w.SPORT();
73 s.EAT();s.SLEEP();s.STUDY();s.SPORT();
74 }
75 }
参考文献:《Java从入门到精通》第2版。
来源:https://www.cnblogs.com/wxywxy/p/6719421.html
