Preliminary Introduction to Java Classes and Objects

Object-oriented is an important programming idea. It is essentially to model objects in the real world. Only with a clear logical concept can the specific implementation work be carried out on this framework. Therefore, this article will focus on this Preliminary organization of knowledge.

1. Object

An object is an entity in which everything exists in the world. Every specific thing can be called an object, such as a person, a cup, an ant, and a mobile phone.

For example, if we want to describe a person, we usually use a specific description method to describe it specifically, such as a person is 180cm tall, his age is 19 years old, his hometown is in Nanjing, his hair is blue, etc. These specific points that can represent other characteristics, in the computer, we call these characteristics attributes. However, he can sing, dance, rap, and play basketball. We can understand this object more concretely through these static characteristics and dynamic behaviors.
insert image description here
When facing certain problems, we can also use object-oriented thinking to solve them.
The steps are as follows
(1) Abstract the specific object in the problem
(2) Identify and describe the attributes of this object
(3) Identify a series of dynamic behaviors of this object
(4) Analyze the problem based on known information

The idea of ​​encapsulation can also be reflected here. To describe a person, it is necessary to describe these attributes or behaviors of this person, and to encapsulate these behaviors and attributes to describe a person. It can be seen that a class is a carrier whose purpose is to encapsulate object properties and behaviors, and an object is a concrete instance abstracted from a class.3

2. class

A thing cannot be described as a category, but if a large category of things is collectively referred to, it can be described using the concept of category. A class of things such as birds, rodents and classes that share the same properties and behaviors is called a class. A class is an abstract name for things in the world, and an object is the corresponding entity of this thing.
In java, attributes are represented by variables, and behaviors are represented by methods.

class book{
    
    
    double price;
    String name;
    int year;//属性

    public String getName() {
    
    
        return name;
    }
}

In the example, a simple book class abstracts the characteristics of the book, the specific price, name, and year attributes of a book, and the method corresponds to obtaining the name of the book instance object.

Member variables
Attributes in java are also called member variables. In this example, the member variables are name, year, and price. There is no big difference between a member variable and an ordinary variable. It can be assigned an initial value or not. If no initial value is assigned, the compiler will also give it a default value.
Member method
In this example getName()
defines the syntax of the member method as follows

权限修饰符 返回值类型 方法名(参数类型 参数名)
{
    
    
....//方法体
return 返回值;
}

Member methods can also have parameters, which can be objects or basic variables, and methods can also have different options that require or do not need to return a value. If you need to return a value, you can use the return keyword in the method body. After using it, the method will terminate execution.

Permission modifiers The
permission modifiers in java mainly include private, public, and protected. These modifiers control access to classes and member variables and member methods of classes.

The member variables modified by priva can only be used in this class, and are not visible in subclasses, nor are they visible to classes in other packages. If the access permission of a class is private, this class will hide all the data in it, and the user cannot directly access it.
Members modified by public can be used not only in this class, but also in subclasses and classes in other packages.
Protect modified member variables, subclasses of this class or other classes in this package can access member variables and member methods in this class.
insert image description here

3. Three major characteristics of object-oriented

Object-oriented programming has the following three characteristics
: 1. Encapsulation
Encapsulation refers to the encapsulation of the attributes and behaviors of objects, using classes as carriers. As customers, they do not need to understand the specific details of classes. It is the idea of ​​encapsulation. For example, when we use a fully automatic washing machine, we only need to press the buttons such as washing and drying, and the washing machine will run. As users, we do not need to understand the specific completion process.
2. Inheritance
There is a connection between classes and classes, and this connection is called association. The commodity category is related to the supermarket category, and the fast food category is also related to the toiletry category. Of course, there is a difference between association and association. Inheritance is a more classic relationship.
When we are solving some problems, we may think about whether we can keep some classes. For example, we want to study the use of a motorcycle. Motorcycles belong to the category of motor vehicles. We can modify the attributes and methods in the category of motor vehicles. Reuse, but the motorcycle also has its own characteristics, such as two wheels, need to wear a helmet, etc. At this time, we need to add the attributes and methods of the motorcycle class, for the previous ones such as refueling The method does not need to be written, which greatly improves the efficiency and saves time. The simplicity of the program is improved and the probability of errors is reduced.
The key to inheritance is to find the common characteristics between the research objects, such as quadrilaterals, squares, rectangles, and quadrilaterals. The common feature between them is that they have four sides. The parallelogram line is an extension of the quadrilateral, successfully utilizing the properties and behaviors of the quadrilateral.
3. Polymorphism
Polymorphism is also an important feature in face-to-face programming. Polymorphism has two meanings. The first meaning is the various forms of the operation name. For example, the same operation "bark", the same operation, there may be differences in form. For a cat to execute, it will issue "meow", and a dog will issue "woof". . The polymorphic feature on the operation name can pass different messages to the operation, so that the object can produce different behaviors according to the corresponding messages.

Guess you like

Origin blog.csdn.net/qq_45742383/article/details/114370119