跳至主要內容

接口和抽象类

晨光-向大约 7 分钟JavaJava语言

接口和抽象类

1. 抽象类

抽象类是一种抽象能力弱于接口的类,在Java中,抽象类使用abstract关键字来表示。如果把接口形容为狗这个物种,那么抽象类可以说是毛发是白色、小体的品种,而实现类可以是具体的类,比如:说是博美、泰迪等。

1.1 抽象类的概述

当我们在做子类共性功能抽取时,有些方法在父类中并没有具体的体现,这个时候就需要抽象类了!

在Java中,一个没有方法体的方法应该定义为抽象方法,而类中如果有抽象方法,该类必须定义为抽象类!

1.2 抽象类的特点

  • 抽象类和抽象方法必须使用abstract关键字修饰
//抽象类的定义
public abstract class 类名 {

    //抽象方法的定义
    public abstract void eat();
    
}
  • 抽象类中不一定有抽象方法,有抽象方法的类一定是抽象类

  • 抽象类不能实例化

抽象类如何实例化呢?参照多态的方式,通过子类对象实例化,这叫抽象类多态。

  • 抽象类的子类

要么重写抽象类中的所有抽象方法

要么是抽象类

1.3 抽象类的成员特点

1. 成员的特点

  • 成员变量

既可以是变量

也可以是常量

  • 构造方法

空参构造

有参构造

  • 成员方法

抽象方法

普通方法

2. 示例

动物类

public abstract class Animal {
    private int age = 20;
    private final String city = "北京";
    public Animal() {}
    public Animal(int age) {
        this.age = age;
    }
    public void show() {
        age = 40;
        System.out.println(age);
        // city = "上海";
        System.out.println(city);
    }
    public abstract void eat();
}

猫类

public class Cat extends Animal {
    @Override
    public void eat() {
        System.out.println("猫吃鱼");
    }
}

测试类

public class AnimalDemo {
    public static void main(String[] args) {
        Animal a = new Cat();
        a.eat();
        a.show();
    }
}

1.4 抽象类的案例

请采用抽象类的思想实现猫和狗的案例,并在测试类中进行测试

动物类

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();
}

猫类

public class Cat extends Animal {

    public Cat() {
    }

    public Cat(String name, int age) {
        super(name, age);
    }

    @Override
    public void eat() {
        System.out.println("猫吃鱼");
    }
}

狗类

public class Dog extends Animal {

    public Dog() {
    }

    public Dog(String name, int age) {
        super(name, age);
    }

    @Override
    public void eat() {
        System.out.println("狗吃骨头");
    }
}

测试类

package com.itheima_04;
/*
    测试类
 */
public class AnimalDemo {
    public static void main(String[] args) {
        //创建对象,按照多态的方式
        Animal a = new Cat();
        a.setName("加菲");
        a.setAge(5);
        System.out.println(a.getName()+","+a.getAge());
        a.eat();
        System.out.println("--------");

        a = new Cat("加菲",5);
        System.out.println(a.getName()+","+a.getAge());
        a.eat();
    }
}

2. 接口

接口相当于就是对外的一种约定和标准,这里拿操作系统举例子,为什么会有操作系统?就会为了屏蔽 软件的复杂性和硬件的简单性之间的差异,为软件提供统一的标准。

2.1 接口的概述

接口就是一种公共的规范标准,只要符合规范标准,大家都可以通用。

Java中的接口更多的体现在对行为的抽象!

2.2 接口的特点

  • 接口用关键字interface修饰
public interface 接口名 {}
  • 类实现接口用implements表示
public class 类名 implements 接口名 {}
  • 接口不能实例化

接口如何实例化呢?参照多态的方式,通过实现类对象实例化,这叫接口多态。

多态的形式:具体类多态,抽象类多态,接口多态。

  • 接口的子类

要么重写接口中的所有抽象方法

要么子类也是抽象类

2.3 接口的成员特点

1. 成员特点

  • 成员变量

只能是常量

默认修饰符:public static fifinal

  • 构造方法

没有,因为接口主要是扩展功能的,而没有具体存在

  • 成员方法

只能是抽象方法

默认修饰符:public abstract

关于接口中的方法,JDK8和JDK9中有一些新特性,后面再讲解

2. 示例

接口

public interface Inter {
    public int num = 10;
    public final int num2 = 20;
//    public static final int num3 = 30;
    int num3 = 30;

//    public Inter() {}

//    public void show() {}

    public abstract void method();
    void show();

}

实现类

package com.itheima_02;

//public class InterImpl implements Inter {

public class InterImpl extends Object implements Inter {
    public InterImpl() {
        super();
    }

    @Override
    public void method() {
        System.out.println("method");
    }

    @Override
    public void show() {
        System.out.println("show");
    }
}

测试类

package com.itheima_02;
/*
    测试类
 */
public class InterfaceDemo {
    public static void main(String[] args) {
        Inter i = new InterImpl();
//        i.num = 20;
        System.out.println(i.num);
//        i.num2 = 40;
        System.out.println(i.num2);
        System.out.println(Inter.num);
    }
}

