Konjac's Java entry journey-facing the object (1)

Novice, welcome to correct me
–lx

Face the object

——“Java是一种纯面向对象的高级编程语言。”

Abstract
In the process of programming in Java language, a series of programming goals need to be transformed into objects. The meaning of abstraction in books is: " abandon individual, non-essential issues or secondary features that have nothing to do with the subject of the research from the research object. " In short, it is to select what we need from all the attributes of the target object. If you need to write a program to calculate the height of the car, we only need to keep the attribute of the car height and its value-related attributes in the class created by the program.

Briefly talk about object-
oriented programming. I think the difficulty should lie in the establishment of object templates. This involves the issue of personality and commonality. Not all the programming goals we face are the determination of attributes that exist in reality like cars. Object. Most of the time, we need to find commonalities from programming requirements, identify individualities, and reasonably retain and discard them to abstract object templates that meet the programming goals-classes.

Classes and Objects After
understanding the meaning of abstraction, the relationship between classes and objects becomes clear.
If we only study the height and weight of people, then the class of "people" in programming should be as follows.
Insert picture description here
The class here is the "template" of a research object, which can also be called the commonality of the object. In reality, we can’t find two identical leaves, and the same is true for objects in programming. In addition to commonality, objects of the same species also have their own personalities (barrier tips: even if the objects are exactly the same data, these are for the device." "Twins" still have differences in reference images, such as memory footprint). At the same time, it can be seen that the object is the instantiation product of the class template, and the following lines of code can witness the "birth" of the class object "Zhang San".
Insert picture description here
Objects and Object References
In the Java language, pointers that were difficult to control for stability and security in C have been abandoned, but the concept of reference has been retained and developed. Compared to language C, references in the Java language seem to be "pointerized" on a certain level. References can communicate with objects through messages and have the function of "changing" objects, similar to a remote manipulation. To use an inappropriate analogy, if programming is a war, then the programmer is like a commander. The object is the hard-fighting soldiers on the front line, and the object reference is the correspondent between the commander and the front line.

People zhangSan;

The above line of code does not create the object of the class, but represents the creation of the object reference of the class. Currently, it does not have any pointers, and if the statement is defined in the method, its storage location will be in the stack.

zhangSan=new People(180,100);

The statement here creates an instantiated object for the People class, and makes the object reference "zhangSan" point to the object. So from a certain point of view, "Zhang San" is not "Zhang San", but a reference name used to refer to the instantiated object, just like everyone's name is not born with, but is given by the day after tomorrow. The name can be Refers to you, but can't replace you (a bit ridiculous, but it should be easy to understand).
If you add a line below the line of code above

zhangSan=new People(160,150);

At this time, "Zhang San" does not refer to the previous 1.8-meter thin guy, but cancels the original pointing and points to the newly built object. "Zhang San" is short and fatter , the original thin guy Has become a poor person without a "name", and will be automatically destroyed at the right time. (Cruel) And the above two objects are generated in statements such as in methods, and the objects they generate are stored in the heap, not in the stack like references .

Object initialization and parameter transfer
Basic data type parameter transfer: The
basic data type is only passed a value when passing parameters for the method, not the transfer itself. Modification of the formal parameters in the method does not change the original value.
Insert picture description here
Insert picture description here
In the show method, n is a formal parameter. Even if the ++ operation is performed, the original value n in main does not change.

Passing parameters of
class objects : When passing parameters, the class object itself is not passed, and the value is not copied to the formal parameters. The formal parameter here is only an object reference. When passing parameters, only the object reference is passed to Formal parameters make the formal parameters have the function of being able to "connect" the object—becoming a reference to the object.
Insert picture description here
Since the formal parameter is already an object reference at this time,
the operation of the object reference in the method will change the actual object data.
Insert picture description here
Object initialization: The
object needs to initialize data when necessary in the process of instantiating the class, such as the height and weight data in the above example. There are many ways to initialize, one is to initialize and assign values ​​to the class yourself, as follows (the construction method is not defined).
Insert picture description here

Here, the initial value of 1 is given to the data when it is defined. If we are not satisfied with the initial value defined for it at this time, we need to use the constructor to initialize it. The method name of the constructor must be exactly the same as the class name, and the method cannot have a return value.
Insert picture description here
Use "new class name (parameter);" to call:
Insert picture description here

When we do not initialize the definition as above, and there is no custom class construction method, the execution of the output statement still has the following results:
Insert picture description here
Insert picture description here
Although there is no artificial call to the construction method here, the system will configure the default construction method of the class when loading the class. To provide the initial value of the data in the preliminary category. In summary, the default construction method will be covered by the defined initial value, and the defined initial value will be covered by the calling construction method. So the order of initialization (sequence is not priority) is- 1. System default structure <2. Defined initial value <3. Defined construction method.

Non-access modifiers:
1.static:
static modifier, which can modify class methods and class variables, and the modified methods and data become class static methods and variables. Class methods and class variables without static modification are not exclusive to the class. Instead, the variables and methods belonging to the object are generated for the object while the object is instantiated. The method can only be called for the object. But after being modified by static, these variables and methods no longer rely on the instantiated object, and can be called directly by the class or called by the object (but the latter is not recommended in general). Therefore, when abstracting objects, some exclusive public and identical properties or methods belonging to the class can be modified with static. The main method must be modified with static, because the main method does not depend on the object.
Insert picture description here
The output result here is the initial value of height in the class 1:
Insert picture description here
At the same time, because the static modified method belongs to the class, the non-static modified class variable cannot be called, because the class variables of different objects are not deterministic.

2.final:
constant modifier, which can modify classes, class methods, class variables and ordinary variables. When modifying a class, the class becomes the final class and cannot be inherited.
Insert picture description here
At this time, the subclass student cannot be inherited, and the error is as follows: When
Insert picture description here
final modifies the class method, the class method at this time becomes the final method and cannot be overwritten by the subclass.

Insert picture description here

The error is as follows: The
Insert picture description here
modified class variable and the ordinary variable become constant, and the value cannot be changed.

Objects and arrays
In the Java language, arrays can also be regarded as a class (a special existence). We cannot specify the size of the array without creating an object. The declaration of array names is similar to the declaration of references.

float[100] arr;//此语句不可执行
//语句
float[] arr;
arr =new float[100];
//可以执行

Because the array needs to be regarded as a class, the float array here is an object of the class, so the number of class objects cannot be defined directly in the statement that generates the reference. Even if the object pointed to has been determined, it can only be created in the object The statement reflects the number of objects. For example
Insert picture description here
Insert picture description here
, for the array of class objects, the general rules are the same as the above-mentioned basic data types. However, because class objects also comply with the pure declaration statement (no new creation object statement) to create, only the class can be created Therefore, when creating an object array, create an array reference first, and then the objects in the created array object are references to the class object. You need to create the class object again to make the reference point to the class object to implement the operation of the object.
Insert picture description here
Insert picture description here

It cannot be output at this time because the object references in the array have no specific points. But when the array is a basic data type, the internal data will be automatically initialized when the array generates an object, and the initial value is 0, and you need to create the object yourself to reference the array. as follows:
Insert picture description here

Guess you like

Origin blog.csdn.net/HiphopHarris/article/details/108907699