java interface, the characteristics of the interface, the interface implements polymorphism, interface-oriented programming

package cn.zy.cellphone;
/**接口是一种引用数据类型。使用interface声明接口,形式
 * 形式:public interface 接口名称{}
 * 接口不能拥有构造方法,不能用于创建对象
 *接口可以多继承。一个接口可以继承多个其他接口
 *列如:public interface Broadcast extends Network,Picture{}
 *Broadcast接口就具备了A、B接口中定义的抽象方法。
 */
public interface Broadcast {
     /**接口中可以声明属性。接口中定义的所有变量都是static final类型的。
      * public static final String version = "1.0";
         public int count = 10; // 默认是static final类型
         一般很少在接口中声明属性。
      */

 
 
package cn.zy.cellphone;

public interface Picture {
public void picture();//照相
}

package cn.zy.cellphone;

public interface Network {
public void network();//网络
}

package cn.zy.cellphone;
/**一个类只能继承一个父类,同时实现多个接口。继承在前,实现在后。
 * public class Photo2 extends Photo implements Network,Picture,Broadcast {}
 * 实现类实现接口,必须实现接口中定义的抽象方法。
 *方法即行为,表示一种功能,接口定义了一套功能,实现类必须实现这些功能 –> 实现类的能力得到拓展。
 */
//实现类(子类)
public class Photo2 extends Photo implements Network,Picture,Broadcast {
            private String name;
          
            
            public String getName() {
                return name;
            }


            public void setName(String name) {
                this.name = name;
            }
             

            public Photo2(String brand, String type, String name) {
                super(brand, type);
                this.setName (name);
         } 
            /**接口中定义的一些系列方法表示的是一种种的能力。接口让实现类实现这些能力,实现类的能力得到拓展和升级。
                                         实现类根据自身特性实现接口中定义的方法。
             * 特殊情况:如果一个抽象父类定义了和接口同名的抽象方法,实现类实现的是抽象父类的抽象方法。
             */
            public void broadcast() {
                System.out.println(this.getName()+"的手机可以播放");
        }

            public void picture() {
                System.out.println(this.getName()+super.getBrand()+super.getType()+"的手机可以照相");
                
            }

            
            public void network() {
                
                System.out.println(this.getName()+super.getBrand()+super.getType()+"的手机可以播放上网");    
            }
            /**实现类(子类)Photo2 手机原本只有下面几个功能,通过接口增加了上面几个功能
             * 实现类实现接口,必须实现接口中定义的抽象方法。
               *方法即行为,表示一种功能,接口定义了一套功能,实现类必须实现这些功能 –> 实现类的能力得到拓展。
             */
            
            public void sendInfo() {
                System.out.println(super.getBrand()+super.getType()+"的手机发信息");            
                
            }

            
            public void call() {
                System.out.println(super.getBrand()+super.getType()+"的手机可以打电话");                
                
            }

            
            public void info() {
                System.out.println(super.getBrand()+super.getType()+"的手机可以发短信");
                
            }
}

package cn.zy.cellphone;
//实现类(子类)
public  class Photo1 extends Photo implements Broadcast {
       private String name;
    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public Photo1() {
    super();
}

    public Photo1(String brand, String type,String name) {
    super(brand,type);
    this.setName(name);
}


    public void broadcast() {
        System.out.println(this.getName()+"的手机可以播放");
}
    public  void sendInfo(){
        System.out.println(super.getBrand()+super.getType()+"的手机发信息");
    }
    public void call(){
        System.out.println(super.getBrand()+super.getType()+"的手机可以打电话");
        }
    public void info(){
        System.out.println(super.getBrand()+super.getType()+"的手机可以发短信");
    }

}

 
 
package cn.zy.cellphone;
//父类
public abstract class Photo {
     private String brand;
     private String type;
    public String getBrand() {
        return brand;
    }
    public void setBrand(String brand) {
        this.brand = brand;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public Photo() {
        super();
    }
    public Photo(String brand, String type) {
        super();
        this.brand = brand;
        this.type = type;
    }
     public abstract void sendInfo();
     public abstract void call();
     public abstract void info();
}
 
 

package cn.zy.cellphone;
//执行类
public class Test {
public static void main(String[] args) {
              // 同一引用类型
    Broadcast broadcast1=new Photo1("诺基亚","a01","张三");
            broadcast1.broadcast();
        
            Photo photo=new Photo1("诺基亚","a01","张三");
            photo.sendInfo();
            photo.call();
            photo.info();
            // 同一引用类型
            Broadcast broadcast2=new Photo1("苹果X","11S","李四"); 
            broadcast2.broadcast();
            // 实例不同, 对同一方法的执行结果不同
            Picture picture1=new Photo2("苹果X","11S","李四");
            picture1.picture();
            Network network1=new Photo2("苹果X","11S","李四");
            network1.network();
           //多态
            Photo photo1=new Photo1("苹果X","11S","李四");
            photo1.sendInfo();
            photo1.call();
            photo1.info();
            /**
             * 接口实现多态: 接口类型 引用 实现类对象
                                      继 承实现多态: 父类类型 引用 子类对象
                                     接口定义的方法被实现类实现,通过接口引用实现类时,调用接口中的方法时,执行的是实现类实现的方法。
                                       实现类对象具备接口中定义的能力 是一种has a 关系
                                           子类对象是一种父类类型 是一种is a 关系                       
                
                                         现在的Photo1同Photo2既是实现类也是子类
             */
}
}


