Summary of java class and object content

1. The difference between object-oriented and process-oriented:

Process-oriented: It is a process-centric programming idea, which is the idea of ​​solving problems step by step in accordance with the process, which is more suitable for solving simple problems. For example, how to drive, we will think about what to do in the first step and what to do in the second step. . . . . . This is a typical process-oriented way of thinking.
Object-oriented: Object-oriented is a way to understand and abstract the real world. Object-oriented programming ideas are more suitable for solving more complex problems (three characteristics of object-oriented: inheritance, encapsulation, and polymorphism). For example, how to build a car, this problem will be more troublesome, starting from the process will be very complicated, and at this time we can consider to make car parts such as engine, tires. . . . . . This is a typical object-oriented way of thinking.

2. The relationship between objects and classes:

A class is an abstract concept of an object, and an object is a concrete manifestation of a class. In short: a class is an abstraction of an object, and an object is a concrete instance of a class.

3. Class writing:

A general class mainly contains the following sections: 1. Member variables (also called class attributes) 2. Common methods. 3. Constructor.

class dog {
    
    
	//成员变量
	int age;
	String name;
	//方法
	void shout() {
    
    
		System.out.println("汪汪汪!");
	}
	//构造器
	dog(int age,String name){
    
    
		this.age=age;
		this.name=name;
	}
}

4. Detailed construction method:

If no constructor is written, the compiler will automatically generate a constructor for us. But if you write a constructor yourself, the compiler will not automatically generate it for us. (Note: The method name of the constructor must be consistent with the class name).

Overloading of construction methods: Construction methods are also methods, the same as ordinary method overloading.
The method name is the same, but the parameter list is different.

class dog {
    
    
	//成员变量
	int age;
	String name;
	//方法
	void shout() {
    
    
		System.out.println("汪汪汪!");
	}
	//构造方法的重载
	dog(){
    
    
		
	}
	
	dog(int age){
    
    
		this.age=age;
	}
	
	dog(String name){
    
    
		this.name=name;
	}
	
	dog(int age,String name){
    
    
		this.age=age;
		this.name=name;
	}
}

5.Keyword this

The essence of this is "the address of the created object"! Because the object has been created before the constructor is called. Therefore, this can also be used to represent the current object in the construction method.

The most common usage of this:
1. When the program produces ambiguity, this should be used to refer to the current object.
2. Use the this keyword to call the overloaded construction method to avoid the same initialization code.
3. This cannot be used in static methods.

6. Keyword static

In a class, member variables declared with static are static variables, also called class variables (attributes or methods modified by static belong to the class, and ordinary variables and methods belong to the object). It has the following characteristics:
1. It is a public variable of the class, belongs to the class, and is shared by all instances of the class (for example, humans have 2 ears, and such shared properties are generally declared as static variables).
2. Generally use "class name. method/attribute" to call. It can also be referenced by object.
3. Non-static members cannot be accessed directly in the static method.

7. Inheritance and application of instanceof

Inheritance makes it easier for us to extend the class, using the keyword extends.
Key points of inheritance:
1. The child class inherits the parent class, and can get all the properties and methods of the parent class (except the construction method of the parent class), but it may not be directly accessible (such as the private properties and methods of the parent class).
2. There is only single inheritance in Java, but multiple inheritance for interfaces.
3. If you define a class without calling extends, its parent class is: Java.lang.Object.

Instanceof can be used to determine whether an object is an object created by a class or subclass.

8. Method rewriting

Subclasses can override the methods of the parent class and replace the methods of the parent class with their own behavior.
The rewriting of the method needs to meet the following three points:
1. The method name and the parameter list are the same.
2. The return value type subclass is less than or equal to the parent class.
3. The access permission subclass is greater than or equal to the parent class.

9.== and equals

"==" means to compare whether the two parties are the same. If it is a basic type, it means that the value is equal, if it is a reference type, it means that the address is equal and it is the same object.
equals: The method defined in the Object class provides the logic to define "object content is equal". (And it may not be the same object but the contents of the two objects are equal, the equals method often needs to be rewritten in actual applications)

10.Super keyword (parent class object reference)

super is a reference to the direct parent object. You can use super to access the methods or attributes in the parent class that are covered by the subclass.

11. Use of encapsulation

Specific advantages of encapsulation in programming:
1. Improve code security.
2. Improve code reusability.
3. "High Cohesion": Encapsulate details to facilitate modification of internal code and improve maintainability.
4. "Low coupling": Simplify external calls, facilitate the use of callers, and facilitate expansion and collaboration.

