As mentioned above, the Unit of the object-oriented program is the object, but the object is instantiated through the class, so the first thing we need to do is how to declare the class, it is easy to create a class. You only need to master the basic program syntax and definition rules. Where is the difficulty? How many classes and objects are used in a project,
As mentioned above, the Unit of the object-oriented program is the object, but the object is instantiated through the class, so the first thing we need to do is how to declare the class, it is easy to create a class. You only need to master the basic program syntax and definition rules. Where is the difficulty? How many classes and objects are used in a project,
As mentioned above, the Unit of the object-oriented program is the object, but the object is instantiated through the class, so the first thing we need to do is how to declare the class, it is easy to create a class. You only need to master the basic program syntax and definition rules. Where is the difficulty? How many classes are used in a project, how many objects are used, where classes are to be defined, what kind of classes are defined, how many objects are produced by this class instance, and how many attributes are contained in the class, how many methods are there, etc. This requires the reader to analyze and design the actual problems in actual development. Class Definition:
Class Name {}
Use a keyword class and a class name you want and a pair of braces. The structure of this class is defined. You only need to write code in it, but what is written in it? What can I write? How to write is a complete class? As mentioned above, the class is used to let it instance out of objects for us to use, so we need to know what kind of object you want, as we mentioned above, what is written on an installation configuration sheet is what you have installed on the machine. For example, if a person is an object, how do you recommend a person you are optimistic about to your leadership? Of course, the more detailed the better:
First, you will introduce the person's name, gender, age, height, weight, phone number, home address, and so on.
Then, you will introduce what this person can do. He can drive, speak English, and use a computer.
As long as you have a little introduction, others will have a little understanding of this person. This is our description of a person. Now, Let's sum up that all objects are similar to class descriptions, from the description above, we can see that a class is defined in two parts: static description and dynamic description, static descriptions are what we call attributes, such as the name, gender, age, height, weight, phone number, home address, and so on. It is also the function of the person's object on the fly. For example, when a person can drive, speak English, or use a computer, we can write dynamic functions or methods into a program, functions and methods are the same. Therefore, all classes are written in terms of attributes and Methods. attributes are also called member attributes of this class, and methods are called member methods of this class.
Class member {member attributes: name, gender, age, height, weight, phone number, home address Member method: can drive, speak English, can use computer}
Attribute:
The variable is declared by using the keyword "var" in the class definition, that is, the class attribute is created. Although the initial value can be given when the member attribute is declared, however, when declaring a class, it is unnecessary to give the initial value of the member attribute. For example, if you assign a person's name to "Michael", dozens of people will be generated using this class instance, these dozens of people are called James, so there is no need to. We can give the initial value of the member attribute after the object is output from the instance.
For example, var $ somevar;
Method (member function ):
The class method is created by declaring a function in the class definition.
For example:
The above is the declaration of a class, a class declared from the attribute and method, but the Member attribute should not be the initial value during the declaration, because our class is a descriptive information and will be used to instantiate objects. For example, if 10 objects are instantiated, the names and gender of these 10 objects are, the age is different, so it is best not to assign an initial value to the member attribute here, but to each object separately.
You can use the same method to create the desired class. As long as you can define the object as a class with the attributes and methods, You Can instantiate the object.
To enhance your understanding of the class, let's create a class with a wide range of shapes. Let's make a rectangle and analyze it first, what are the attributes of a rectangle? What are the functions of Rectangles?
Class rectangle {// The length of the attribute rectangle of the rectangle; the width of the rectangle; // the perimeter of the rectangle using the rectangle method; the area of the rectangle ;}
If you use this class to create multiple rectangle objects, each rectangle object has its own length and width, you can find its own length and area.