20 抽象类与接口
为抽象类与接口实例化----通过对象的多态性
抽象类和接口之所以不能直接实例化,是因为其内部包含了各个抽象方法,抽象方法本身都是未实现的方法,所以无法调用。
通过对象多态性可以发现,子类发生了向上转型关系后,所调用的全部方法都是被覆写过的方法。
1 abstract class A{ // 定义抽象类A 2 public abstract void print() ; // 定义抽象方法print() 3 }; 4 class B extends A { // 定义子类,继承抽象类 5 public void print(){ // 覆写抽象方法 6 System.out.println("Hello World!!!") ; 7 } 8 }; 9 public class AbstractCaseDemo01{ 10 public static void main(String args[]){ 11 A a = new B() ; // 通过子类为抽象类实例化 12 a.print() ; 13 } 14 };
1 interface A{ // 定义接口类A 2 public abstract void print() ; // 定义抽象方法print() 3 }; 4 class B implements A { // 定义子类,实现接口类 5 public void print(){ // 覆写抽象方法 6 System.out.println("Hello World!!!") ; 7 } 8 }; 9 public class AbstractCaseDemo01{ 10 public static void main(String args[]){ 11 A a = new B() ; // 通过子类为接口类实例化 12 a.print() ; 13 } 14 };
抽象类的应用----定义模板
人说话的模板
1 abstract class Person{ 2 private String name ; // 定义name属性 3 private int age ; // 定义age属性 4 public Person(String name,int age){ 5 this.name = name ; 6 this.age = age ; 7 } 8 public String getName(){ 9 return this.name ; 10 } 11 public int getAge(){ 12 return this.age ; 13 } 14 public void say(){ // 人说话是一个具体的功能 15 System.out.println(this.getContent()) ; // 输出内容 16 } 17 public abstract String getContent() ; // 说话的内容由子类决定 18 }; 19 class Student extends Person{ 20 private float score ; 21 public Student(String name,int age,float score){ 22 super(name,age) ; // 调用父类中的构造方法 23 this.score = score ; 24 } 25 public String getContent(){ 26 return "学生信息 --> 姓名:" + super.getName() + 27 ";年龄:" + super.getAge() + 28 ";成绩:" + this.score ; 29 } 30 }; 31 class Worker extends Person{ 32 private float salary ; 33 public Worker(String name,int age,float salary){ 34 super(name,age) ; // 调用父类中的构造方法 35 this.salary = salary ; 36 } 37 public String getContent(){ 38 return "工人信息 --> 姓名:" + super.getName() + 39 ";年龄:" + super.getAge() + 40 ";工资:" + this.salary ; 41 } 42 }; 43 public class AbstractCaseDemo02{ 44 public static void main(String args[]){ 45 Person per1 = null ; // 声明Person对象 46 Person per2 = null ; // 声明Person对象 47 per1 = new Student("张三",20,99.0f) ; // 学生是一个人 48 per2 = new Worker("李四",30,3000.0f) ; // 工人是一个人 49 per1.say() ; // 学生说学生的话 50 per2.say() ; // 工人说工人的话 51 } 52 };
又如违纪卡的模板:
接口的实际应用----制定标签
1 interface USB{ // 定义了USB接口 2 public void start() ; // USB设备开始工作 3 public void stop() ; // USB设备结束工作 4 } 5 class Computer{ 6 public static void plugin(USB usb){ // 电脑上可以插入USB设备 7 usb.start() ; 8 System.out.println("=========== USB 设备工作 ========") ; 9 usb.stop() ; 10 } 11 }; 12 class Flash implements USB{ 13 public void start(){ // 覆写方法 14 System.out.println("U盘开始工作。") ; 15 } 16 public void stop(){ // 覆写方法 17 System.out.println("U盘停止工作。") ; 18 } 19 }; 20 class Print implements USB{ 21 public void start(){ // 覆写方法 22 System.out.println("打印机开始工作。") ; 23 } 24 public void stop(){ // 覆写方法 25 System.out.println("打印机停止工作。") ; 26 } 27 }; 28 public class InterfaceCaseDemo02{ 29 public static void main(String args[]){ 30 Computer.plugin(new Flash()) ; 31 Computer.plugin(new Print()) ; 32 } 33 };
工厂设计模式----java开发中最常使用的一种设计模式
1 interface Fruit{ // 定义一个水果接口 2 public void eat() ; // 吃水果 3 } 4 class Apple implements Fruit{ 5 public void eat(){ 6 System.out.println("** 吃苹果。") ; 7 } 8 }; 9 class Orange implements Fruit{ 10 public void eat(){ 11 System.out.println("** 吃橘子。") ; 12 } 13 }; 14 public class InterfaceCaseDemo03{ 15 public static void main(String args[]){ 16 Fruit f = new Apple() ; // 实例化接口 17 f.eat() ; 18 } 19 };
分析:主方法,应该就表示一个客户端,主方法的代码越少越好。此时,直接在主方法中指定了要操作的子类,如果要更换子类,则肯定要修改客户端。就表示跟特定的子类紧密耦合在一起了。
注:此过渡端在程序中就称为工厂设计
1 interface Fruit{ // 定义一个水果接口 2 public void eat() ; // 吃水果 3 } 4 class Apple implements Fruit{ 5 public void eat(){ 6 System.out.println("** 吃苹果。") ; 7 } 8 }; 9 class Orange implements Fruit{ 10 public void eat(){ 11 System.out.println("** 吃橘子。") ; 12 } 13 }; 14 class Factory{ // 定义工厂类 15 public static Fruit getInstance(String className){ 16 Fruit f = null ; 17 if("apple".equals(className)){ // 判断是否要的是苹果的子类 18 f = new Apple() ; 19 } 20 if("orange".equals(className)){ // 判断是否要的是橘子的子类 21 f = new Orange() ; 22 } 23 return f ; 24 } 25 }; 26 public class InterfaceCaseDemo05{ 27 public static void main(String args[]){ 28 Fruit f = Factory.getInstance(args[0]) ; // 实例化接口 29 if(f!=null){ // 判断是否取得实例 30 f.eat() ; 31 } 32 } 33 };
代理设计模式
1 interface Network{ 2 public void browse() ; // 浏览 3 } 4 class Real implements Network{ 5 public void browse(){ 6 System.out.println("上网浏览信息") ; 7 } 8 }; 9 class Proxy implements Network{ 10 private Network network ; // 代理对象 11 public Proxy(Network network){ 12 this.network = network ; 13 } 14 public void check(){ 15 System.out.println("检查用户是否合法。") ; 16 } 17 public void browse(){ 18 this.check() ; 19 this.network.browse() ; // 调用真实的主题操作 20 } 21 }; 22 public class ProxyDemo{ 23 public static void main(String args[]){ 24 Network net = null ; 25 net = new Proxy(new Real()) ;// 指定代理操作 26 net.browse() ; // 客户只关心上网浏览一个操作 27 } 28 };
适配器设计----在日后学习java的图形界面上用得非常多,但在javaEE的开发中并不很常见
1 interface Window{ // 定义Window接口,表示窗口操作 2 public void open() ; // 打开 3 public void close() ; // 关闭 4 public void activated() ; // 窗口活动 5 public void iconified() ; // 窗口最小化 6 public void deiconified();// 窗口恢复大小 7 } 8 abstract class WindowAdapter implements Window{ 9 public void open(){} ; // 打开 10 public void close(){} ; // 关闭 11 public void activated(){} ; // 窗口活动 12 public void iconified(){} ; // 窗口最小化 13 public void deiconified(){};// 窗口恢复大小 14 }; 15 class WindowImpl extends WindowAdapter{ 16 public void open(){ 17 System.out.println("窗口打开。") ; 18 } 19 public void close(){ 20 System.out.println("窗口关闭。") ; 21 } 22 }; 23 public class AdapterDemo{ 24 public static void main(String args[]){ 25 Window win = new WindowImpl() ; 26 win.open() ; 27 win.close() ; 28 } 29 };
内部类的扩展
在一个抽象类中包含一个接口:
1 abstract class A{ // 定义抽象类 2 public abstract void printA() ; // 抽象方法 3 interface B{ // 定义内部接口 4 public void printB() ; // 定义抽象方法 5 } 6 }; 7 class X extends A{ // 继承抽象类 8 public void printA(){ 9 System.out.println("HELLO --> A") ; 10 } 11 class Y implements B{ // 定义内部类实现内部接口 12 public void printB(){ 13 System.out.println("HELLO --> B") ; 14 } 15 }; 16 }; 17 public class InnerExtDemo01{ 18 public static void main(String args[]){ 19 A.B b = new X().new Y() ; 20 b.printB() ; 21 } 22 };
在一个接口中定义一个抽象类:
1 interface A{ // 定义接口 2 public void printA() ; // 抽象方法 3 abstract class B{ // 定义内部抽象类 4 public abstract void printB() ; // 定义抽象方法 5 } 6 }; 7 class X implements A{ // 实现接口 8 public void printA(){ 9 System.out.println("HELLO --> A") ; 10 } 11 class Y extends B{ // 继承抽象类 12 public void printB(){ 13 System.out.println("HELLO --> B") ; 14 } 15 }; 16 }; 17 public class InnerExtDemo02{ 18 public static void main(String args[]){ 19 A.B b = new X().new Y() ; 20 b.printB() ; 21 } 22 };
注:在实际开发中,以上的设计并不很常见,因为代码的结构有些混乱了。

浙公网安备 33010602011771号