2.4 接口的案例

对猫和狗进行训练,他们就可以跳高了,这里加入跳高功能。请采用抽象类和接口来实现猫狗案例,并在测试类中进行测试。

动物类

package com.itheima_03;

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();
}

跳高接口

public interface Jumpping {
    public abstract void jump();
}

猫类

public class Cat extends Animal implements Jumpping {

    public Cat() {
    }

    public Cat(String name, int age) {
        super(name, age);
    }

    @Override
    public void eat() {
        System.out.println("猫吃鱼");
    }

    @Override
    public void jump() {
        System.out.println("猫可以跳高了");
    }
}

测试类

package com.itheima_03;
/*
    测试类
 */
public class AnimalDemo {
    public static void main(String[] args) {
        //创建对象,调用方法
        Jumpping j = new Cat();
        j.jump();
        System.out.println("--------");

        Animal a = new Cat();
        a.setName("加菲");
        a.setAge(5);
        System.out.println(a.getName()+","+a.getAge());
        a.eat();
//        a.jump();

        a = new Cat("加菲",5);
        System.out.println(a.getName()+","+a.getAge());
        a.eat();
        System.out.println("--------");

        Cat c = new Cat();
        c.setName("加菲");
        c.setAge(5);
        System.out.println(c.getName()+","+c.getAge());
        c.eat();
        c.jump();
    }
}

2.5 类和接口的关系

  • 类与类的关系

继承关系,只能单继承,但是可以多层继承

  • 类与接口的关系

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

  • 接口与接口的关系

继承关系,可以单继承,也可以多继承

2.6 抽象类和接口的区别

1. 成员区别

  • 抽象类

变量,常量;有构造方法;有抽象方法,也有非抽象方法

  • 接口

常量;抽象方法

2. 关系区别

  • 类与类

继承,单继承

  • 类与接口

实现,可以单实现,也可以多实现

  • 接口与接口

继承,单继承,多继承

3. 设计理念区别

  • 抽象类

对类抽象,包括属性、行为

  • 接口

对行为抽象,主要是行为

3. 参数传递

3.1 类名作为形参和返回值

  • 1、类名作为方法的形参

    方法的形参是类名,其实需要的是该类的对象

    实际传递的是该对象的【地址值】

  • 2、类名作为方法的返回值

    方法的返回值是类名,其实返回的是该类的对象

    实际传递的,也是该对象的【地址值】

  • 示例代码:

    class Cat {
        public void eat() {
            System.out.println("猫吃鱼");
        }
    }
    class CatOperator {
        public void useCat(Cat c) { //Cat c = new Cat();
            c.eat();
        }
        public Cat getCat() {
            Cat c = new Cat();
            return c;
        }
    }
    public class CatDemo {
        public static void main(String[] args) {
            //创建操作类对象,并调用方法
            CatOperator co = new CatOperator();
            Cat c = new Cat();
            co.useCat(c);
    
            Cat c2 = co.getCat(); //new Cat()
            c2.eat();
        }
    }
    

3.2 抽象类作为形参和返回值

  • 抽象类作为形参和返回值

    • 方法的形参是抽象类名,其实需要的是该抽象类的子类对象
    • 方法的返回值是抽象类名,其实返回的是该抽象类的子类对象
  • 示例代码:

    abstract class Animal {
        public abstract void eat();
    }
    class Cat extends Animal {
        @Override
        public void eat() {
            System.out.println("猫吃鱼");
        }
    }
    class AnimalOperator {
        public void useAnimal(Animal a) { //Animal a = new Cat();
            a.eat();
        }
        public Animal getAnimal() {
            Animal a = new Cat();
            return a;
        }
    }
    public class AnimalDemo {
        public static void main(String[] args) {
            //创建操作类对象,并调用方法
            AnimalOperator ao = new AnimalOperator();
            Animal a = new Cat();
            ao.useAnimal(a);
    
            Animal a2 = ao.getAnimal(); //new Cat()
            a2.eat();
        }
    }
    

3.3 接口名作为形参和返回值

  • 接口作为形参和返回值

    • 方法的形参是接口名,其实需要的是该接口的实现类对象
    • 方法的返回值是接口名,其实返回的是该接口的实现类对象
  • 示例代码:

    interface Jumpping {
        void jump();
    }
    class JumppingOperator {
        public void useJumpping(Jumpping j) { //Jumpping j = new Cat();
            j.jump();
        }
        public Jumpping getJumpping() {
            Jumpping j = new Cat();
            return j;
        }
    }
    class Cat implements Jumpping {
        @Override
        public void jump() {
            System.out.println("猫可以跳高了");
        }
    }
    public class JumppingDemo {
        public static void main(String[] args) {
            //创建操作类对象,并调用方法
            JumppingOperator jo = new JumppingOperator();
            Jumpping j = new Cat();
            jo.useJumpping(j);
    
            Jumpping j2 = jo.getJumpping(); //new Cat()
            j2.jump();
        }
    }