Introduction to the use of Java abstract classes


1. The concept of abstract class

In the object-oriented concept, all objects are described by classes, but conversely, not all classes are used to describe objects. If a class does not contain enough information to describe a specific object , Such a class is an abstract class.

Second, the use of abstract classes

1. The format of abstract classes and abstract methods

The abstract key isabstract

Abstract class: plusabstract keyword modification
Abstract method: plusabstract keyword modificationRemove the braces

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

    //成员方法
    public void methodTest(){
    
    
    }
}

【note】

1.抽象方法一定在抽象类中抽象类不一定有抽象方法
2.抽象类仍然可以有成员变量、成员方法、构造方法

2. Use of abstract classes and abstract methods

If you want to use abstract classes and abstract methods, you must have an inheritance relationship
Abstraction is generated on the basis of inheritance. If you are unfamiliar with inheritance, please click here.
Frist
defines an abstract class Person

Add attributes and methods to the Person class

public abstract class Person{
    
    
    //抽象类可以拥有成员属性、成员方法、构造方法
    String name;
    String sex;
    //定义两个抽象方法
    public abstract void eat();
    public abstract void sleep();
}

Second
defines a Student class (student class) that inherits the Person class

The student class is a subclass of the abstract Person,必须实现(重写)Person类的所有抽象方法

public class Student extends Person{
    
    
    @Override
    public void eat() {
    
    
        System.out.println("学生吃饭了");
    }
    @Override
    public void sleep() {
    
    
        System.out.println("学生睡觉了");
    }
}

Third
defines a Test class, creates a subclass object and uses

public class Test{
    
    
    public static void main(String[] args) {
    
    
    Student s = new Student();
    s.eat();
    s.sleep();
    }
}

operation result:
Insert picture description here
【note】

1. The abstract class can 具有所有普通类具拥有的东西
2.Abstract method without curly braces
3. 抽象类不能创建对象
4. 抽象类中不一定有抽象方法but 有抽象方法的类一定是抽象类
5.The subclass of the abstract class must override all the abstract methods of the parent class( 子类是抽象类除外)
6. The construction method of the abstract class is provided to the subclass to initialize the parent class member


Seeing this, you may feel that the abstract class is unnecessary, so please continue to look down


3. The meaning of abstract classes

Abstract class 约束了继承它的子类,必须实现某一种或多种功能.
Putting it in real life can putAbstract class is understood as a norm.

For example:
Just like a mobile phone, it has the most basic functions for making calls and texting. This can derive an abstract class. The abstract class needs to define abstract methods for calling and texting. Although the abstract class does not need to care about the specific implementation, it ensures that all mobile phones (subclasses) must implement the functions of calling and texting.

4. Examples of the use of abstract classes

Define abstract Person class

Add properties and methods

public abstract class Person{
    
    
    String name;
    String sex;
    //Person类吃饭的抽象方法
    public abstract void eat();
    //Person类睡觉的抽象方法
    public abstract void sleep();
}

Define abstract Student class

Inherit the Person abstract class

public abstract class Student extends Person{
    
    
	//这里只实现了Person类的eat(),因为抽象子类对抽象父类得抽象方法实现是没有要求的
    @Override
    public void eat() {
    
    
        System.out.println("学生吃饭了");
    }
    //Student类学习的抽象方法
    public abstract void study();
}

【note】

抽象子类Whether 实现抽象父类的抽象方法is 随意of
note that this is only an abstract class implements an abstract method to eat. If other classes inherit the Student class, they must implement the parent class abstract methods that the Student class does not implement and the abstract methods of the Student class

Define two student subclasses (GoodStudent and BadStudent classes)

Inherit the Student abstract class

 public class GoodStudent extends Student {
    
    
 
    static{
    
    
        System.out.println("我是好学生");
    }
    
    //实现Person类的sleep方法
    @Override
    public void sleep() {
    
    
        System.out.println("好学生睡觉了");
    }
    
    //实现Student类的study方法
    @Override
    public void study() {
    
    
        System.out.println("好学生在学习");
    }
    
}
public class BadStudent extends Student {
    
    

    static{
    
    
        System.out.println("我是坏学生");
    }
    
    //重写Student类eat方法
    @Override
    public void eat(){
    
    
        System.out.println("我最能吃");
    }
    
    //实现Person类的sleep方法
    @Override
    public void sleep() {
    
    
        System.out.println("坏学生没睡觉");
    }
    
    //实现Student类的study方法
    @Override
    public void study() {
    
    
        System.out.println("坏学生没学习");
    }
    
}

GoodStudent and BadStudent classes need to implement all abstract methods that have not been implementedThe abstract methods that have been implemented can also be rewritten as needed

Write test classes and view program execution results

public class Test{
    
    
    public static void main(String[] args) {
    
    
        //创建好学生对象
        GoodStudent goodStudent = new GoodStudent();
        goodStudent.eat();
        goodStudent.study();
        goodStudent.sleep();
        System.out.println("==============");
        //创建坏学生对象
        BadStudent badStudent = new BadStudent();
        badStudent.eat();
        badStudent.study();
        badStudent.sleep();
    }
}

operation result
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44580492/article/details/106300270
Recommended