Java notes: Java object oriented

1、 Method

1. Overview

Methods can also be called functions. In other languages, the concepts and syntax of methods and functions may be different. For example, functions in Python can be independently defined and exist in files, and square rules are functions defined in classes. However, in Java, methods and functions refer to the same syntax and are the same, which can be called methods, It can also be called a function. The following points should be noted:

2. Definition method

The syntax is as follows:

Modifier list: This is optional and not required. If you do not write, the default option is used. For access control characters, such as public and private, the default access control permission is within the package scope.

Return value type: use "return value" to return a value. The value type must be consistent with the specified return value type. The specified return value type can be any data type in Java, including basic data types and all reference types. If this method does not return any value, it must be specified as void, which means that this method does not return any value, that is, there can be no statements such as "return value" in the method body, but "return;" Indicates that void is returned.

Method name: follow the definition rules of identifiers, but you should pay attention to the following points:

Formal parameter list:

Method call: use dot "." Call, but note that the program in the method body will be executed only when called, and will not be executed when defined or compiled.

3. Static method call

If there is static in the modifier list, it is called a static method. The syntax format of calling this method is "class name. Method name (actual parameter list)". However, if you call the static method in this class, you can omit the class name and call it directly with the method name.

4. Transfer of parameter values

When a method is called, The parameter passed to the method is the value of the variable (i.e. value transfer), not the variable itself, because if the variable itself is passed, it can be used in the called method, but the actual situation is that the called method can only use the local variable in its own scope, not the variable in the scope of its caller. Therefore, the value of the variable is passed, so Make the formal parameters of the method have their own values.

5. Variable length parameter

The variable length parameter of a method means that when defining a method, you only need to define the type of the parameter without writing the number of parameters of this type. You can also pass in different numbers of parameters as needed.

6. Method overload

Overload principle: when the overloaded methods have similar functions, method overload can be considered, but when the functions are different, try to use methods with different method names.

Overload mechanism:

7. JVM memory allocation

If you only define a method and do not call it, it will not be executed, and the memory required for the operation of the method will not be allocated in the JVM. The memory space required for the method will be dynamically allocated only when calling the method. There are three main blocks of memory space in the memory partition of the JVM (of course, there are other memory spaces):

8. Method recursion

Method recursion means that the method calls itself in the method body. The following points should be paid attention to when using the method recursion.

2、 Classes and objects

1. Encapsulation and class definition

Class mainly describes the state (property) and action (method) of the object.

Class is also the syntactic embodiment of the "encapsulation" feature in object-oriented programming. The advantages of encapsulation feature usually include the following:

The definition syntax of a common class is as follows:

Member variables: variables outside the class body and methods are called member variables. If the member variable is not assigned manually, the system will automatically assign the default value (everything is in line with "0"). The member variables are divided into:

Note: a class is also a data type and belongs to a reference data type. Its type name is the corresponding class name.

2. Object creation and memory allocation

Object is the concrete individual after class instantiation. The process from class to object is called instantiation. Conversely, the process from object to class is called abstraction.

New keyword: Java uses the new keyword to create an object. The new keyword is also an operator in Java.

Memory allocation: when the code in the memory of the method area is executed, a memory space corresponding to the method will be opened in the stack memory. When an object is created by using the new keyword during method execution, a memory space corresponding to the object will be opened in the heap memory. Therefore, the local variables defined in the method are in the stack, and the created objects are in heap memory. Each instance object has its own memory space, that is, 100 objects will allocate 100 memory space.

Pointer shielding: if you want to access the data in the heap memory in Java, you must use reference instead of directly operating the heap memory. Because the concept of pointer is shielded in Java, you can't directly access or operate the data in memory through pointer.

Access attribute: for instance variable attribute reading and modification, use the syntax format "reference. Variable name" to read, and use the syntax format "reference. Variable name = value" to modify the attribute. Note that instance variables are stored in the corresponding instance object in heap memory and cannot be accessed by class name.

3. Null pointer exception NullPointerException

When the value of a variable of reference type no longer points to the memory address of an object, but is null, a null pointer exception will occur when accessing the related properties or methods of the object, because the variable no longer points to the object, but the value is null, so the object cannot be accessed, let alone the properties and methods in the object, Null pointer exceptions are bound to occur when null references access instance related data.

4. Get method and set method

Attribute privatization: in the encapsulation feature, all attributes in the class should be decorated with the private modifier. Private means private, which means that this attribute can only be accessed in this class and cannot be accessed outside the class. However, in the class, some simple public operation entries should be provided for external access to these properties, such as the corresponding get method and set method.

