Java abstract classes Precautions

Use a Case abstract class

Code 1

abstract class Person        //定义一抽象类Person
{
    String name;
    int age;
    String occupation;

    public abstract String talk(); // 声明一抽象方法talk()
}

class Student extends Person    // Student类继承自Person类
{
    public Student( String name, int age, String occupation ) {
        this.name = name;
        this.age = age;
        this.occupation = occupation;
    }

    @Override
    public String talk()   // 实现talk()方法
    {
        return "学生——>姓名:" + name + ",年龄:" + age + ",职业:" + occupation;
    }
}

class Worker extends Person    // Worker类继承自Person类
{
    public Worker( String name, int age, String occupation ) {
        this.name = name;
        this.age = age;
        this.occupation = occupation;
    }

    public String talk()     // 覆写talk()方法
    {
        return "工人——>姓名:" + name + ",年龄:" + age + ",职业:" + occupation;
    }
}

public class AbstractClassDemo {
    public static void main( String[] args ) {
        Student s = new Student("张三", 20, "学生"); //创建Student类对象s
        Worker w = new Worker("李四", 30, "工人");   //创建Worker类对象w
        Person s1 = new Student("王五", 20, "学生"); //通过向上转换的方法创建Student类对象s
        Person w1 = new Worker("马六", 30, "工人");   //通过向上转换的方法创建Worker类对象w
        System.out.println(s.talk());          //调用被实现的方法
        System.out.println(w.talk());
        System.out.println(s1.talk());
        System.out.println(w1.talk());
    }
}

2 runs

学生——>姓名:张三,年龄:20,职业:学生
工人——>姓名:李四,年龄:30,职业:工人
学生——>姓名:王五,年龄:20,职业:学生
工人——>姓名:马六,年龄:30,职业:工人

3 Description

In this case by reference to the parent class itself and references to achieve the creation of an object.

Use abstract class defines two constructors

Code 1

abstract class Person {
    String name;
    int age;
    String occupation;

    public Person( String name, int age, String occupation ) {
        this.name = name;
        this.age = age;
        this.occupation = occupation;
    }

    public abstract String talk();
}

class Student extends Person {
    public Student( String name, int age, String occupation ) {   // 在这里必须明确调用抽象类中的构造方法
        super(name, age, occupation);
    }

    public String talk() {
        return "学生——>姓名:" + name + ",年龄:" + age + ",职业:" + occupation;
    }
}

public class AbstractConstructor {
    public static void main( String[] args ) {
        Student s = new Student("张三", 18, "学生");//第1种方式创建对象s
        Person s1 = new Student("李四", 18, "学生");//第2种方式创建对象s1
        System.out.println(s.talk());
        System.out.println(s1.talk());
    }
}

2 runs

学生——>姓名:张三,年龄:18,职业:学生
学生——>姓名:李四,年龄:18,职业:学生

3 Description

Abstract classes have constructors, methods, and properties in general, more importantly, can also have some abstract methods, we need to subclass to achieve, but in an abstract class constructor after the declaration must be explicitly invoked in a subclass.

Abstract class can not define the final use, defined as the use of final class can not have subclasses, and when to use abstract classes must have subclasses.

Among the abstract class without abstract methods, but even without abstract methods abstract class, the abstract nature has not changed, so it can not be instantiated directly outside the new keyword.

 

Guess you like

Origin blog.csdn.net/chengqiuming/article/details/92159051
Recommended