Introduction to Java (Java Classes and Objects - Object-Oriented Overview)

An overview of object orientation

    Object-oriented thinking is the most natural way of thinking for human beings. It abstracts all preprocessing problems into objects, and at the same time understands that these objects have corresponding properties and displays the behavior of these objects, which has solved some practical problems faced by these objects, so that The concept of object-oriented design is introduced in program development, and object-oriented design is essentially the modeling operation of objects in the real world.

    1.1 Objects

    In the real world, a kind of thing that can be seen everywhere is an object, and an object is an entity in which things exist, such as human beings, desks, computers, etc. The way people solve problems is always to simplify complex things, thinking about what parts these objects are made of. Objects are usually divided into two parts, a static part and a dynamic part. The static part, this part is called "attributes", any object will have its own attributes, such as a person, whose attributes include height, weight, gender, age and so on. The person can cry, smile, talk, walk, these are the behaviors that this person has (the dynamic part). Humans learn about objects by exploring their properties and observing their behavior.

    In the computer world, the idea of ​​object-oriented programming is to think about problems in terms of objects. First, the entities in the real world should be abstracted into objects, and then the properties and behaviors of this object should be considered.

    1.2 Class

    A so-called thing cannot be described as a class of things, such as a bird cannot be called a bird. If you need to collectively refer to the same kind of thing, then class.

    A class is a general term for the same kind of things. If a thing in the display world is abstracted into an object, a class is a general term for such objects, such as birds and humans. A class is a specification on which an object is constructed. For example, a bird has a pair of wings, and it can fly with this pair of wings. Basically, all birds have the feature of wings and the ability to fly, so they have the same characteristics and behavior. A class of things is called a class. A class is an abstract name for a thing in the world, and an object is the entity corresponding to this thing. If faced with a real problem, it is usually necessary to instantiate a class object to solve it.

    A class is a carrier that encapsulates the properties and behaviors of an object. Conversely, a class of entities with the same properties and behaviors is called a class.

    In the Java language, the behavior of objects in a class is defined in the form of methods, the properties of objects are defined in the form of member variables, and classes include properties and methods of objects.

    1.3 Packaging

    Object-oriented programming has the following characteristics: (1) encapsulation; (2) inheritance; (3) polymorphism.

    Encapsulation is the core idea of ​​object-oriented programming. Encapsulate the properties and behaviors of an object, and its carrier is a class, which usually hides its implementation details from clients. This is the idea of ​​encapsulation. For example, when a user uses a computer, he only needs to use his fingers to tap the keyboard to achieve some functions, without knowing how the computer works, even if it is possible to know the working principle of the computer, but when using the computer, it is not completely dependent on the working of the computer. principles of these details.

    The idea of ​​encapsulation ensures the integrity of the internal data structure of the class. Users who apply this class cannot easily directly manipulate this data structure, but can only execute the data that the class allows to be disclosed. In this way, the influence of external operations on internal data is avoided, and the maintainability of the program is improved.

    1.4 Inheritance

    There is also a relationship between classes, such as a department store class is associated with a salesperson class, and this relationship between classes is called an association. Associations mainly describe the general binary relationship between two classes. For example, a department store class is an association with a salesperson class, and a student class is also an association with a teacher class. There are many kinds of relationships between two classes, and inheritance is one of the associations.

    When dealing with a problem, you can keep some useful classes and reuse them when you encounter the same problem. Designing software code using inheritance can shorten software development time. Reusing those defined classes can improve system performance and reduce the chance of errors during system use.

    Inheritance mainly exploits the common properties between specific objects. For example, a parallelogram is a quadrilateral, and a square and a rectangle are also quadrilaterals. A parallelogram and a quadrilateral have common characteristics, that is, they have 4 sides. The parallelogram class can be regarded as an extension of a quadrilateral. A parallelogram reuses the properties and behaviors of a quadrilateral. , while adding properties and behaviors unique to parallelograms, such as parallelograms whose opposite sides are parallel and equal. Here the parallelogram class can be thought of as inheriting from the quadrilateral class. In the Java language, a parallelogram-like class is called a subclass, and a quadrilateral-like class is called a parent class or superclass. It can be said that a parallelogram is a special quadrilateral, but it cannot be said that a quadrilateral is a parallelogram, that is, instances of subclasses are instances of parent classes, but instances of parent classes cannot be said to be instances of subclasses.

    The inheritance relationship can be represented by a tree relationship, and there is a hierarchical relationship between parent classes and subclasses. A class is in the inheritance system. It can be the parent class of other classes, providing trees and behaviors for other classes, or it can be the subclass of other classes, inheriting the properties and methods of the parent class.

    1.5 Polymorphism

    The trait of applying an object of a parent class to a child class is polymorphism. The polymorphism is illustrated by the graphics class. Each graphics has the ability to draw its own. This ability can be regarded as the behavior of the class. If the objects of the subclass are regarded as the instance objects of the parent class, so when drawing graphics When you simply call the parent class, that is, the graphics class's method of drawing graphics, you can draw any graphics. This is the basic idea of ​​polymorphism.

    Polymorphism allows programs to be written in a uniform style to handle a wide variety of existing and related classes. The unified style can be implemented by the parent class, and according to the processing of the unified style of the parent class, the object of the subclass can be instantiated. Since the processing of the whole event only depends on the method of the parent class, it is only necessary to maintain and adjust the method of the parent class in the future, which reduces the difficulty of maintenance and saves time.

    The implementation of polymorphism does not depend on concrete classes, but on abstract classes and interfaces.

    As the parent class of all graphics, the graphics class has the ability to draw graphics. This method can be called "drawing graphics", but if you want to execute this "drawing graphics" command, no one knows what kind of graphics should be drawn, and if you want to A graphic object is abstracted in the graphic class, and no one can tell what the graphic is, so it is more appropriate to use the word "abstract" to describe the graphic class. Such classes are called abstract classes in the Java language, and abstract classes cannot instantiate objects. In the mechanism of polymorphism, the parent class is usually defined as an abstract class, and the standard of a method is given in the abstract class, but the specific process of implementation is not given. In essence, this method is also abstract. For example, the "Drawing Graphics" method in the graphics class only provides a standard for drawing graphics, and does not provide a specific process for drawing graphics, because no one knows what shape graphics need to be drawn.

    In the mechanism of polymorphism, a more convenient way than abstract classes is to define abstract classes as interfaces. A collection of abstract methods is an interface. The concept of interface is also very common in reality. For example, different hardware stores buy screw caps and screws. The screw cap can be easily screwed on the screw. The manufacturer of the screw cap and the screw may be different, but these two items can be easily This is because the manufacturers of screw caps and screws follow a standard, which in Java is an interface. You can use "Drawing Graphics" as an abstract method of an interface, and then make the graphics class implement this interface and implement the abstract method "Drawing Graphics". When the triangle class is drawn, you can inherit the graphics class and rewrite the "Drawing Graphics" in it. Graphics" method, and rewrite this method to "Draw Triangle", so that different graphics can be drawn by this standard.

    Welcome everyone to make mistakes, suggestions, and make progress together.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325631926&siteId=291194637