Fully resolve Java abstract classes and interfaces scenarios and role of

Abstract class

注:先将抽象类中的两种方法解释完,再综合解释抽象类

Abstract method

  • Scenario: In all its subclasses should have this method but most specific subclass of the steps are different.
  • You must be rewritten: You can also say "must be achieved", because the abstract parent class is no method body.
  • Keywords:abstract

Common method

  • Scenario: In all of its subclasses and method should have the most specific subclass of step is identical.
  • Rewritable: Common methods can be overridden, for each step are the same subclass is no need to rewrite NATURAL; most of the same subclass step is necessary in the case that a small portion of the different sub- class to rewrite.
  • Select by: see scenarios

Detailed abstract class

  • Definition: A method wherein the abstract class must be defined as an abstract class.
  • Raison d'etre: to achieve polymorphism program. (It must be inherited, otherwise its existence is meaningless)

    Question: Since conventional methods can be overridden, then the subclasses have different execution steps I would like to rewrite abstract methods why do I need it?

    A: The abstract method must be rewritten and no parent class method body, rather than the conventional method must be rewritten and the parent class has a method body; thus causing a problem: program too large to use this method, the parent too many ordinary class methods, subclass must remember to rewrite, but the java virtual machine and will not tell you to rewrite, rewrite not just subclass the parent class default method in accordance with the implementation of the body; and abstract method is different the subclass does not override
    the java virtual machine error, this time played a role as well as a reminder mandatory.

interface

  • Definition: the presence of the interface is to achieve scalable procedures.
  • Raison d'etre: If your architecture class, one class extensions to how to do? So we're going to modify the parent of this class is the superclass even say it? This is clearly unreasonable. (And if you use the class provided by someone else, it is simply impossible to modify it) Maybe you will not be traced back to Object. But using the interface, you want to give a class in this system extensions, just give this class implements a new interface, will naturally provide new functionality, it does not affect its superclass, 而它的子类自动也扩充了它新增加的这个接口的方法(a bit like C ++ and more inherit). This makes the software easier to extensions. 设计模式中有一条开闭原则,说:软件实体必须都修改关闭,对扩展开放。Use interface, you can meet such design requirements.
  • Scenario: This class requires some subclasses under which while some not, then access to the interface at the subclass needs.

    Question: Since the interface is a subclass-specific, so why do not we direct this subclass defined common way to do that?

    A: This is no experience writing large programs frequently asked questions. The so-called interface, is used by people you do not know what the subclass, and call your subclass. This time how people know what you subclass way to do that? So you tell him that my subclass inherits an interface, and others to see, oh, this interface, there are methods a, b, c, so that you can call. You say why do not you tell him subclass of it, this is the reusability. An interface, you can use multiple subclasses to achieve it, others just call an interface by changing the subclass, you can use a variety of functions without changing the code. Of course, to achieve sub-class nature of these methods is also OK ah, but if you had a parent, they do not understand what you mean, you inherit the parent class, forget the method of writing to be achieved how to do? Interface played coercive measures.

注:抽象类和接口共同支持了程序的可维护性。

By way of example understanding

First we sort out what the relationship between classes (marked with a * is an abstract class)

  • Student * (students)
    • Studystudent * (learning class)
      • Libstudent (liberal arts students)
      • Sciencestudent (science students)
    • Longhairstudent * (Art)
      • Artstudent (art students)
      • Musicstudent (music students)

Then combing a variety of student behavioral characteristics (marked * are abstract methods, marked # as interfaces)

  • Student's features: wake up, eat, study *, sleep
    • Studystudent new added features: * Exam
      • Libstudent new added feature: do languages ​​#
      • Sciencestudent new added feature: do mathematics #
    • Longhairstudent new added features: Skills *

Next on the code

//Student

public abstract class Student {
    public void getup() {//起床,所有学生都要做且步骤相同
        System.out.println("起床");
    }
    public void eat() {
        System.out.println("吃饭");
    }
    public void sleep() {
        System.out.println("睡觉");
    }
    abstract public void study();
}
//Studystudent

public abstract class Studystudent extends Student {
    abstract public void exam();
}
//Longhairstudent

public abstract class Longhairstudent extends Student {
    abstract public void skilltrain();
}
//Libstudent

public class Libtstudent extends Studystudent implements Morechinese{

    @Override
    public void dochinese() {
        System.out.println("多做语文");
    }

    @Override
    public void exam() { 
        System.out.println("考试政史地");
    }

    @Override
    public void study() {
        System.out.println("学习政史地");
    }

}
//Sciencestudent

public class Libtstudent extends Studystudent implements Morechinese{

    @Override
    public void dochinese() {
        System.out.println("多做语文");
    }

    @Override
    public void exam() { 
        System.out.println("考试理化生");
    }

    @Override
    public void study() {
        System.out.println("学习理化生");
    }

}
//Artstudent

public class Artstudent extends Longhairstudent{

    @Override
    public void skilltrain() {
        System.out.println("美术技能训练");
    }

    @Override
    public void study() {
        System.out.println("学习画画基本功");
    }

}
//Musicstudent


public class Musicstudent extends Longhairstudent{

    @Override
    public void skilltrain() {
        System.out.println("唱歌技能训练");
    }

    @Override
    public void study() {
        System.out.println("学习唱歌基本功");
    }

}
//主类

public class Demo {
    public static void Oneday(Student cuteStudent) {
        cuteStudent.getup();
        cuteStudent.eat();
        cuteStudent.study();
        cuteStudent.sleep();
    }

    public static void main(String[] args) {
        Oneday(new Artstudent());
    }
}

We define a method body (Oneday) in the main category, to view a student day general activities. The method parameter is Student of reference, then we only need to change the instantiation can get different types of students in a day when using this method.

FIG execution results are as follows:

If the changed new Libstudents, execution results as shown below:

How do we define the interfaces to use it? Was added in the main category code is as follows:

Moremath iMoremath=new Sciencestudent();
iMoremath.domath();

or;

new Sciencestudent().domath();

It will be displayed after the implementation of "do math."

Guess you like

Origin www.cnblogs.com/hackerstd/p/12519387.html