面向对象之抽象_接口_多态

一.抽象

1.抽象的介绍abstract

​ 1.将多个类中共有的方法抽取到父类,发现方法体没法实现,所以将此方法定义成抽象方法

​ 2.抽象方法所在的类一定是抽象类

​ 3.抽象类

public abstract class 类名{}

​ 4.抽象方法

修饰符 abstract 返回值类型 方法名(参数);

​ 5.注意:子类继承父类之后必须要重写父类中的所有抽象方法

public abstract class Animal {
    private String brand;

    public Animal() {
    }

    public Animal(String brand) {
        this.brand = brand;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    //抽象方法
    public abstract void eat();
    public abstract void drink();
    public abstract void sleep();
}
public class Dog extends Animal{

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

    @Override
    public void drink() {
        System.out.println("狗喝水");
    }

    @Override
    public void sleep() {
        System.out.println("狗睡觉");
    }
}

public class Test01 {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.setBrand("哈士奇");//继承的
        String brand = dog.getBrand();//继承的
        System.out.println("brand = " + brand);
        System.out.println("====================");
        dog.eat();//重写的
        dog.drink();//重写的
        dog.sleep();//重写的
    }
}

​ 6.抽象类不能直接new对象,只能创建非抽象子类的对象

​ 7.抽象类中,可以有构造方法,是供子类创建对象时,初始化父类属性使用的

​ 8.抽象类中可以有成员变量,构造,成员方法

​ 9.抽象类中不一定非得有抽象方法,但是有抽象方法的类一定是抽象类

​ 10.抽象类的子类,必须重写父类中的所有抽象方法,否则,编译无法通过,除非该子类也是抽象类

public abstract class Employee {
    private String name;
    private int age;

    public Employee() {
    }

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

public class Teacher extends Employee{
    public Teacher() {
    }

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

