3.14、接口和抽象类

  1. 3.14、接口和抽象类
    1. 3.14.1、适配器设计模式
    2. 3.14.2、工厂设计模式
    3. 3.14.3、代理设计模式
    4. 3.14.4、抽象类和接口的区别(绝对重点)

3.14、接口和抽象类

3.14.1、适配器设计模式

正常情况下一个接口的子类要实现全部的抽象方法

interface Window{
    public void open();
    public void close();
    public void icon();
    public void unicon();
}
class MyWindow implements Window{
}

在Mywindow类此时肯定要覆写全部的抽象方法,但是现在希望可以根据自己的需要来选择性的覆写,那么该怎么实现呢?

用一个类先将接口实现了,但是所有的方法都属于空实现,之后再继承此类。

应该使用抽象类,因为抽象类也不能直接使用。

interface Window{
    public void open();
    public void close();
    public void icon();
    public void unicon();
}
abstract class WindowAdapter implements Window{
    public void open(){};
    public void close(){};
    public void icon(){};
    public void unicon(){};
}
class MyWindow extends WindowAdapter{
    public void open(){
        System.out.println("打开窗口!!!");
    }
}
public class Demo{
    public static void main(String args[]){
        Window win = new MyWindow();
        win.open();
    }
}

3.14.2、工厂设计模式

现在有如下一道程序:

interface Fruit{
    public void eat();
}
class Apple implements Fruit{
    public void eat(){
        System.out.println("吃苹果。。。");
    }
}
class Orange implements Fruit{
    public void eat(){
        System.out.println("吃橘子。。。");
    }
}
public class Demo{
    public static void main(String args[]){
        Fruit f = new Apple();
        f.eat();
    }
}

以上的程序如果开发的话是否存在问题?

**注意:**主方法实际上就是一个客户端,客户端的代码越简单越好。

interface Fruit{
    public void eat();
}
class Apple implements Fruit{
    public void eat(){
        System.out.println("吃苹果。。。");
    }
}
class Orange implements Fruit{
    public void eat(){
        System.out.println("吃橘子。。。");
    }
}
class Factory{	//工厂类
    public static Fruit getFruit(String className){
        Fruit f = null;
        if("apple".equals(className)){
            f = new Apple();
        }
        if("orange".equals(className)){
            f = new Orange();
        }
        return f;
    }
}
public class Demo{
    public static void main(String args[]){
        Fruit f = Factory.getFruit(args[0]);
        if(f!=null){
          f.eat();  
        }
    }
}

所有接口的实例化对象都是通过工厂类取得的。那么客户端调用的时候根据传入的名称不同,完成的功能也不同。

 if("orange".equals(className)){
            f = new Orange();
        }

在以上的程序中将字符串写在前面,通过字符串的匿名对象调用方法。

3.14.3、代理设计模式

现在有如下一种情况,生活的情况

  • 例如:张某借给马某1000000000钱。规定一年后还
  • 一年之后,马某抵赖无钱还债
  • 张某找到了开讨债公司的范某,基本手法:刀子、手枪
  • 范某准备好了了小刀、绳索、钢筋、钢锯、手枪
  • 马某害怕了,之后还钱
  • 范某销毁证据。

张某–>范某–>讨债马某

interface Give{
    public void giveMoney();
}
class RealGive implements Give{
    @Override
    public void giveMoney() {
        System.out.println("把钱还给我。。。。。");
    }
}
class ProxyGive implements Give{
    private Give give = null;
    public ProxyGive(Give give){
        this.give = give;
    }
    public void before(){
        System.out.println("准备:小刀、绳索、钢锯、手枪");
    }
    public void alfter(){
        System.out.println("销毁所有罪证");
    }
    @Override
    public void giveMoney() {
        this.before();
        this.give.giveMoney();  //表示真正的讨债者完成讨债的操作
        this.alfter();
    }
}

public class Demo00 {
    public static void main(String args[]) {
        Give give = new ProxyGive(new RealGive());
        give.giveMoney();
    }
}

3.14.4、抽象类和接口的区别(绝对重点

抽象类和接口在使用上如此相似,那么该使用哪个呢?

比较点抽象类接口
组成抽象方法、普通方法、常量、变量、构造方法、全局常量抽象方法、全局常量
定义abstractinterface
子类子类通过extends继承抽象类子类通过implements实现接口
限制一个子类只能继承一个抽象类一个子类可以同时实现多个接口
关系一个抽象类可以实现多个接口一个接口不能继承一个抽象类只能实现多个接口
一个抽象类可以包含多个接口一个接口中可以包含多个抽象类
设计模式模板设计工厂设计、代理设计
与接口一起操作可以完成适配器设计与抽象类一起操作可以完成适配器设计
实例化都是通过对象的多态性,通过子类进行对象的实例化操作都是通过对象的多态性,通过子类进行对象的实例化操作
实现限制存在单继承局限不存在此类局限
特性-表示一个标准、一种能力

从以上的表格中可以发现,如果抽象类和接口都同时使用的话,优先使用接口,因为接口可以避免单继承的局限。

一个抽象类中允许包含一个接口

abstract class A{
    public abstract void fun();
    interface B{	//内部接口
        public void print();
    }
}
class X extends A{
    public void fun(){
        System.out.println("*************************");
    }
    class Y implements B{
        public void print(){
            System.out.println("============================");
        }
    }
}
public class Demo{
    public static void main(String args[]){
        A a = new X();
        a.fun();
        A.B b = new X().new Y();
        b.print();
    }
}

反之,道理一样

interface A{
    public void fun();
    abstract class B{	//内部抽象类
        public abstract void print();
    }
}
class X implements A{
    public void fun(){
        System.out.println("*************************");
    }
    class Y extends B{
        public void print(){
            System.out.println("============================");
        }
    }
}
public class Demo{
    public static void main(String args[]){
        A a = new X();
        a.fun();
        A.B b = new X().new Y();
        b.print();
    }
}

转载请注明来源