面向对象进阶-接口

发布时间:2024年01月22日

接口:就是一种规则,是对行为的抽象。

接口的定义和使用

  • 接口用关键字interface来定义 public interface接口名{}

  • 接口不能被实例化

  • 接口和类之间是实现关系,通过implements关键字表示

  • 接口的子类(实现类):要么重写接口中所有的抽象方法要么是抽象类

    注意:

    1.接口和类的实现关系,可以是单实现也可以是多实现。

    2.实现类还可以在继承一个类的同时实现多个接口。

    package oop.a01interface;
    
    public abstract class Animal {
        private String name;
        private int age;
    
        public Animal() {
        }
    
        public Animal(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
        public abstract void  eat();
    }
    package oop.a01interface;
    
    public interface Swim {
        public abstract void swim();
    }
    package oop.a01interface;
    
    public class Dog extends Animal implements Swim{
        public Dog() {
        }
    
        public Dog(String name, int age) {
            super(name, age);
        }
    
        @Override
        public void eat() {
            System.out.println("狗再吃骨头");
        }
    
        @Override
        public void swim() {
            System.out.println("狗在狗刨");
        }
    }
    package oop.a01interface;
    
    public class Fog extends Animal implements Swim{
        public Fog() {
        }
    
        public Fog(String name, int age) {
            super(name, age);
        }
    
        @Override
        public void eat() {
            System.out.println("青蛙吃虫子");
        }
    
        @Override
        public void swim() {
            System.out.println("青蛙在蛙泳");
        }
    }
    package oop.a01interface;
    
    public class Rabbit extends Animal{
        public Rabbit() {
        }
    
        public Rabbit(String name, int age) {
            super(name, age);
        }
    
        @Override
        public void eat() {
            System.out.println("吃胡萝卜");
        }
    }
    package oop.a01interface;
    
    public class Test {
        public static void main(String[] args) {
            Dog d =new Dog("大黄",12);
            System.out.println(d.getName()+","+d.getAge());
            d.eat();
            d.swim();
            Rabbit r=new Rabbit("小白",2);
            System.out.println(r.getName()+","+r.getAge());
            r.eat();
        }
    }
    

    接口中成员的特点

  • 成员变量:只能是常量,默认修饰符public static final

  • 构造方法:没有

  • 成员方法:只能是抽象方法,默认修饰符 public abstract

  • JDK7以前:接口中只能定义抽象方法

  • JDK8的新特性:接口中可以定义有方法体的方法

  • JDK9的新特性:接口中可以定义私有方法

    package oop.a02interface;
    
    public interface Inter {
        int a=10;
        void method();
    }
    package oop.a02interface;
    
    public class InterImpl implements Inter{
        @Override
        public void method() {
            System.out.println("method");
        }
    }
    package oop.a02interface;
    
    import java.util.Scanner;
    
    public class Test {
        public static void main(String[] args) {
            System.out.println(Inter.a);
            InterImpl ii=new InterImpl();
            ii.method();
            Scanner sc=new Scanner(System.in);
        sc.next();
        }
    }
    

    接口和类之间的关系

  • 类和类之间的关系:继承关系,只能单继承,不能多继承,但可以多层继承

  • 类和接口之间的关系:实现关系,可以单实现也可以多实现,还可以在继承一个类的同时实现多个接口

    package oop.a03interface;
    
    public interface Inter1 {
        public abstract void method1();
        public abstract void method2();
        public abstract void method3();
    }
    package oop.a03interface;
    
    public interface Inter2 {
        public abstract void method4();
        public abstract void method5();
        public abstract void method6();
    }
    package oop.a03interface;
    
    public class InterImpl implements Inter1,Inter2{
        @Override
        public void method1() {
    
        }
    
        @Override
        public void method2() {
    
        }
    
        @Override
        public void method3() {
    
        }
    
        @Override
        public void method4() {
    
        }
    
        @Override
        public void method5() {
    
        }
    
        @Override
        public void method6() {
    
        }
    }

    接口和接口之间的关系:继承关系,可以单继承也可以多继承

    package oop.a04interface;
    
    public interface Inter1 {
        public abstract void method1();
    }
    package oop.a04interface;
    
    public interface Inter2 {
        public abstract void method2();
    }
    package oop.a04interface;
    
    public interface Inter3 extends Inter1,Inter2{
        public abstract void method3();
    }package oop.a04interface;
    
    public class InterImpl implements Inter3{
    
        @Override
        public void method1() {
    
        }
    
        @Override
        public void method2() {
    
        }
    
        @Override
        public void method3() {
    
        }
    }package oop.a04interface;
    
    public class Test {
        public static void main(String[] args) {
          //接口和接口之间是继承关系,可以是单继承也可以是多继承
          //细节:如果实现类是实现了最下面的子接口,那么就需要重写所有的抽象方法
    
        }
    }
    package oop.a05interface;
    
    public abstract class Person {
        private String name;
        private int age;
    
        public Person() {
        }
    
        public Person(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    }
    package oop.a05interface;
    
    public abstract class Coach extends Person{
        public Coach() {
        }
    
        public Coach(String name, int age) {
            super(name, age);
        }
        public abstract void teach();
    }
    public abstract class Sporter extends Person{
    
        public Sporter() {
        }
    
        public Sporter(String name, int age) {
            super(name, age);
        }
    
        public abstract void study();
    }
    package oop.a05interface;
    
    public interface English {
        public abstract void speakEnglish();
    }
    package oop.a05interface;
    
    public class BasketBallCoach extends Coach{
        public BasketBallCoach() {
        }
    
        public BasketBallCoach(String name, int age) {
            super(name, age);
        }
    
        @Override
        public void teach() {
            System.out.println("篮球教练在教运动员打篮球");
        }
    }
    package oop.a05interface;
    
    public class BasketBallSporter extends Sporter{
    
        public BasketBallSporter() {
        }
    
        public BasketBallSporter(String name, int age) {
            super(name, age);
        }
    
        @Override
        public void study() {
            System.out.println("篮球运动员在学习打篮球");
        }
    }
    package oop.a05interface;
    
    public class PingPangCoach extends Coach implements English{
    
        public PingPangCoach() {
        }
    
        public PingPangCoach(String name, int age) {
            super(name, age);
        }
    
        @Override
        public void speakEnglish() {
            System.out.println("乒乓球教练在说英语");
        }
    
        @Override
        public void teach() {
            System.out.println("乒乓球教练在教运动员打乒乓球");
        }
    }
    package oop.a05interface;
    
    public class PingPangSporter extends Sporter implements English {
        @Override
        public void speakEnglish() {
            System.out.println("乒乓球运动员在说英语");
        }
    
        @Override
        public void study() {
            System.out.println("乒乓球运动员在学习打乒乓球");
        }
    
        public PingPangSporter() {
        }
    
        public PingPangSporter(String name, int age) {
            super(name, age);
        }
    }
    package oop.a05interface;
    
    public class Test {
        public static void main(String[] args) {
            BasketBallSporter bs=new BasketBallSporter("张三",14);
            System.out.println(bs.getName()+","+bs.getAge());
            bs.study();
            PingPangCoach pc=new PingPangCoach("karry",24);
            System.out.println(pc.getName()+","+pc.getAge());
            pc.teach();
            pc.speakEnglish();
        }
    }

文章来源:https://blog.csdn.net/qq_52326978/article/details/135735703
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。