Detailed Java object-oriented programming basics (Java essential knowledge)

Java is an object-oriented programming language. Classes and objects are important concepts of object-oriented programming. In essence, classes can be regarded as the carriers of objects, which define the attributes and behaviors of objects.

Object-oriented overview

Object-oriented thinking is the most natural way of thinking for human beings. It abstracts all pre-processing problems as objects, and at the same time understands the corresponding attributes and behaviors of these objects, so as to solve some practical problems faced by these objects. The essence of object-oriented design The above is to model real-world objects.

Object

Object is an abstract concept, called "Object" in English, which means anything that exists. Usually the object is divided into two parts, namely the static part and the dynamic part. The static part is called an attribute, such as the gender of a person, and the dynamic part refers to the behavior of the object, such as a person can walk.

class

A class is a carrier that encapsulates the properties and behavior of an object. Conversely, a class of entities with the same properties and behavior is called a class. In the Java language, classes include the attributes and behaviors of objects. The properties of an object in a class are defined in the form of member variables, and the behavior of an object is defined in the form of methods.

Features of object-oriented programming

Encapsulation

Encapsulate the properties and behaviors of the object. The carrier is the class. The class usually hides its implementation details from the customer. This is the encapsulation idea. For example, when using a computer, you only need to use your fingers to tap the keyboard to achieve some functions without knowing the computer. How the interior works.

The idea of ​​encapsulation ensures the integrity of the internal data structure of the class. Users who use this class cannot easily directly manipulate this data structure, but can only manipulate the data that the class allows to expose. This avoids the influence of external operations on internal data and improves the maintainability of the program.

inherit

It can be said that the instances of the subclass are all instances of the superclass, but it cannot be said that the instances of the superclass are the instances of the subclass. Inheritance is an important means to achieve reuse. Through inheritance, subclasses reuse the properties and behaviors of the parent class while adding the unique properties and behaviors of the subclass.

Polymorphism

The feature of applying the parent class object to the subclass is polymorphism. While the subclass inherits the characteristics of the parent class, it also has its own characteristics and can achieve different effects. This is the structure of polymorphism.

Class and object

Member variables

The properties of objects in Java are also called member variables, and the definition of member variables is the same as that of ordinary variables.

数据类型 变量名称 [=];

The defined variable can be assigned a value or not. If the initial value is not set, there will be a default value.

public class Test30 {
    
    
	// 实例成员变量
    private boolean bool;
    private byte b;
    private short s;
    private char c;
    private int i;
    private long l;
    private float f;
    private double d;
    private String str;
    private String[] strArray;
 
    public void printInstance() {
    
    
        System.out.println("实例成员变量默认值:");
        System.out.println("boolean:" + bool);
        System.out.println("byte:" + b);
        System.out.println("short:" + s);
        System.out.println("int:" + i);
        System.out.println("long:" + l);
        System.out.println("float:" + f);
        System.out.println("double:" + d);
        System.out.println("String:" + str);
        System.out.println("String[]:" + strArray);
        System.out.println("char:" + c);
    }
    
    public static void main(String[] args) {
    
    
    	Test30 dv = new Test30();
        dv.printInstance();
    }
}

running result:

Member method

The member method corresponds to the behavior of the class object, and it is mainly used to define the operations that the class can perform.

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

If no permission modifier is specified when the method is defined, the default access permission of the method is the default (that is, it can only be accessed in this class and classes in the same package)

Member method parameters

When calling a method, you can pass one or more values ​​to the method. The value passed to the method is called the actual parameter, and the variable that accepts the actual parameter inside the method is called the formal parameter. Formal parameters are only valid inside the method. There are three main types of method parameters in Java, namely value parameters, reference parameters and variable length parameters.

Member methods are not static, but for testing purposes, all methods are static.

Value parameter