 programming to interface

The interface represents a kind of agreement (protocol), and the agreement (specification) specifies that the implementation class should have (has a) capabilities.

understand:

=> The implementation class must implement all the methods in the interface, so the interface regulates the behavior of the implementation class

=> The interface agrees on the behavior that the implementing class should have.

programming to interface

The so-called interface-oriented programming, when programming, only cares about what capabilities the implementing class has, and does not care about how the implementing class realizes this ability. When programming to an interface, the contract is oriented to the interface without regard to the specific implementation of the interface.

That is to say, in interface-oriented programming, the definer of the interface only cares whether the implementation class has the capabilities defined by the interface, and the interface definer does not care at all about how the implementation class has the capabilities defined by the interface.

//接口实现多态 电脑类(通过接口把3种零件组成电脑,而且零件随时可以更换)(cpu接口  4Hz  5Hz  。。。。(随时更换大小不同的cpu)  )
                                                            (硬盘接口  3000GB  4000Gb(随时更换大小不同的硬盘))
(内存接口  30GB  40Gb(随时更换大小不同的内存))
package cn.computer;
//电脑
public class Computer {
  private Cpu cpu;                       //  预留cpu接口
  private Caliche caliche;                 // 预留硬盘接口
  private InternalStroage internalStroage;   //预留内存接口
public Cpu getCpu() {
    return cpu;
}
public void setCpu(Cpu cpu) {
    this.cpu = cpu;
}
public Caliche getCaliche() {
    return caliche;
}
public void setCaliche(Caliche caliche) {
    this.caliche = caliche;
}
public InternalStroage getInternalStroage() {
    return internalStroage;
}
public void setInternalStroage(InternalStroage internalStroage) {
    this.internalStroage = internalStroage;
}
public Computer() {
    super();
}
public Computer(Cpu cpu, Caliche caliche, InternalStroage internalStroage) {
    super();
    this.cpu = cpu;                         
    this.caliche = caliche;                    
    this.internalStroage = internalStroage;       
}
  //把各个部件通过接口组装成一个完整的电脑
public void print(String name) {
    System.out.println(name+"计算机");
    System.out.println("cpu:"+getCpu().Hz());
    System.out.println("硬盘容量:"+getCaliche().big2());
    System.out.println("内存容量:"+this.getInternalStroage().big());
}
    
}

 
 
 
 
package cn.computer;
//cpu接口
public interface Cpu {
    public String Hz();  //这是接口实现多态的形式:  public 你要返回的变量类型   方法名()
    
        
    }
 
 

package cn.computer;
//实现类
public class Realize1 implements Cpu{
  public String Hz(){             // public 你要返回的变量类型   方法名()
      return "3.8GHz";               //返回值
  }
}

 
 
package cn.computer;
//内存接口
public interface InternalStroage {
  public String big();
}
 
 

package cn.computer;
//实现类
public class Realize2 implements InternalStroage{
    public String big(){
        return "4GB";

    }

}

 
 
 
 
package cn.computer;
//硬盘接口
public interface Caliche {
    public String big2();
}
 
 

package cn.computer;
//实现类
public class Realize2 implements InternalStroage{
    public String big(){
        return "4GB";

    }

}
 
 

package cn.computer;

public class Test {
public static void main(String[] args) {
    Cpu cpu=new Realize1();     //把一种3.8Hz的cpu赋予给cpu接口
    Caliche caliche=new Realize3();   //把3000GB的硬盘赋予给硬盘接口
    InternalStroage internalStroage=new Realize2();     //把4GB的内存赋予给内存接口
    Computer computer=new Computer(cpu,caliche,internalStroage);    //把cpu,硬盘,内存给电脑类组合(地址)
     computer.print("华硕");                 //最后得到了完整的电脑
     }
}


1.1.1.1  Similarities and differences between abstract classes and interfaces

  • Both abstract classes and interfaces are reference data types, neither of which can create objects.
  • Both of them can define abstract methods, and both can achieve polymorphism. But abstract classes can define non-abstract methods, while interfaces define abstract methods.
  • Both abstract classes and interfaces are transitive. Abstract classes are single root (single inheritance), while interfaces are multiple inheritance.
  • Conceptually, abstract methods can be overridden. The subclass overrides the abstract class, and the implementation class implements the interface
  • Abstract classes and subclasses solve problems within modules (code reuse, rewriting, polymorphism), while interfaces solve problems between modules => high cohesion  , low coupling. Interfaces can be decoupled with modules.

Guess you like

Origin blog.csdn.net/Noah_ZX/article/details/130223699