Java basics: 24th relationship between classes and objects

Source: Internet
Author: User

The main purpose of this lecture is to help you better understand the object-oriented thinking. The main points of view include: all objects are objects; objects have attributes, States, behaviors, identifiers, and so on; objects have inclusion and association relationships; classes are the division of objects; class is the abstraction of objects. Abstract multiple objects to get classes. assign values to each attribute of a class to get objects.

1 object view 11 thousand objectsNo matter what the environment is, it is undeniable that you will face many objects. If you are studying, books, computers, your classmates, and your teachers are all objects. If you are playing football, football, venue, and football are all targets. If you are eating, the rice bowl, chopsticks, and dining table are all objects. Objects may be very small, such as molecules. objects may be very large, such as the solar system. 1.2 attributes of an object The attributes of an object are used to describe the object's status, features, and components.If you describe a bus, the following content may be involved: What is the license plate number, the vehicle height, the vehicle length, and the vehicle width, the vehicle color, the maximum speed, the rated passenger volume, the mileage of the vehicle, whether it is accelerating, whether it is slowing down, whether it is on the bus, whether it is off the bus, whether it is running, the direction of operation, the speed of operation, the number of tires in the car, the number of seats in the car, the engine of the car, the coin box of the car, and so on. The descriptions are used to describe the characteristics of a vehicle, including: license plate number, height, length, width, color, and top speed. There are statuses used to describe the vehicle, including: mileage, whether to accelerate, whether to slow down, whether to migrate to the customer, whether to get off the customer, whether to run, the direction of the operation, and the speed of the operation. There are components used to describe the car, including: the seat of the car, the engine of the car and the coin box of the car. 1.3 object behaviorThe behavior of an object is the function that the object can complete. Each object has its own behavior. The behavior is used to change the state of the object or send messages to other objects, sometimes an action includes both. Now we will only discuss the behavior that changes the status of the object. We will discuss the behavior of sending messages to other objects later. The following describes the behavior of drivers. Each object has a large number of behaviors. Some behaviors can be seen.Drivers play a major role in the entire 10-way bus system, so the most basic behavior of drivers is: start a car, start a car, speed up, bend, slow down, stop, open a door, close a door, and broadcast. In addition, some behaviors are necessary for drivers, but are not noticed.These behaviors include: being able to walk, move hands, Move feet, seeing external information in the eyes, and querying line information from the brain (Where is the station of the 10-way car ). The ability to walk ensures that the driver can reach the location of the car and get on the bus. Hand Movement is necessary for drivers to turn, shift, switch the door, and play broadcast. It is necessary for drivers to accelerate, slow down, start, and stop these actions. The eyes can see that external information is the basis of all these actions. If you cannot obtain information from the outside, the driver does not know when to stop, when to drive, and when to turn. 1.4 Object ID Each object in the system has its own number. This number is unique for each object in the system and is used to identify this object.Some of these identifiers are familiar to everyone, such as stations and some are unfamiliar, such as license plate numbers. However, the license plate number must be familiar to drivers. Because it belongs to different users of the system, the objects of interest are different. However, if you are a system analyst and want to complete such a system, you need to pay attention to all the information of users in the system. The identifier is also an attribute.The identifier is usually used to describe the object, which is the same as the previous attribute. The identifier itself may be meaningful, such as the station name of the station. The identifier itself may also be meaningless. It is used only as an identifier. For example, an ID card number is used to identify a person, but it does not make sense. 1.5 relationship between objectsThe relationship between objects includes the relationship between N and parts, such as the relationship between tires and cars. N association, for example, Zhang San takes a bus. 1.5.1 overall and partial relationshipThere is a strong dependency between the two objects in this relationship. As mentioned above, a car has several tires. If there are no tires, the car cannot run normally, that is to say, as long as there is a car, there will be its tires. Once an object of this relationship is created, it is used as a whole. Normally, components are not considered separately. For example, if you want to repair a certain tire, you usually say that you want to repair the car, and then you will talk about the tires of the car. Even if you say that the tires are tires, there is a premise that the tires of a car are. Once this relationship is established, it is usually not changed. Or the lifecycle is basically the same. 1.5.2 Association There is usually no dependency between two objects in this relationship.. Just like the whole process of a car's operation, there will be no stop on-site and off-site passengers, so that the passengers it carries will be constantly changing. When a passenger gets on the bus, the association between the vehicle and the passenger is established. After the association is established, the vehicle moves and the passengers on the vehicle follow the movements. When a passenger gets off the bus, the Association is removed, and the vehicle movement will no longer affect the passengers. In addition, unlike the relationship between the whole and the part, once the creation is basically not changed, The association can be created and released at any time as needed. 1.5.3 amount in the LinkOne car has four tires and one car has 20 passengers. The number here is the amount in the relationship. One car corresponds to four tires, 1 car corresponds to 20 passengers. No matter whether it is the relationship between the whole and the part, or the correlation relationship, there is a volume. According to the amount in the relationship, the relationship can be divided into four types: N, one to N, one to N, multiple to N, one to n 2 type 2.1 type is the classification of ObjectsPeople always like classification and can better understand objects through classification. Each object belongs to a specific type. For example, a 10-way vehicle is not a 10-way vehicle, a passenger is not a specific passenger, or a specific station. A 10-way vehicle is usually a collection of all 10-way vehicles. A passenger is a collection of all the people who take the 10-way vehicle, A station is a collection of all 10 bus stations. The 10-way vehicles, passengers, and stations here are different types, and a specific vehicle should be an element in the 10-way car collection, A specific person is an element in the passenger collection, and a station is an element in the station collection. From the above description, we can draw such a conclusion, Type is a set. But is the set type? Cars and people can form a collection, but they are not a type. Therefore, the set is not necessarily a type.. So what kind of set is the type? Measure the test taker's knowledge about the features of the elements in the above collections. A collection of 10-way vehicles is composed of all 10-way vehicles, all of which are cars and look like each other. More importantly, the operating routes are the same and the functions are the same, that is to say, they have many identical features, especially in terms of main features. Here for 10-way vehicles, the main feature is the running line. Therefore, the type should be a set of multiple objects with the same features.When we mention this type, we will know the characteristics of objects of this type. As described above, elements of an object include the attributes, behaviors, and identifiers of an object. Objects of the same type should have these same attributes and behaviors, use the same ID (license plate number or ID card number), but for a specific object, these attributes may be different from the value of the identity, and the behavior may be different. For example, the license plate number is used as the identification of a 10-way vehicle, but different vehicles have different license plate numbers. But the acceleration process may be different for different vehicles. 2.2 type levelsA type is a collection composed of multiple objects. A bus is a collection composed of many buses. A car is also a type and a collection composed of all cars, this collection also includes buses. A specific bus belongs to both the bus and the car, so it can be a bus or a car. So what is the relationship between the two types? Obviously, all buses are cars, that is, the bus collection belongs to the subset of the car collection. The relationship between these two types is usually referred to as the relationship between subclass and parent class. A bus is a subclass and a car is a parent class. Each parent class has its own feature subclass, and each car has its own feature bus. However, a bus has its own special characteristics, such as its specific line, its start time and end time, and its departure interval. In the real world composed of objects, this type of hierarchy is everywhere. Relationship between Class 3 and ObjectThe object is specific and the type is abstract. A type is an abstraction of a group of objects, extracting the common characteristics of this group of objects.These objects are objective objects and specific objects, while types are abstract objects rather than objective objects. So if you say a type, you can't help but think about what a specific object will look like. Types are abstract. You can say that humans have attributes such as name, height, and weight, but you can only say the height of a person (specific object, because a specific person has a specific height. If you assign values to all attributes of a type, you will get a specific object, which is a type instance.To understand the relationship between types and objects, first abstract a type based on these objects with the same features, and then describe the object based on the Type features when necessary. For example, every student in a school has attributes such as student ID, name, birthday, classroom, course, and Class. Therefore, a type of student is abstracted based on these students, in this way, if you want to describe a student, you will also describe the student's student ID, name, classroom, and class, which courses are available. The student will be known through this description. Last time: Lecture 2 Object-oriented OverviewNext time: 25th relationship between software system objects and real-world objectsLi xucheng csdn blog: Why? U= 124362 & C = 7be8ba2b6f3b6cc5

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.