The value parameter table name actually participates in the transfer by value between the formal parameters. When the method of using the value parameter is called, the compiler will allocate storage units for the formal parameter, and then copy the value of the corresponding actual parameter to the formal parameter. The transfer of value types, so the modification of the formal parameters of the value type in the method does not affect the actual parameters.

Reference parameter

If the parameter type is an array or other reference type when passing a parameter to the method, then the modification of the formal parameter in the method will be reflected in the original array or other reference type. This type of method parameter is called Reference parameters.

Variable length parameter

When declaring a method, if there are several parameters of the same type, they can be defined as indefinite length parameters. There are three dots between the parameter type and the parameter name, not other quantities or ellipsis.

Construction method

In addition to member methods in a class, there is also a special type of method, that is, the construction method. The construction method is a method with the same name as the class, and the creation of the object is done through the construction method. Whenever an object is instantiated, the class will automatically call the constructor for you.
The characteristics of the construction method are as follows: the
construction method has no return type, and cannot be defined as void.
The name of the constructor must be the same as the name of this class.
The main function of the construction method is to complete the initialization of the object. It can pass the parameters that define the object to the object members.

The syntax is as follows:

class Book {
    
    
	public Book() {
    
     
	}
}

If the constructor is not clearly defined in the class, the compiler will automatically create a default constructor without parameters. If there are all parameter construction methods in the class, the compiler will not automatically generate a default no-parameter construction method for the class.

Local variable

If you define a variable in a member method, then this variable is called a local variable.
Local variables are created when the method is executed, and destroyed at the end of the method. Local variables must be assigned or initialized when they are used, otherwise a compilation error will occur.

Class member variables and member methods can be collectively referred to as class members. If a method contains a local variable with the same name as a member variable, the access to this variable in the method is based on the value of the local variable.

Local variables with the same name and type can be defined in areas that are not nested with each other.

this keyword

If a parameter with the same name as a local variable appears in the method, the method cannot directly use the member variable.

In the Java language, it is stipulated that the this keyword is used to represent the reference of this class of object, and the this keyword is implicitly used to refer to the member variables and methods of the object.
Insert picture description here
In addition to calling member variables or member methods, the this keyword can also be used as the return value of the method.
Insert picture description here

This can not only call the member variables and member methods of the class, in addition, he can also call the constructor of the class.

static keyword

Variables, constants, and methods modified by static are called static variables, static constants, and static methods, respectively, and are also called static members of the class.

static keyword

In a Java program, if the shared variable is decorated with static, then the variable is a static variable.
The syntax for calling static variables is as follows:

类名.静态类成员

Static method

If you want to use the member methods in the class, you need to instantiate the class first, but sometimes when you don’t want or cannot create objects of the class, you have to call the methods in the class to complete the business logic. In this case, you can Use static methods.
The static method syntax of the calling class is as follows:

类名.静态方法();

Static code block

In addition to the member methods of the class, the code area decorated with static can be called a static code block. Define a static code block to complete the initialization of the class, and it will run when the class is declared.

public class StaticTest {
    
    
	static {
    
    
			// 此处编辑执行代码
	}
}

Similar to static, interview written exams often appear

The main method of the class

The main method is the entry point of the class. It specifies where the program starts and provides control over the flow of the program. The Java compiler executes programs through value methods.

The main method syntax is as follows:

public static void main(String args[]){
    
    

}

The main method is static, so if you want to call other methods directly in the main method, it must be a static method.
The main method has no return value
. The formal parameters of the main method are arrays, where arg[0]-args[n] respectively represent the first parameter to the n+1th parameter of the program.

Difficulties

Local variables and global variables

In fact, all variables are local variables, but they are called differently based on the effective scope of the variables, such as non-static global variables and static variables. The effective scope of non-static global variables is the entire class body, and these global variables will be destroyed at the same time when the class is destroyed. The scope of the static variable is the life cycle of the entire program, and the static variable will be destroyed only after the program ends.

Guess you like

Origin blog.csdn.net/weixin_43888891/article/details/113488671