Catalogue
I. process-oriented thinking and object-oriented thinking
Ii. simple understanding of object-oriented
Three, object-oriented design ideas
Iv. concepts of objects and classes
V. how to abstract a class?
Vi. Relationships between classes (objects)
Vii. Java and object-oriented
Eight, why use object-oriented programming?
I. Process-oriented thinking and object-oriented thinking
Object-oriented and process-oriented ideas are fundamentally different, as object-oriented thinking, when you get a problem, you analyze the problem is no longer the first step to do what, the second step to do what, this is a process-oriented thinking, you should analyze the problem of what classes and objects, this is the 1th, Then analyze what properties and methods these classes and objects should have. This is the 2nd. Finally, it is 3rd to analyze what is the specific relationship between classes and classes.
object-oriented has a very important design thinking: the right method should appear in the appropriate class
Ii. simple understanding of object-oriented
It is in the program that the first decomposition should be noticed is no longer a step by step process, but first consider in this problem domain or the program should have what objects, so from now on to think of any problems in the mind do not think of me to achieve this thing I first step should do, the second step should do, If you think like that, it's a process-oriented mindset. Object-oriented thinking is that when I come across this problem domain, when I run into this program, I should first put the object in this question, what is the relationship between objects and objects abstracted out.
three, object-oriented design ideas
The basic idea of object-oriented is to construct the software system from the reality in the real world, and to use the natural way of thinking as far as possible in the structure of the system.
Object-oriented more emphasis on the use of human logic in the daily life of thinking and principles often adopted, such as abstraction, classification, inheritance, aggregation, polymorphism and so on.
When people think, the first thing you see in your Eyes is an object.
Iv. concepts of objects and classes
objects are used by computer languages to describe things in a problem domain, with objects using attributes (attribute) and methods to correspond to the static and dynamic properties of things .
A class is an abstract concept used to describe objects of the same class, which defines the static and dynamic properties of objects of this type.
A class can be seen as a template for a class of objects, and an object can be seen as a concrete instance of it .
eg. what is a bottle?
The definition of a bottle: something with certain class characteristics is a bottle, the score says what kind of shape, say have a mouth, can pour water, can fill water, generally have a cover and so on. The process of defining a bottle is actually abstracting something out of the bottle, so the bottle here is an abstraction called a kind of thing, the concept of a bottle in your mind, what is the concept of a bottle in your head? The concept of a bottle is called an abstraction of a kind of thing in your mind. How is it abstract? You tend to abstract these two aspects: the first aspect we call it static properties, the bottle should have what characteristics, the score said the bottle should have a mouth, which is its a static property, the bottle usually has a lid, which is also it has a static property, in addition, you may also give it a summary of dynamic properties, What about the dynamic properties? The bottle can pour water, which is its dynamic property. The concept of the bottle in your mind if you think carefully, you actually gave it two aspects of the summary, on the one hand is static, on the one hand is dynamic. Reflected on the Java class, one is the member variable (static property), one is the method (dynamic property) method can be executed, can move. A member variable is a static property of a class. So the concept of a bottle in your head is actually an abstraction of something that we call a class, a chair is a class, a table is a class, a student is a class. What is an object? A specific instance of this type of thing is called an object. Therefore, a particular thing of a class of things, which conforms to the specific characteristics of such things, is called an object . A bottle is a class, a bottle is an object within the class of a bottle.
v. How to abstract a class?
There are two aspects, on the one hand, its static property, and the other is its dynamic property. How does a class in Java wrap it? On the one hand member variables, on the other hand are methods.
eg. how do you abstract the class of employees? It is also from two aspects, on the one hand is its static property, on the other hand its dynamic property
What are the attributes of the staff? What are the methods of having a name, age, current salary and other attributes? Let the clerk show his name, show his age, change his name and get his salary. Of course, the display of the name, display age, change the name, collect salary these can also let others do, but object-oriented design thinking is the most appropriate method should appear in the most suitable class . Display the name, show age, change the name, pay the salary by who do more suitable, that is the staff of the most appropriate. So these methods should appear in the staff category.
For a class, it has some properties or is called a member variable, and later said that the attribute or member variable refers to the same thing. The specific object does he have any related attributes or called member variables? Yes, each person has a share, but the value is different. For example, two staff members from this class of staff: Staff A and Clerk B, who have names, ages, current wage amounts, but their names, age, and the amount of wages they receive are different. This distinguishes staff a from employee B, because they have different property values, so the object can be distinguished from another object, so the attribute is able to distinguish between two objects . The cat is a class, the cat is an object, how should this cat and the other cat distinguish? It depends on how your cat is defined, cat has cat hair, hair has color. OK, the cat is a black cat, and the other cat is a white cat, which distinguishes it by the color of cat hair. If only one definition, such as catching mice, white cat can catch, black cat can catch, so there is no way to distinguish between black cat and White cat, so according to the method is not able to distinguish two objects . So each object has its own property, and the property value is not the same as another object .
Be sure to differentiate between classes and objects, what is a class? What is an object? A class is an abstraction of a kind of thing, an abstraction of a kind of thing with common characteristics. An object is a specific instance of this class, so later instances (instance) or objects (object) refer to the same story.
vi. Relationships between classes (objects)
A. Association relationship (weakest relationship)
Classes and classes are related, such as students and teachers of the two classes, teachers can teach students, students can learn from the teacher. This is the relationship between them. Relationships and relationships are different, and your relationship with your wife and your relationship with your other girlfriends cannot be confused. A relationship with the weakest relationship is called an association relationship. An association relationship reacts to code that is often a method of a class in which the parameter is a specific object of another class, such as teaching a graduate student, teaching a graduate student, teaching is a method of teaching this class, a graduate student is a specific object in this class. correlation is the weakest relationship , we have a relationship between the two classes, or there is a relationship between two objects, but the relationship is not very close.
two. An inheritance relationship (a stronger relationship)
The inheritance relationship encapsulates the logic that "xx is a kind of xx", so long as this thing can be said, you can consider the inheritance relationship to encapsulate it . For example: Swimmers inherit from athletes, swimmers are an athlete, which makes sense, so swimmers are inherited from athletes, swimmers and athletes is an inheritance relationship. The student is a person, the student inherits from the person, the teacher is a person, the teacher also inherits from the person, the student is a kind of teacher, this does not pass, therefore the student and the teacher does not have the inheritance relation. So in the future to do design to distinguish between the inheritance is very simple, you just say a word: "xx is a kind of xx." OK, then they are the inheritance relationship. Basketball player is a ball player, which makes sense, so the basketball players inherit from the ball players, so inheritance is likely to produce a succession of trees, athletes derived from the meaning of swimming athletes this class, ball players this class, shooting athlete class inherit from it, The equivalent of athletes derived from these different athletes, including swimming, ball, shooting. The re-derivation of football, basketball, or volleyball. This is an inheritance tree, but this tree is a more idealized case, with only one root node. But in reality, the inheritance of our real world is not necessarily inherited from only one class, a class may inherit from multiple classes, such as: Golden Monkey inherit from the class of animals, which is normal, but I have another special class: "should be protected", which is also a class, the Golden monkey should be protected, So the Golden Monkey is a kind of thing that should be protected. So the golden monkeys inherit from the category "something that should be protected". So in reality, a class is completely possible to inherit from several different classes, and C + + is because it wants to encapsulate this inheritance, so C + + has multiple inheritance.
three. Aggregation relationships (overall and partial) (a stronger relationship)
What is aggregation? aggregation is the relationship between a whole and a part . We say "xx is part of XX", as long as it is said, then the relationship between them is the aggregation relationship , the captain is a part of the team, the team is a part. So the captain and the team are the aggregation relationship, the team and the team are also the aggregation relationship. The head is a part of the human body and the arm is also a part of the human, so the head, body and arms and people are the aggregation relationship. The aggregation relationship can be subdivided into aggregation relationships and combinations of relationships, such as team, Captain, team members, these three are aggregation relations, if the captain is a football captain, but also the captain of the basketball, a person is divided into two different teams, which is possible, Between the team and the Captain no I can't leave you, you can't leave me this way, so if you get finer, this is called the aggregation relationship. There is also a situation called combination, the combination is that we are inseparable, I am an essential part of you. A person's head can not belong to you and belong to others, the body is the same, can not belong to you and belong to others. So your body, your head and you are inseparable, this is a more rigorous aggregation relationship, specifically to it named a combination.
Four. Implementing Relationships
as a parent, I think I should have such a method, but I do not know how to achieve, who to achieve, my subclass to achieve, this is the realization of the relationship. There is also a relationship called Polymorphism that is closely related to the realization of the relationship.
Five. polymorphic
Vii. Java and object-oriented
Objects and classes are inseparable, you must first define a class to have an object. The method is defined first to be called. The object is the core of Java, and to do anything you first have to create an object for me to do. A static property, called a member variable, is the same thing that refers to a property or member variable.
The whole class can be thought of as static properties and there is a synthesis of methods between them. How to abstract the concept of a class, or that sentence, you have to abstract two aspects, the first aspect is to see their static properties, they have what member variables, the second aspect is to see what their methods.
When we write Java programs, we write about the public class (Declaring a Class), in which we write member variables and methods.
each class in Java corresponds to an abstraction of something in our real life . For example, in Java to encapsulate a dog, specifically how to encapsulate, how to write code, the code is as follows:
1 package cn.javastudy.summary; 2/** 3 * A class of things encapsulated in Java first has to write class, define this class, the class name is what can be taken. 4 * The class name here is called Dog 5 */6 public class Dog {7/** 8 * Next is to write the dog this class of properties or called member variables, 9 * For example, dog this class of hair color, how to define this property, 10 * First you have to define a type of hair, such as using int to define the color type of the hairs one */12 int furcolor; Definition properties: The color of the wool float height; Definition properties: Height of the dog, float weight; Defining properties: Dog Weight 15 16/**17 * Dog color, height, weight these attributes are defined, and the next thing to define is the method. 18 * such as writing a Catchmouse () method, the method of catching mice. * Catchmouse This method has an object type parameter, which mouse to catch, this object parameter belongs to the class of Mouse * @param m21 */22 void Catchmouse (Mouse m) { 23//In the method body write catch mouse This process, how to catch, run to catch, walk to catch the System.out.println ("I caught the mouse, bark!") , the rat's going to scream! 25/**26 * The mouse screamed and said it was bitten by a dog, and it was natural to think, 27 * Screaming (Scream ()) that this method belongs to one of mouse's classes. Method. 28 * The mouse calls it itself and makes it scream itself. This is object-oriented thinking. */30 M.scream ();}32 public static void Main (string[] args) { D = new Dog ();//First use the New keyword to create a Mouse m=neW Mouse ();//Build a mouse. D.catchmouse (m);//Then use the dog to catch the mouse and let the dog Call the Catchmouse () method to catch a mouse. 37}38}
1 package cn.javastudy.summary; 2/** 3 * Package Mouse Class 4 */ 5 public class Mouse {6 /** 7 * Mouse has a screaming method 8 * When bitten by a dog will scream 9 */10 public void Scream () {One System.out.println ("I was bitten by a dog, so it hurts!") "); }13 14}
In this sense, each definition of a class in Java is actually equivalent to a new data type. Just like int, float, string, and so on, it's just a new type of definition.
Eight, why use object-oriented programming?
Object-Oriented Programming: A group of objects interact with each other to accomplish specific functions through communication
A realm of hard-to-do software is reusability (reusable) and extensibility. If it is a process-oriented, the general situation is that the properties and methods they are separate, they are not aggregation of the relationship, not together, so it is more troublesome to reuse, the level of reuse is limited to the method at this level, and object-oriented is different, it is the properties and methods integrated in one. When combined, the entire object is reused. So object-oriented and process-oriented, the former is more likely to enable us to achieve reusability.
Object-oriented 1