The get method and set method are written as follows:

Example:

Note: get and set methods do not have static modifier, but use public modifier. The access method of methods without static modifier is "reference. Method name (argument)".

5. Passing of reference parameters

Object variables are usually called references, because in the stack, this variable is only a local variable, and the value of the object variable is the memory address of the object in heap memory. Of course, this memory address points to the object instance in heap memory. Therefore, for basic data types, the value passing will not affect the value of the original variable, but for class instances, because the passed value is the memory address, Therefore, although it will not affect the value of the original local variable (i.e. memory address), if you modify the object instance in the memory address, it will affect the instance object pointed to by the memory address, that is, the instance object pointed to by the original local variable will be modified.

6. Construction method (constructor)

The syntax is as follows:

Example:

The call of the constructor method: the construction method is to create objects and initialize the values of instance variables by calling the constructor method, and the invocation of the method uses the new keyword "new to construct the method name (argument list)". Note that after new is actually calling the method name instead of the class name, but because the two are the same. Therefore, it may be mistaken for calling the class name.

Construction method return value: Although no return value type is specified, the return value type of construction method is the type of its class, and the return value is the reference of the newly created object. However, note that this return value does not need to be written manually by developers, that is, in the definition of construction method, the return value type and return value do not need to be defined manually.

Default constructor: when there is no constructor defined in the class, the system will provide a parameterless default constructor for the class. It should be noted that if a construction method is provided in a class, the system will no longer provide the default parameterless construction method for this class. Therefore, if its own construction method is provided in a class, it is recommended to manually add the parameterless construction method, because this construction method is too common.

As for the construction method, the following points should also be noted:

7. This keyword

In fact, each instance object has a this variable, which stores the memory address of the instance object where it is located, that is, this is a variable of reference type pointing to the instance object itself. It can be understood in another way that this can appear in the instance method, and this in the method represents the instance object currently executing the method action.

Access to the instance variable in the instance method can be accessed without using the this keyword because it is an instance variable, so this can not be written in most cases. This is mainly used to distinguish instance variables from local variables, such as setter methods and construction methods.

Of course, this cannot be used in a method with a static modifier.

This keyword not only uses "this. XXX" to indicate the use of instance objects, but also can call another constructor of this class in the form of "this (argument list)" in the constructor. Note that, When using this method, this statement can only appear in the first line of the construction method (of course, other statements can be added after this statement, but there can be no other statements before it), such as:

8. Super keyword

Super keyword and this keyword have many similarities in usage, but this represents the current instance object, while super represents the characteristics (including properties and methods) of the parent class of the current subclass. They are usually used to access some properties and methods of the parent class. Compared with this, their similarities are as follows:

For the usage of super (), when the first row of a subclass construction method has neither this nor super (), there will be a super () execution by default, which means that the method of constructing the parent class without reference can be called in the construction method of the subclass. We must ensure that the parent class must have a method of constructing no reference. It is recommended to manually write a construction method without reference in the definition of the class. Of course, if you manually call this (argument list) or super (argument list), the program will call as you write. Examples are as follows:

For the use of super, please pay attention to the following points:

9. Inherit

Advantages of inheritance: the most basic role of inheritance is code reuse, but the most important role is that with inheritance, there is method coverage and polymorphism mechanism.

Single inheritance: the inheritance mechanism in Java only supports single inheritance. A class cannot inherit multiple classes at the same time, but only one class. The syntax is as follows:

Data that can be inherited:

Multiple inheritance: although only single inheritance is supported in Java, multiple inheritance can be realized indirectly:

Default base class: if a class in Java does not explicitly inherit any class, the class inherits the Java. Net class provided in the javase library by default Lang.Object class.

It should be noted that when a subclass inherits a parent class, at runtime, it does not mean to find the corresponding method or property in the subclass, and the subclass does not find it in the parent class, but when defining, if it inherits a parent class, the definition of this class contains some methods and properties inherited from the parent class, That is, the methods and properties executed by subclass objects are always their own properties and methods.

10. Method override

Method overrides are also called method overrides. Subclasses rewrite the methods inherited from the parent class, which is called method overrides. When overriding methods, you should pay attention to:

11. Polymorphism

Upcasting: subtype -- > parent type, which can be understood as automatic type conversion. Downcasting: parent type -- > child type, which can be understood as forced type conversion. Between classes, whether it is up conversion or down conversion, there must be inheritance relationship, otherwise the compilation will not pass.

Polymorphic syntax mechanism: the reference of the parent type points to the sub type object. This mechanism leads to two different forms or states in the compilation stage and running stage of the program. This mechanism can be called a polymorphic syntax mechanism.

