首页 > 其他 > 详细

接口,接口的特性,接口实现多态,面向接口编程

时间:2019-04-25 21:43:40      阅读:115      评论:0      收藏:0      [点我收藏+]
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既是实现类也是子类 */ } }


技术分享图片

 

接口,接口的特性,接口实现多态,面向接口编程

原文:https://www.cnblogs.com/406070989senlin/p/10770900.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!