接口interface

基本介绍

接口就是一些没有实现的方法,封装到一起,到某个类要使用时,再根据具体情况把这些方法写出来

语法:interface 接口{  属性;方法;必须实现的接口的抽象方法}

注意:jdk7.0以前接口中的所有方法都没有方法体(都是抽象方法)。jdk8.0后接口类可以有静态方法,默认方法,即接口中可以有方法的具体实现(需要default关键字修饰)。

在接口中抽象方法可以省略abstract关键字

TestInterface.java
public class TestInterface {
    public static void main(String[] args) {
        
    }
}
class A implements Interface{
    @Override
    public void say() {
        System.out.println("hello");
    }
}
Interface.java
public interface Interface {
    // 属性
    public int age=18;
    // 方法
    public void say();
    // default修饰。
    default public void run(){
        System.out.println("running");
    }
    // static修饰
    public static void eat(){
        System.out.println("eating");
    }
}
  1. 接口不能实例化
  2. 接口中所有方法是public方法,接口中的抽象方法可以不用abstract修饰,
  3. 普通类实现接口,必须把接口中所有方法都实现
  4. 光标放在 class C implements B 按【alt】+【Enter】(回车)快速实现
  5. 抽象类实现接口时可以不实现接口的抽象方法。
Interface01.java
 public interface Interface01 {
    public static void main(String[] args) {
        System.out.println("这是一个接口的规范");
    }
}
interface B{
    void sayHi();
    void  How();

}
//普通类必须实现接口中所有的方法
class C implements B{   // 光标放在 class C implements B 按【alt】+【Enter】(回车)快速实现
    @Override
    public void sayHi() {

    }

    @Override
    public void How() {

    }

}
//class D implements B{}
abstract class D implements B{
    // 可以不实现接口中的方法
} 
  1.  一个类可以同时实现多个接口
  2. 接口中的属性只能是final,而且是public static final修饰符(int a=1;实际上是public static final int a=1且必须初始化)
  3.  接口中属性的访问形式:接口名.属性名
  4. 接口不能继承其他类,但是可以继承多个接口
  5. 接口的修饰符只有public和默认
Interface01.java
public interface Interface01 {
    public static void main(String[] args) {
        System.out.println(A.num);//静态属性的功能
        //A.num=13; 不能修改,final修饰符的功能
    }
}
interface A{
    int num=12;
    void say();
    void hi();
}
interface B{
    void run();
    
}
class Person implements A,B {
    @Override
    public void say() {
        
    }

    @Override
    public void hi() {
        
    }
    
    public void run() {
        
    }
}
//class C extends B,A{}接口不能继承类,可以继承多个其他接口
interface C extends B,A{}

实现接口与继承类

继承主要功能在于解决了代码的复用性和可维护性。

接口主要功能在于设计(规范)好各种方法,让其它类去实现这些方法。

Java提供的单继承机制是对单继承的一种补充。

  1. 当子类继承了父类,就自动拥有了父类的功能
  2. 如果子类需要扩展功能,可以通过实现接口的方式扩展更加灵活
  3. 接口在一定程度上可以实现代码解耦,即:接口规范化+动态绑定机制。
ExtendsAndInterface.java
 public class ExtendsAndInterface {
    public static void main(String[] args) {
        AMonkey Wukong = new AMonkey("悟空");
        Wukong.climbing();
        Wukong.flying();
        Wukong.swimming();
    }
}
// 继承
class Monkey{
    private String name;

    public Monkey(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void climbing(){
        System.out.println(name+"会爬树。。");
    }
}
//class AMonkey extends Monkey{
class AMonkey extends Monkey implements Fishable,Birdable{// 接口多继承
    public AMonkey(String name) {
        super(name);
    }
    //实现从接口获得的能力
    public void swimming(){
        System.out.println(getName()+"通过继承接口Fishable,获得了游泳能力");
    }

    @Override
    public void flying() {
        System.out.println(getName()+"通过继承接口Birdable,获得了飞行能力");
    }
}
// 接口(学习能力)
interface Fishable{
    void swimming();
}
interface Birdable{
    void flying();
}

接口的多态性质

多态参数

既可以接受camera对象也可以接收phone对象;体现接口多态。

USB.java
public interface USB {
    public void start();
    public void stop();
}
Phone.java
 public class Phone implements USB{

    @Override
    public void start() {
        System.out.println("手机开始工作");
    }

    @Override
    public void stop() {
        System.out.println("手机停止工作");
    }
}
Camera.java
 public class Camera implements USB{

    @Override
    public void start() {
        System.out.println("相机开始工作");
    }

    @Override
    public void stop() {
        System.out.println("相机停止了工作");
    }
}
Computer.java
 public class Computer {
    public void work(USB usb){
        usb.start();
        usb.stop();
    }
}
TestUsb.java
 public class TestUsb {
    public static void main(String[] args) {
        Phone phone = new Phone();
        Camera camera = new Camera();
        Computer computer = new Computer();
       computer.work(camera);
        computer.work(phone);
    }
}

接口多态体现

接口类型的变量a可以指向实现了接口A的对象实例

Test.java
 public class Test {
    public static void main(String[] args) {
        A a = new B();
        a = new C();
    }

    interface A {
    }

   static class B implements A {
    }

    static class C implements A {
    }
}

 多态数组

接口类型数组

Test.java
 public class Test {
    public static void main(String[] args) {
        A[] a = new A[2];
        a[0]=new B();
        a[1]=new C();
        for (int i=0;i< a.length;i++){
            a[i].doing();
            // a[i].say(); 需要进行类型转换
            if (a[i] instanceof B){
                ((B)a[i]).say();
            }
        }
    }

    interface A {
        void  doing();
    }

   static class B implements A {
        public void say(){
            System.out.println("这是一个实现了接口A的B类");
        }

       @Override
       public void doing() {
           System.out.println("B类正在实现中");
       }
   }

    static class C implements A {
        public void run(){
            System.out.println("C类的run方法");
        }
        @Override
        public void doing() {
            System.out.println("C类正在实现中");
        }
    }
}

 多态传递

DemoPolyPass.java
 public class DemoPolyPass {
    public static void main(String[] args) {
        Inter02 i2=new Person();
        // Inter01 i1=new Person(); Inter02未继承Inter01时无法使用
        // 如果Inter02继承了Inter01,而Person类实现了Inter02,实际上Person类也实现了Inter01接口
        Inter01 i1=new Person();
        
    }
}
interface Inter01{}
interface Inter02 extends Inter01{}
class Person implements Inter02{}

 属性冲突

Demo.java
 public class Demo {
    public static void main(String[] args) {
        new C().show();
    }
}
interface A{
    int x=1;
}
class B{
    int x=2;
}
class C extends B implements A{
    public void show(){
        // System.out.println(x);
        // 不明确x是指的是哪一个
        // 访问接口的x用A.x
        System.out.println("接口的x="+A.x);
        // 访问父类的x用super.x
        System.out.println("父类的x="+super.x);
    }
}

 

posted @ 2024-04-03 23:36  Dr丶云幕  阅读(2)  评论(0编辑  收藏  举报