Java Basics-Objects and Class 1 (object-oriented basic concepts)
first, the programming idea
Programming thought : the need--> to establish the idea--> target code
process-oriented thinking : From top to bottom, gradually refinement
Not suitable to solve some of the more complex problems
the idea of object-oriented
Ii. Basic concepts of objects
1, Object : No matter what is the object, all the objective existence of things
2, properties : what
3, how can you do that?
For example, for a car object, this object has color, brand, price, top speed and other attributes, there are start-up, acceleration, steering, braking and other methods.
Objects do not exist in isolation, and there is a connection between objects and objects in some way.
One way is the relationship of the method invocation. An object can invoke the method of another object, thereby generating an association between the two objects. That is, the object's method consumer is also an object. For example, a student object can call the teacher's object's "lecture" method, the driver object can invoke the "driving" method of the car object, the customer object can invoke the chef's "cooking" method, and so on. Any object may call methods of other objects, or it may provide methods for other objects to invoke.
Another way is to combine attributes: An object's properties, possibly another object. For example, the host, keyboard, mouse and so on small objects, can be regarded as a computer this large object of the attributes, with a number of student objects as attributes, the composition of the class object, a number of book objects, the composition of library objects and so on.
By using these two methods, we can organize objects with relatively simple functions to form complex systems.
The objective world is made up of objects.
1, small objects constitute large objects
2, the objects are called between the methods
For example, in real life, enterprise this object, often is a very complex system. However, each object in the enterprise, each employee, the problem to be solved is always relatively simple. For example, in a software enterprise, the programmer to solve the problem is programming, accounting to solve the problem is to settle accounts, sales to solve the problem is to obtain orders, and so on. However, although each employee has a relatively simple problem to solve, when a large number of simple objects are grouped together, a very complex enterprise system can be formed.
Solving objective problems: Object-oriented thought
1, get the objects you need to solve the problem.
2, how to meet the needs of the target
For example, suppose we are going from Beijing to Shanghai. In order to accomplish this task, the first thing to do is to determine, in what way to Shanghai, is to fly. Take the train. or driving by yourself. Wait a minute.
This is a process of choosing a vehicle, which is the process of choosing the right object to solve the problem. In this example, we want to be able to select an object that can have a "transportation" approach. Obviously, cars, trains, airplanes, all have this "transportation" method. According to the actual situation, we can choose different means of transport and call its "transportation" method to complete the goal from Beijing to Shanghai. If we choose the "aircraft" object, then we need to call the "ticket Agent" object of the "sell tickets" method, to obtain a "ticket" object. Then call the "Taxi" object's "drive" method and get to the airport. Use the hands of the ticket, call the "aircraft" object of the "boarding" method, "flight" method. So as to reach the destination.
third, the object in the computer
computer demand comes from the objective world
To solve computer problems, we can also adopt object-oriented ideas.
Designing E-commerce sites is a problem in the computer field. However, we can see that this problem is put forward in close connection with the real world. In other words, computer programming to solve the problem, must come from the real life. For example, there are word software in the software industry to solve the problem of people dealing with documents; There are Excel software to solve people's statistical data and the need to make reports, and QQ software is to solve the needs of people's communication and communication, and so on.
Since the problems of the computer world come from the real world, the best way is to let the computer simulate the real world. Since the real world is an object-oriented world, it is natural that we want to introduce object-oriented ideas in the computer world, so that programmers can take advantage of real-life experiences to better solve computer problems.
objects in the computer :
Continuous storage space in 1,JVM (data)
2, representing the objects in life, objects in life in the abstract of the computer
Through abstraction, we turn the objects in reality into objects in the computer. For example, we abstract the student objects in real life and form the student objects in the computer. At the time of abstraction, there are several attributes of the student object: Name, age, and student performance. So, for a student object on a computer, it's actually a data area in memory. In this area, there is a string that represents the name, an integer used to denote age, and a double number to indicate the student's performance. The representation in memory is as follows:
In memory, we assign a range of data that contains the attributes of the abstract student object. These data appear as a whole, representing the student object in life. So, essentially, an object in a computer is an area of data in memory.
by abstracting real-life objects into objects in the computer, we can simulate the same object-oriented world as the real world in the computer world. In this way, when we are programming, we can use this idea to solve the problem.
Iv. The characteristics of object-oriented
1, their duties
The use of object-oriented thinking, the completion of a requirement, may be a number of objects to participate together. And each of the 6 objects that make up the system should have a single function. This is the object of "each other's duties."
2, weak coupling
Weak coupling refers to making the connection between objects and objects as weak as possible. What is the advantage of this? If an object is less connected to the surrounding, the object can be easily replaced.
3, reusable
The so-called reusability means that for similar functions, different systems can reuse the same code. In this way, some common functionality, after the programmer has written it again, after encountering similar functions, do not need to start from scratch, just need to use these common functions to do.
4, scalability
Scalability refers to the extension of the function of the system without modifying the original system. In the process-oriented programming approach, if you want to increase the functionality of the system, most of the process must be changed.
v. Concept of class
1. Abstract of object generality
For example, in real life, there are many dog "objects" (small white, rhubarb, Richter, Fu ""). For a baby who doesn't know "dog", when he saw more and more dog object, slowly can gradually sum up, this kind of object characteristic: Four legs, will eat will run will call, will wag tail "" So, slowly, he knew the dog. In other words, in his mind, the concept of "dog" was formed, and the "class" of the dog was included in his mind. In other words, when people face a large number of objects, slowly will be the object of the generality of abstraction, thus forming a "class." In other words, class is a summary and abstraction of objective things.
2, the subjective reflection of objective object in human brain, class is the cognition of human being to object
At the same time, we note that "class" is a concept formed in the human mind, that is, the class is in the human mind, not an objective thing. We can assume that class is the reflection of objective things in the human brain.
3, the class is the template of the object
In the computer world, there is another layer of meaning: A class can be used as a template for creating objects. That is, in a computer, if a class is defined, you can use this class to create multiple objects.
As shown in the figure above, we have defined a Student class that stipulates that the Student object has two attributes, a String name, and an age of type int. After creating four objects, it is clear that these objects are built with the Student class as a template.