• 面向对象编程(七)接口,面向接口编程


    接口

    接口语法

    interface 接口名{

               //静态常量、抽象方法

    }

     

    接口的特点

    ①   接口中只能存放静态常量和抽象方法;

    ②   Java接口是对功能的扩展;

    ③   通过实现接口,java类可以实现多实现;

    ④   一个人可以同时继承(extends)一个父类并且实现(implements)多个接口;

    ⑤   接口与接口之间可以通过使用extends来产生继承关系;

     

    接口与抽象类的区别

    ①   抽象类和具体实现类之间是一种继承关系,也就是说如果采用抽象类的方式,则父类和子类在概念上应该是相同的;

    ②   接口和实现类在概念上不要求相同,接口只是抽取互相之间没有关系的类的共同特征,而不去关注类之间的关系,它可以使没有层次关系的类具有相同的行为;

    ③   抽象类是对一组具有相同属性和行为的逻辑上有关系的事物的一种抽象,而接口则是对一组具有相同属性和行为的逻辑上不相关的事物的一种抽象;

    ④   对于接口和抽象类的选择,反映出设计人员看待问题的不同角度。抽象类用于一组相关的事物,表示的事“is-a”的关系;而接口用于一组不相关的事物,表示的是“like-a”的关系;

    不使用接口code

     1 public class InterfaceDemo{
     2     public static void main(String []args){
     3         Person p=new Person();
     4         Child child=new Child();
     5         Dog dog=new Dog();
     6         p.feed(child);
     7         p.feed(dog);
     8     }
     9 }
    10 
    11 class Person{
    12     public void feed(Child child){
    13         child.eat();
    14     }
    15     public void feed(Dog dog){
    16         dog.eat();
    17     }
    18     
    19 }
    20 
    21 interface IAbility{
    22     //接口中只能放共有的静态常量和抽象方法
    23     //接口中会默认public static final
    24     /*public static final*/ /*int number=1;*/
    25     
    26     //方法中会默认加上public abstract
    27     /*public abstract*/ /*void show();*/
    28 }
    29 
    30 class Child{
    31     public void eat(){
    32         System.out.println("吃米饭");
    33     }
    34 }
    35 
    36 class Dog{
    37     public void eat(){
    38         System.out.println("啃骨头");
    39     }
    40 }

    使用接口code

     1 public class InterfaceDemo{
     2     public static void main(String []args){
     3         Person p=new Person();
     4         Child child=new Child();
     5         Dog dog=new Dog();
     6         p.feed(child);
     7         p.feed(dog);
     8     }
     9 }
    10 
    11 class Person{
    12     /*public void feed(Child child){
    13         child.eat();
    14     }
    15     public void feed(Dog dog){
    16         dog.eat();
    17     }*/
    18     //接口的引用变量可以引用其实现类的对象
    19     //接口实现了多态
    20     public void feed(IAbility ability){
    21         ability.eat();//动态绑定
    22     }
    23     
    24 }
    25 
    26 interface IAbility{
    27     //接口中只能放共有的静态常量和抽象方法
    28     //接口中会默认public static final
    29     /*public static final*/ /*int number=1;*/
    30     
    31     //方法中会默认加上public abstract
    32     /*public abstract*/ /*void show();*/
    33     
    34     void eat();
    35 }
    36 
    37 class Child implements IAbility{
    38     public void eat(){
    39         System.out.println("吃米饭");
    40     }
    41 }
    42 
    43 class Dog implements IAbility{
    44     public void eat(){
    45         System.out.println("啃骨头");
    46     }
    47 }

     

    面向接口编程

    开发系统时,主体架构使用接口,接口构成系统的骨架;

    这样就可以通过更换接口的实现类更换系统的实现;

     

    示例:

    需求:学习内部安装了彩色打印机或黑白打印机,通过其所安装的打印机可以用来打印教员的详细信息,也可以打印学校其自身的详细信息;

    改写以前编写过的一个程序(打印机打印的程序)

     1 public class PrinterDemo{
     2     public static void main(String []args){
     3         ColorPrinter cp=new ColorPrinter("惠普");
     4         BlackPrinter bp=new BlackPrinter("戴尔");
     5         ZhenPrinter zp=new ZhenPrinter("戴尔");
     6         School school=new School();
     7         
     8         school.setPrinter(zp);//这里的参数可以调用较灵活,使用cp,bp,zp都可以,而不用改school类中的方法
     9         
    10         Teacher t=new Teacher("张三",30);
    11         school.print(t);
    12         school.print(school); 
    13     }
    14 }
    15 
    16 interface IInfo{
    17     String detail();
    18 }
    19 
    20 abstract class Printer{
    21     private String brand;
    22     
    23     public Printer(String brand){
    24         this.brand=brand;
    25     }
    26     
    27     public String getBrand()
    28     {
    29         return brand;
    30     }
    31     
    32     //打印方法应该由其子类来具体的实现
    33     public abstract void print(String content);
    34 }
    35 
    36 //开原原则:对修改是封闭的,对扩展是开放的,不要违反开闭原则
    37 //可以使用多态解决这个问题,父类的引用变量可以引用其子类的对象
    38 class School implements IInfo{
    39     private Printer p=null;//安装打印机
    40     
    41     //拿父类的引用变量作为参数,好处就是可以接受任何其子类的对象
    42     //越是抽象的东西就是越稳定的
    43     public void setPrinter(Printer p){
    44         this.p=p;
    45     }
    46     //多态,程序设计时主体框架使用接口或抽象类,是程序具有很好的维护性和扩展性
    47     public void print(IInfo info){
    48         //交给中心所安装的打印机来打印
    49         p.print(info.detail());
    50     }
    51     
    52     public String detail(){
    53         return "hello,我是学校";
    54     }
    55 }
    56 
    57 class ColorPrinter extends Printer{
    58     public ColorPrinter(String brand){
    59         super(brand);
    60     }
    61     //对父类的方法进行重写
    62     public void print(String content){
    63         System.out.println(getBrand()+"彩色打印:"+content);
    64     }
    65 }
    66 
    67 class BlackPrinter extends Printer{
    68     public BlackPrinter(String brand){
    69         super(brand);
    70     }
    71     //对父类的方法进行重写
    72     public void print(String content){
    73         System.out.println(getBrand()+"黑白打印:"+content);
    74     }
    75 }
    76 
    77 class ZhenPrinter extends Printer{
    78     public ZhenPrinter(String brand){
    79         super(brand);
    80     }
    81     //对父类的方法进行重写
    82     public void print(String content){
    83         System.out.println(getBrand()+"针式打印:"+content);
    84     }
    85 }
    86 
    87 class Teacher implements IInfo{
    88     private String name;
    89     private int age;
    90     public Teacher(String name,int age){
    91         this.name=name;
    92         this.age=age;
    93     }
    94     public String detail(){
    95         return "我的名字叫:"+name+",今年:"+age+"岁";
    96     }
    97 }

     

  • 相关阅读:
    return 与 exit() 的区别
    RtlInitUnicodeString
    计算机网络 学习笔记-概论
    STM32学习笔记(五) USART异步串行口输入输出(轮询模式)
    STM32学习笔记(四) RCC外设的学习和理解
    简单RTOS学习(一) uc/os-II 工程模板建立
    web前端学习(一) html+js实现文本框背景及只读属性修改
    TCP/IP协议学习(一) LWIP实现网络远程IAP下载更新
    STM32学习笔记(三) STM32的GPIO的深入学习
    STM32学习笔记(二) 基于STM32-GPIO的流水灯实现
  • 原文地址:https://www.cnblogs.com/wzy330782/p/5299348.html
Copyright © 2020-2023  润新知