Encapsulated realization (using access control symbols):
1.private: means private, only your own class can access.
2. Default means that there is no modifier modification, and only classes of the same package can be accessed.
3. Protected means that it can be accessed by classes in the same package and subclasses in other packages.
4. Public means that it can be accessed by all classes in all packages of the project.

Encapsulation of the class:
1. Private access permissions are generally used.
2. Provide corresponding get/set methods to access related attributes. These methods are usually publicly modified to provide attribute assignment and read operations.
3. Some auxiliary methods that are only used in this class can be decorated with private, and it is hoped that methods called by other classes will be decorated with public.

12. Polymorphism

Polymorphism refers to the same method call, which may have different behaviors due to different objects.

The main points of polymorphism:
1. Polymorphism is method polymorphism, not attribute polymorphism (polymorphism has nothing to do with attributes).
2. The existence of polymorphism requires three necessary conditions: inheritance, method rewriting, and parent class references pointing to subclass objects.
3. After the parent class reference points to the subclass object (different subclass objects will produce different results), use the parent class reference to call the subclass override method, and then polymorphism appears.

13.final keyword

1. Modified variable: The variable modified by it cannot be changed. Once the initial value is assigned, it cannot be reassigned.
2. Modification method: This method cannot be overridden by subclasses, but can be overloaded.
3. Modified class: Modified class cannot be inherited.

14. Abstract methods and abstract classes (using the abstract keyword)

Abstract method: This method has no method body, only declaration. What is defined is a "standard", which tells subclasses that they must provide concrete implementations for abstract methods.

Abstract class: A class that contains abstract methods is an abstract class. Through abstract classes, we can strictly limit the design of subclasses and make the subclasses more versatile.

Key points for using abstract classes:
1. Classes with abstract methods can only be defined as abstract classes.
2. Abstract classes cannot be instantiated, that is, abstract classes cannot be instantiated with new.
3. The abstract class can contain attributes, methods, and construction methods. But the constructor cannot be used for new instances, it can only be used by subclasses.
4. Abstract classes can only be used to be inherited.
5. Abstract methods must be inherited by subclasses.
6. The abstract class can contain general methods.

15. Interface (keyword interface)

All methods in the interface are abstract methods.

A detailed description of the defined interface:
1. Modified visitor: only public or default.
2. Interface name: use the same naming mechanism as the class name.
3.extends: The interface can be inherited multiple times.
4. Constants: The attributes in the interface can only be constants and are decorated by public static final by default.
5. Methods: The methods in the interface are modified by public abstract by default.

Key points:
1. Subclasses implement the specifications in the interface through implements.
2. The interface cannot create an instance, but it can be used to declare the type of a reference variable.
3. A class implements an interface, all methods in the interface must be implemented, and these methods can only be public.

16. Classification of internal classes

Inner classes are divided into member inner classes (non-static inner classes, static inner classes), anonymous inner classes, and local inner classes.

Non-static inner classes (the use of non-static inner classes in outer classes is no different from using other classes):
1. Non-static inner classes must be registered in an outer class object. The non-static inner class object belongs solely to an object of the outer class.
2. Non-static inner classes can directly access members of outer classes, but outer classes cannot directly access non-static inner class members.
3. Non-static inner classes cannot have static methods, static properties, and static initialization blocks.
4. Static methods of external classes, static code blocks cannot access non-static internal classes, including non-static internal classes to define variables and create instances.
The key points of member variable access:
1. Local variable of method in inner class: variable name.
2. Inner class attribute: this. variable name.
3. External class attribute: external class name.this.variable name.

Static inner class:
Use points:
1. When a static inner class object exists, there is not necessarily a corresponding outer class object. Therefore, instance methods of static inner classes cannot directly access instance methods of outer classes.
2. The static inner class is regarded as a static member of the outer class. Therefore, the method of the external class can access the static members of the static internal class by means of "static internal class name. name", and access the instance of the static internal class through the new static internal class ().

Anonymous inner classes (suitable for classes that only need to be used once):

Syntax:
new parent class constructor\implementation interface() { //anonymous inner class body }

Note:
1. Anonymous inner class has no access modifier.
2. There is no construction method for anonymous inner classes. Because it doesn't even have a name.

Local internal classes: (Because local internal classes are rarely used in practical applications, the author will not introduce too much here).

Guess you like

Origin blog.csdn.net/ABded/article/details/106358879