Package:
first, attributes can be used to describe the characteristics of the same class of things, and the behavior describes what a class of things can do, encapsulation is to classify the commonality (including attributes and behaviors) of the same kind of thing into a class for ease of use. For example, people this stuff, can be encapsulated in the following way:
Person {
Age (attribute one)
height (attribute two)
Sex (attribute three)
work (one of the behaviors)
Walking (Act II)
Talk (Act of the third)
}
Inheritance:
because of encapsulation, so that all the descriptive information of a class of things with common characteristics is attributed to a category, but we know that this is not omnipotent, some things have commonalities, but there are differences, such as teachers, simple package up as follows:
Teacher {
Age (attribute one)
height (attribute two)
Sex (attribute three)
work (one of the behaviors)
Walking (Act II)
Talk (Act of the third)
Teaching (Act of the four)
}
The encapsulation of "teacher" above is almost the same as the encapsulation of "human", just one more characteristic behavior: teaching,
teachers have the same characteristics as people, but we can not say "people teach", that is, teaching can not be encapsulated in "people", teaching is one of the characteristic behavior of teachers. In order to easily encapsulate the teacher (code reuse, which is only one of the reasons for the existence of inheritance), can let teachers to inherit people, such as:
teacher extends person {
Teaching (Act III)
}
in this way, we do not have to redefine those properties and behaviors that have been encapsulated by the "human" class, but only by using inheritance to expand the teacher's proprietary behavior on a human basis, that is, "teaching" can describe the teacher, and the result is that teachers also have "people" All attributes and behaviors encapsulated in it also have their own characteristic behavior "teaching".
Polymorphic :
The concept of polymorphism is developed based on encapsulation and inheritance (in fact, I think abstraction should also be regarded as one of the most object-oriented features, to encapsulate, abstraction is necessary)
a simple understanding of polymorphism, such as:
Human beings , which encapsulate a lot of common human traits,
teachers are the children of human beings, inheriting the attributes and behaviors of people, of course, teachers have their own characteristic behavior, such as teaching teaching;
students are the children of human beings, inheriting the attributes and behavior of people, of course, students have their own characteristics of behavior, such as learning to do homework;
now, when we need to describe the behavior of teachers and students, we can say "teachers are teaching", "students do homework", but if we want to stand in an abstract perspective, that is, from the perspective of teachers and students ' "people", how do we describe their respective behaviors? " People are teaching "?" People doing their homework "?" Isn't that weird? The problem is that, for the subject, we use the abstract level of the East "people", and for the behavior itself, we have used the concrete "teaching" and "teaching". How to solve it? It is necessary to solve the problem of abstraction and concrete contradiction.
since we are standing in the abstract at the angle of the description, then we have to abstract the behavior, can not be described at the same time? For example, "people doing things" (teachers and students do homework can say that adults in doing things), so that the level of abstraction and the specific level of contradiction.
At this point, we can put two descriptions: "The teacher is doing", "the student is doing" the two unify for "the person is doing",
then, we can invoke the teacher's own characteristic behavior "teaching" in the "work" behavior of "teacher",
In the "students" in the "doing " behavior to call students ' own characteristic behavior "do homework",
so, when calling "man" to "do things", if this person is a teacher, then what he does is actually "teaching",
If this person is a student, what he does is actually "doing homework".
That is to say, in this "people" is polymorphic, in different forms, the characteristics of the behavior is not the same, here the "people", at the same time there are two forms, one is the teacher form, a student form, the corresponding characteristics of the behavior is "teaching" and "doing homework. "
The completion of the above description process, in fact, is the embodiment of the polymorphic mechanism.
Polymorphism , is to stand on the abstract level to implement a unified behavior, to the individual (concrete) level, the unified behavior will be because of the individual (specific) characteristics of the characteristics of the implementation of their own behavior.
Polymorphism is a lot more complicated than encapsulation and inheritance, the above description is very simple, do not go to die keying polymorphic two words,
In fact, just understand:
can stand in an abstract way to describe a thing,
and for this abstract thing, for each individual (concrete) can find its own behavior to execute, this is polymorphic.
Three major features of Java object-oriented