The function of polymorphism: reduce the coupling degree of the program and improve the expansion force of the program. If you can use polymorphism, use polymorphism more, that is, the parent type reference points to the child type object. The core idea of polymorphism: oriented to abstract programming, try not to oriented to concrete programming.

Example: focus on notes

12. Instanceof operator

Syntax: "reference instanceof data type name". The return value is true / false. True means that the real memory object pointed to by this reference is the object of this data type. False means that the real memory object pointed to by this reference is not the object of this data type. In the example above, "animal bird1 = new bird();" Although bird1 of is converted to animal type, its real memory object is actually bird type, so if "bird1 isinstanceof bird" is executed, it will return true. In the Java programming specification, when performing forced type conversion, it is recommended to use the instanceof operator to judge the type of reference before conversion.

13. Abstract class

Abstract classes are decorated with the abstract keyword, which is a class formed by the extraction of common features between classes. Usually, abstract classes contain abstract methods, but it does not mean that abstract methods must be defined in abstract classes. For the definition of an abstract method, it should be noted that it also needs to be modified by the abstract keyword. At the same time, it cannot have curly braces and must end with a semicolon.

Abstract classes also belong to a reference type. Abstract classes are used to define the objects of a subclass. This syntax is a polymorphic application, that is, upward transformation. The reference of the parent type points to the objects of the subclass.

Example:

When using abstract classes, you should pay attention to the following points:

14. Interface

Although the interface can be understood as a class when learning, note that the interface is not a class. The keyword used in the definition is interface instead of class, but it is also a class bytecode file after compilation.

At the same time, like an abstract class, an interface is also a reference type. When using an interface, polymorphism can be used, or the use of an interface is inseparable from polymorphism, because the interface itself cannot directly create an object. Once an object is created, it must be a "subclass" of the interface, that is, an upward transformation, and the reference of the parent type points to the object of the subtype.

Example:

The following points should be paid attention to when using the interface:

15. The difference between abstract classes and interfaces

In fact, in actual use, more interfaces are used and less abstract classes are used. There are many similarities between abstract classes and interfaces, but there are still many differences between them, as follows:

3、 Modifier

1、static

Static variable: a variable with static keyword is called a static variable, and the static variable starts to initialize when the class is loaded. Its memory has been opened up without creating an object, and is stored in the method area.

Static methods: methods with static modifier are called static methods. In static methods, instance variables and instance methods cannot be accessed. Of course, this keyword is also included, but only variables (static variables) with static modifier can be accessed.

Usage principle: when the execution of a method or variable is independent of the specific object, or all objects will use the method or variable, and will not change due to different objects, it should be defined as static type. When an object needs to participate in the execution of a behavior or action, or the results of different objects executing the action may be different, this method should be defined as an instance method and should not add the static keyword. Similarly, when an attribute may be different in different objects, the attribute should be defined as an instance variable, and the static keyword should not be added.

Accessing static variables and methods: methods and variables with static can be accessed by class name or by reference, but access by reference is essentially accessed by class name, because you will find that static methods and variables can also be accessed when the reference is null without null pointer exception, Therefore, it is not recommended to use the static method by reference.

The use of another syntax of static: static code block. This method will be executed when the class is loaded. The definition and use examples are as follows:

2、final

The following points should be noted when using the final keyword:

If an instance variable is declared as a final variable, it needs to be assigned a value at the same time, otherwise an error will be reported. If the instance variable of a class has not been assigned after calling the constructor, it will be given a default value by the system, and the final variable cannot be re assigned. Therefore, if the final instance variable is allowed to be declared without assignment, the variable will always be the default value of the system. This is certainly not possible, Therefore, the syntax requires that the instance variable of final must be manually assigned or assigned in the constructor at the same time of declaration. Examples are as follows:

Note that although the final modified reference can no longer point to other objects, the internal memory of the pointed object can be modified.

The instance variable modified by final is usually used in conjunction with static and is called a constant.

3. Constant

Syntax format: "public static final type constant name = value". Final means that it cannot be modified. Static means that no matter how many objects are instantiated, only one copy of data will be saved in the memory of the method area. At this time, even if it is declared as public, there is no need to worry about being modified by others, because it is final and cannot be modified. In the Java specification, all constants must be capitalized and underlined between words.

4. Access control permission modifier

The following four types of attributes and methods can be used, but for classes, only public and default methods can be used. However, the scope of action of these four types of attributes and methods is the same whether they are classes or attributes and methods:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>