Class)
Object-oriented thinking comes from cognition of the real world. The real world is colorful, complex, diverse, and difficult to understand. However, smart people have learned to classify these complicated things so that the world is organized. For example, we abstract the concept of a car from all kinds of cars, the concept of a cat from all kinds of cats, and the concept of a colorful flower. Cars, cats, and flowers all represent a kind of things. Each category of things has a specific State, such as the car brand, speed, horsepower, fuel consumption, number of seats, the age, weight, and hair color of the kitten, the color, shape, and number of petals of flowers are all describing the state of things. Each type of thing also has certain behaviors, such as starting, driving, accelerating, slowing down, braking, parking, cat and mouse, flowers in full bloom. These different states and actions separate various things.
Object-Oriented Programming also uses the concept of classes to compile things into "classes ". In a class, data is used to represent the state of a thing, and functions are used to implement the behavior of things. In this way, the programming method is consistent with the human way of thinking, greatly reducing the difficulty of thinking.
Encapsulation(Encapsulation)
Who is the best person in the automobile manufacturing process? Of course, it is not a welding steel plate, nor a tire, not a screw, but an engineer who designs a car because he knows the operating principle of the car. But do we need to know how a car works when we drive? The answer is obviously not required. The operating principle of a car has been encapsulated by a great engineer inside the car. It is just a simple interface for the driver to use. The driver can use the steering wheel and various buttons to start the car flexibly.
Similar to car manufacturing, object-oriented technology encapsulates the state and behavior Implementation Details of things in a class to form a reusable "part ". Once the class is designed, it can be like industrial parts, and thousands of people have no idea about its internal principlesProgramStaff use. Class designers are equivalent to car engineers, and class users are equivalent to drivers. In this way, programmers can make full use of the "parts" that others have prepared, and focus their main efforts on their own specialized fields.
Interface(Interface)
Today, you bought a plasma TV. You took out the remote control and manipulated the buttons to enjoy the wonderful TV programs. People use the TV through the remote control interface. Similarly, people use the class through the class interface. Programmers carefully design interfaces for classes. Well-designed interfaces are not only convenient for other programmers, but also conducive to class upgrade and transformation.
Suppose you are an environmental protection person. Recently, you have replaced ordinary incandescent lamps with energy-saving fluorescent lamps. Incandescent Lights illuminate the room by heating resistance wires; fluorescent lights are electronic energy that activates some molecules in the air. Although they have different principles, their functions are the same-lighting. Because the two lamps have the same interface, you only need to unscrew the incandescent lamp and then screw the fluorescent lamp to easily update the "parts. Similarly, in object-oriented programming, as long as the interface remains unchanged, You can arbitrarily change the implementation details of the class, replace the original class with a better design class, and upgrade the class.
Currently, the cost of software maintenance and modification has accounted for the entire software development.80%The idea of "class" greatly facilitates program maintenance and modification, and reduces software costs.
Object(Object)
As a beginner, it is easy to confuse the concepts of classes and objects. A class is an abstract concept, and an object is a concrete instance of a class. For example, people are a class. Sima Qian, Li Bai, and Du Fu are all objects. Capital is a class. Beijing, London, Washington, and Moscow are objects. animation cats are a class.Kitty,GrafieldAndDoraemonAll objects
Classes are abstract concepts, and objects are real individuals. We can say thatKittyThe weight of a cat is1.5 kgInstead of saying that the weight of a cat is1.5 kgIt can be said that Liu Xiang won the championship in the cross-bar competition, not that humans won the championship in the cross-bar competition. Generally, the State describes a specific object rather than a class, and the behavior is sent by a specific object rather than a class.
In real life, objects are enriched everywhere. A house, a car, an elephant, an ant, or even a language or method can all be called objects.