Three features of object-oriented

Source: Internet
Author: User

Zookeeper

Introduction: I think this is nothing more basic about the three features of "Object-Oriented". It is these basic things that always make me ambiguous. Today, I found some materials on the Internet to learn about it. By the way, I sorted it out. I feel that my basic knowledge is still too weak and I need to learn more.

Encapsulation:
First, attributes can be used to describe the characteristics of a class of things. behavior can describe the operations that a class of things can perform. encapsulation refers to the commonality of the same class of things (including attributes and behaviors) to a class for ease of use. for example, a person can be encapsulated in the following method:
Persons {
Name (attribute 1)
Gender (attribute 2)
Meal (Behavior 1)
Work (behavior 2)
}

Inheritance:
Because of encapsulation, all the descriptive information of a class of things with common characteristics is classified into one class. However, we know that this is not omnipotent. Some things have commonalities, but there are still differences, for example, teachers are simply encapsulated as follows:
Instructor {
Name (attribute 1)
Gender (attribute 2)

Meal (Behavior 1)

Teaching (behavior 2)
}
The encapsulation of "Teachers" is basically the same as that of "people", but it only has one more characteristic behavior: teaching, teachers share the same characteristics as people, however, we cannot say "people teach", that is, we cannot encapsulate teaching into "people". Teaching is one of the characteristic actions of teachers. to facilitate the encapsulation of teachers (code reuse, this is only one of the reasons for inheritance), teachers can be allowed to inherit, such:
Instructor extends {
Teaching (Behavior 1)
}
In this way, we do not need to redefine the attributes and behaviors encapsulated by the "person" class, but only need to use the inheritance method, expand the teacher's proprietary behavior on the basis of people, that is, "Teaching" can describe the teacher, that is to say, teachers also possess all attributes and behaviors encapsulated in "persons" and their own characteristic behaviors "Teaching ".

Polymorphism:
The concept of polymorphism is developed on the basis of encapsulation and inheritance (in fact, I think abstraction is also one of the major characteristics of object-oriented. To encapsulate, abstraction is necessary)

There are two ways to achieve polymorphism: overwrite and reload.
Overwrite: refers to the method by which child classes redefine the functions of the parent class.
Overload: multiple functions with the same name are allowed, and the parameter tables of these functions are different (maybe the number of parameters is different, maybe the parameter types are different, or both are different ).

A simple understanding of polymorphism, such:
The human class encapsulates many features common to humans,
A teacher is a child class of a person and inherits his/her attributes and behaviors. Of course, a teacher has his/her own characteristic behaviors, such as the teacher's teaching;
Students are child classes of people and inherit the attributes and behaviors of people. Of course, students have their own characteristic behaviors, such as students attending classes;

Now, when we need to describe the actions of teachers and students, we can separate them to say "teachers are teaching" and "students are in Class", but if we want to abstract them, that is, how do we describe both the teacher and the student's parent class "person" to describe their respective behaviors? "People are teaching "? "Persons are in class "? Is this strange and inappropriate? The inappropriate problem is that we use abstract "people" for the behavior subject, but we use specific "Teaching" and "class" for the behavior itself ". how can this problem be solved? Then we need to solve the abstract and specific contradictions.
Since we describe the behavior from the abstract point of view, can we describe the behavior at the same time? For example, "people are doing things" (both teachers and students are doing things as adults). This solves the conflict between abstract levels and specific levels.

In this step, we can describe the following two parts: "teachers are doing things" and "students are doing things". Then, we can call teachers' own characteristic behaviors in "Teachers'" Doing Things "to" teach ", in "student"'s "work" behavior, we call students' own characteristic behavior "to do homework". Therefore, when we call "person" to "work, if this person is a teacher, what he does is actually "Teaching". If this person is a student, what he does is actually "doing homework ". that is to say, "people" are polymorphism. in different forms, feature behavior is different. Here, "people" have two forms at the same time, one is the teacher form, one is the student form, and the corresponding characteristic behaviors are "Teaching" and "class" respectively ". the process described above is actually a manifestation of the polymorphism mechanism.

Polymorphism means to implement a unified behavior at the abstract level. At the individual (specific) level, this unified behavior will be caused by individual (specific) to implement their own feature behavior.

As long as you understand:
It can describe one thing from an abstract point of view. For this abstract thing, each individual (specific) can find its own behavior to execute. This is polymorphism.

My personal understanding of polymorphism is as follows: There are many types of people, including teachers (who can teach), students (who can attend classes), and IT men (who write code hard) and so on, so all people are polymorphism and there are a variety of hard forms; or, for example, beauty is polymorphism, beauty like a circle, beauty like Lin Qingxia, and beauty like Wang zuxian, so the beauty is also polymorphism.

Let's take a look at the specific code:

public class Person { public String sex; public String name;  public String getSex() {  return sex; } public void setSex(String sex) {  this.sex = sex; } public String getName() {  return name; } public void setName(String name) {  this.name = name; } public void eatSomeThing(){  System.out.println("eating someThing ..."); }  public void doSomeThing(){  System.out.println("doing someThing ..."); }   }


Public class Teacher extends Person {public void doSomeThing () {// overwrite the parent class doSomeThing method System. out. println ("Instructor: Teaching ...");}}


Public class Student extends Person {public void doSomeThing () {// overwrite the parent class doSomeThing method System. out. println ("Student: class ...");}}


Public class Client {public static void main (String [] args) {Teacher t = new Teacher (); t. setName ("Zhang San"); System. out. println (t. getName (); t. eatSomeThing (); // human feature t. doSomeThing (); Student s = new Student (); s. setName ("Li Si"); System. out. println (s. getName (); s. eatSomeThing (); // human feature s. doSomeThing ();}}

Output result:


Zhang San eating someThing... Teacher: Teaching... Li Si eating someThing... Student: class...





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.