java的接口的应用

java接口

一接口

1关键词interface进行修饰的一个类,叫接口。

eg:public interface myinterface(){

public void foo();

}

2接口中所有的方法都是public abstract.只有声明,没有实现,可以省略public abstract.只有声明,没有方法体。

3接口中属性是属于静态(Static)常量(final)

eg:public static  final String usb="2000";

注:public static  final可以省略。

3接口不可以被实例化

4用Static 修饰的常量,可以用接口名点来调用。

5无构造方法。

二如何实现接口

1用关键词implements来执行

2将接口中所有的方法都要重写

3可以实现多个接口,用逗号分开,接口中的方法要全部重写

eg:public class UsbInterfaceIm    implements Usb,usb1{}

三如何使用接口,用多态来实现

父类的引用指向子类的对象

接口的引用指向实现类的对象

四接口表现一种能力

接口是一种能力,体现在接口的方法上

程序设计时,关心实现类有何能力,而不关心实现的具体细节。也就是面向接口的月约定而不考虑接口的具体实现。

五判断接口

例如:防盗门是一个门    是is-a的关系

      防盗门有一个锁   是 has-a的关系

       防盗门有开锁和上锁的能力

六接口有比抽象类更好的特性

1可以被多继承

2设计与实现完全分离

3更自然的使用多态

4更容易搭建程序框架

5更容易更换实现

例题用接口实现编写手机的功能,这是类图

代码:

父类手机:

public class Handset {

    

    private  String Brand;

    

    private  String type;

    

   

    public String getBrand() {

        return Brand;

    }


    public void setBrand(String brand) {

        Brand = brand;

    }


    public String getType() {

        return type;

    }



    public void setType(String type) {

        this.type = type;

    }


    public void sendInfo(){

        System.out.println("发信息");       

    }

    public void call(){

        System.out.println("打电话");       

    }

    public void Info(){

        System.out.println("接受信息");       

    }

}

播放的接口

public interface Playwiring {

      public void play(String n);

}

子类普通手机继承父类和实现接口

public class CommonHandset extends Handset implements  Playwiring{


    @Override

    public void play(String n) {

        setBrand("诺基亚");

        setType("520型号");

        String n1=getBrand();

        String n2=getType();

        System.out.print(n1+n2+"播放"+n);

    }

    public void sendInfo(){

        setBrand("诺基亚手机");

        setType("520型号");

        String n1=getBrand();

        String n2=getType();

        System.out.println(n1+n2+"发信息");       

       }

    public void call(){

        System.out.println(getBrand()+getType()+"打电话");       

       }

    public void Info(){

        System.out.println(getBrand()+getType()+"接受信息");       

       }


}

链接网络和拍照的接口

public interface Network {

   public void networkConn();

}

public interface TheakePictures {

     public void takePicture();

}

子类智能手机继承父类和实现接口

public class AptitudeHandset extends Handset implements TheakePictures,Network,Playwiring{


    @Override

    public void play(String n) {

        setBrand("华为荣耀");

        setType("6x");

        String s=getBrand();

        String s1=getType();

        System.out.println(s+s1+"播放"+n);        

    }


    @Override

    public void networkConn() {

        setBrand("华为荣耀");

        setType("6x");

        String s=getBrand();

        String s1=getType();

        System.out.print(s+s1+"链接网络");

        

    }


    @Override

    public void takePicture() {

        setBrand("华为荣耀");

        setType("6x");

        String s=getBrand();

        String s1=getType();

        System.out.println(s+s1+"拍照功能");        

    }

    public void sendInfo(){

        setBrand("华为荣耀");

        setType("6x");

        String s=getBrand();

        String s1=getType();

        System.out.println(s+s1+"发信息");       

       }

    public void call(){

        setBrand("华为荣耀");

        setType("6x");

        String s=getBrand();

        String s1=getType();

        System.out.println(s+s1+"打电话");       

       }

    public void Info(){

        

            setBrand("华为荣耀");

            setType("6x");

            String s=getBrand();

            String s1=getType();

        System.out.println(s+s1+"接受信息");       

       }

    

}

测试类调用普通手机和只能手机的方法,实现该对应的功能

public class Test {

   public static void main(String[] args) {

      Playwiring phone=new CommonHandset();

      phone.play("音频");

      System.out.println(" ");

      Handset p=new CommonHandset();

      p.sendInfo();

      p.call();

      p.Info();

      

      Playwiring phone1 = new  AptitudeHandset();

      phone1.play("视频");

      TheakePictures phone2 =new  AptitudeHandset();

      phone2.takePicture();

      Network phone3=new AptitudeHandset();

      phone3. networkConn();

      

      Handset u=new AptitudeHandset();

      u.sendInfo();

      u.call();

      u.Info();

      

 }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值