    @Override
    public void work() {
        System.out.println("讲师在讲课");
    }
}
public class Test {
    public static void main(String[] args) {
        Teacher t1 = new Teacher("张三", 23);
        t1.work();
    }
}

二.接口

1.基本介绍

1.概述:一种标准一种规范

2.关键字:interface

3.实现:implements

4.成员:

jdk7:
a.成员变量:public static final修饰的不写也有
b.抽象方法:public abstract 返回值类型 方法名(->不写abstract默认也有

jdk8:
默认方法:
public default 返回值类型 方法名(参数){
方法体
return 结果

}
静态方法:
public static 返回值类型 方法名(参数){
方法体
return 结果

}

jdk9:
私有方法:
带private的方法

5.使用:

public interface 接口名{

}
public class 类名 implements 接口名

6.注意

在实现类中重写接口的所有抽象方法

创建实现类对象(接口不能直接new对象),调用重写的方法

public interface USB {
    public abstract void open();
    public abstract void close();
}
public class Mouse implements USB{
    @Override
    public void open() {
        System.out.println("鼠标开启");
    }

    @Override
    public void close() {
        System.out.println("鼠标关闭");
    }
}

public class Test01 {
    public static void main(String[] args) {
        Mouse mouse = new Mouse();
        mouse.open();
        mouse.close();
    }
}

2.接口中的成员

2.1抽象方法

1.格式

public abstract 返回值类型 方法名();

2.注意:

即使没有abstract,默认也有

3.使用

​ a.创建实现类实现接口

​ b.在实现类里重写抽象方法

​ c.创建实现类对象

​ d.调用重写方法

2.2默认方法

1.格式:

public defalut 返回值类型 方法名(){
			方法体
            return 结果
}

2.注意:只能在接口中定义

3.使用:

​ a.定义实现类,实现接口

​ b.在实现类中,可重写默认方法,也可不重写

​ c.创建实现类对象,调用默认方法(有重写调重写)

public interface USB {
    //默认方法
    public default void methodDef(){
        System.out.println("我是接口中的默认方法");
    }
}
public class Mouse implements USB{
    @Override
    public void methodDef(){
        System.out.println("我是重写的接口中的默认方法");
    }
}
public class Test01 {
    public static void main(String[] args) {
        Mouse mouse = new Mouse();
        mouse.methodDef();
    }
}

2.3静态方法

1.格式

publuc static 返回值类型 方法名(参数){
		方法体
		return 结果
}
public interface USB {
   
    //静态方法
    public static void methodSta(){
        System.out.println("我是接口中的静态方法");
    }
}
public class Test01 {
    public static void main(String[] args) {
        USB.methodSta();
    }
}

2.4成员变量

1.格式

public static final 数据类型 变量名 =

2.注意:
a.不写static final 默认也有
b.被static final修饰的变量名一般都是大写

public interface USB {
    /*
      final代表最终的
      被final修饰之后不能被二次赋值
     */
    public static final int NUM = 100;
    int NUM1 = 10;
}
public class Test01 {
    public static void main(String[] args) {
        System.out.println(USB.NUM);
        System.out.println(USB.NUM1);
    }
}

3.接口的特点

1.接口可以多实现:

 public class InterfaceImpl implements InterfaceA,InterfaceB{}

2.接口可以多继承

 public Interface InterfaceA extends InterfaceB,InterfaceC{}

3.一个类可以继承一个父类的同时实现一个或者多个接口

  public class InterfaceImpl extends Fu implements InterfaceB,InterfaceC{}

当一个类实现多个接口时,如果接口中的抽象方法有重名且参数一样的,只需要重写一次

public interface InterfaceB {
    public abstract void methodAbs();
}
public interface InterfaceC {
    public abstract void methodAbs();
}

public class InterfaceImpl implements InterfaceB,InterfaceC{
    @Override
    public void methodAbs() {
        System.out.println("我是重写的methodAbs");
    }
}

当一个类实现多个接口时,如果默认方法有重名的,参数一样,要重写一次

public interface InterfaceB {
    public abstract void methodAbs();

    public default void methodDef(){
        System.out.println("我是接口B中的methodDef方法");
 }
}
public interface InterfaceC {
 public abstract void methodAbs();

public default void methodDef(){
     System.out.println("我是接口C中的methodDef方法");
 }
}
public class InterfaceImpl implements InterfaceB,InterfaceC{
 @Override
 public void methodAbs() {
    System.out.println("我是重写的methodAbs");
 }

    @Override
 public void methodDef() {
        System.out.println("我是重写的默认方法");
    }

}

4.接口和抽象类区别

1.相同点:
a.都位于继承的顶端,用于被其他类实现或者继承
b.都不能new
c.都包含抽象方法,其子类都必须重写这些抽象方法

2.不同点:
a.抽象类:一般作为父类使用,可以有成员变量,构造,成员方法,抽象方法等
b.接口:成员单一,一般抽取接口,抽取的都是方法,是功能的大集合
c.类不能多继承,接口可以

三.多态

1.前提

1.必须有子父类继承关系或者接口实现关系

2.必须有方法重写(没有方法的重写多态没有任何意义)

3.父类引用指向子类对象:

父类类型 对象名 = new 子类对象()

类似于:double b = 10

2.多态的基本使用

1.定义一个父类或者接口

2.定义一个子类或者实现

3.重写抽象方法

4.父类引用指向子类对象

5.调用重写的方法

public class Animal {
    public void eat(){
        System.out.println("动物要吃饭");
    }
}
public class Dog extends Animal{
    public void eat(){
        System.out.println("狗啃骨头");
    }

    //特有方法
    public void lookDoor(){
        System.out.println("狗会看门");
    }
}

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

    //特有方法
    public void catchMouse(){
        System.out.println("猫会抓老鼠");
    }
}
public class Test01 {
    public static void main(String[] args) {
        //原始方式new对象
        Dog dog = new Dog();
        dog.eat();
        dog.lookDoor();
        System.out.println("==========");
        Cat cat = new Cat();
        cat.eat();
        cat.catchMouse();

        System.out.println("==========");

        //多态形式new对象
        Animal animal = new Dog();
        animal.eat();
        //animal.lookDoor();//多态前提下,不能直接调用子类特有功能
        System.out.println("==========");

        Animal animal1 = new Cat();
        animal1.eat();
        //animal1.catchMouse();//多态前提下,不能直接调用子类特有功能
    }
}

3.多态的条件下成员的访问特点

3.1成员变量

看等号左边是谁,先调用谁中的成员变量,子类没有,找父类

public class Fu {
    int num = 100;
}
public class Zi extends Fu{
    int num = 10;
}

public class Test {
    public static void main(String[] args) {
        Fu fu = new Zi();
        System.out.println(fu.num);//父类的100
        
        System.out.println("============");

        Zi zi = new Zi();
        System.out.println(zi.num);//子类的10
    }
}

3.2成员方法

看new的是谁,先调用谁中的成员方法,子类没有,找父类

public class Fu {
    int num = 100;
    public void method(){
        System.out.println("父类中的method方法");
    }
}
public class Zi extends Fu{
    int num = 10;

    public void method(){
        System.out.println("子类中的method方法");
    }
}
public class Test {
    public static void main(String[] args) {
        //多态方式
        Fu fu = new Zi();
        System.out.println(fu.num);//父类的100
        fu.method();

        System.out.println("============");

        //原始方式
        Zi zi = new Zi();
        System.out.println(zi.num);//子类的10
        zi.method();
    }
}

4.多态的好处

1.多态的new法

父类类型 对象名 = new 子类对象() 相当于大类型接收了小类型的数据

2.多态的好处

原始方式 :等号左右两边一样

​ a.好处:既能调用子类特有的,还能调用重写的,还能调用从父类继承过来的

​ b.坏处:扩展性差

多态方式:父类引用指向子类对象

​ a.好处:扩展性强

​ b.弊端:不能直接调用子类特有方法

5.多态中的转型

5.1向上转型

父类引用指向子类对象(默认)

5.2向下转型

1.可理解为强转

2.父类类型 对象名1 = new 子类对象() (向上转型)

子类类型 对象名 = (子类类型)对象名1 (向下转型)

类似于:double b = 10;

​ int i = (int) b;

3.用于调用子类特有功能

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

    //特有方法
    public void catchMouse(){
        System.out.println("猫会抓老鼠");
    }
}
public class Dog extends Animal {
    public void eat(){
        System.out.println("狗啃骨头");
    }

    //特有方法
    public void lookDoor(){
        System.out.println("狗会看门");
    }
}
public class Test01 {
    public static void main(String[] args) {
        Dog dog = new Dog();
        method(dog);
    }

    public static void method(Animal animal) {//Animal animal = Dog  Animal animal = Cat
        animal.eat();
        //向下转型
        Dog dog = (Dog) animal;
        dog.lookDoor();
    }
}

6.转型可能会出现的问题

1.ClassCastException -> 类型转换异常

2.出现原因:转型的时候,等号左右两边类型不一致

3.解决:做判断(instanceof)

对象名 instanceof 类型 -> 关键字前面的对象是否属于关键字后面的类型
public class Animal {
    public void eat(){
        System.out.println("动物要吃饭");
    }
}
public class Dog extends Animal {
    public void eat(){
        System.out.println("狗啃骨头");
    }

    //特有方法
    public void lookDoor(){
        System.out.println("狗会看门");
    }
}

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

    //特有方法
    public void catchMouse(){
        System.out.println("猫会抓老鼠");
    }
}
public class Test01 {
    public static void main(String[] args) {
        Dog dog = new Dog();
        method(dog);

        Cat cat = new Cat();
        method(cat);
    }

    public static void method(Animal animal) {//Animal animal = Dog  Animal animal = Cat
        if (animal instanceof Dog){
            animal.eat();
            //向下转型
            Dog dog = (Dog) animal;
            dog.lookDoor();
        }

        if (animal instanceof Cat){
            animal.eat();
            Cat cat = (Cat) animal;
            cat.catchMouse();
        }

    }
}

  • 43
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值