Java Basics - Object Oriented

1. Basic knowledge of java

1. Object-oriented features: encapsulation, inheritance, polymorphism

2. The difference between member variables and local variables

(1) The scope is different, the member acts on the class, and the local acts on the method or statement (for )

(2) The location in the memory is different, the member exists in the heap memory, and the local exists in the stack memory.

3. Anonymous objects

(1) Anonymous objects are a simplification of objects

(2) Anonymous object usage scenarios

        Modifying anonymous object properties has no meaning and will be collected by the garbage collector later.

        An anonymous object's method will only be used once

        Anonymous objects can be passed in as actual parameters

4. Construction method

If a class does not have a constructor, the system will assign it a parameterless constructor by default. But once you write the constructor yourself, the system will not assign the constructor to the object. But to be on the safe side, it's better to write a parameterless constructor in every object.

5. The difference between constructor and construction code block

The constructor is to initialize the corresponding object, and the construction code block is to initialize all the objects uniformly.

6.this use

(1) What does this represent? Which object is calling the function where this is located, and this represents which object.

(2) this is used to distinguish the case where the member variable and the local variable have the same name

(3) The default call between functions in a class is to use this. function name

(4) this represents the mutual call between constructors, and this can only appear in the first line of the constructor. Ordinary methods cannot call constructors through this

(5) this represents the memory area

7.static

(1) static is a modifier used to modify members

(2) static represents the method area and the shared area

(3) static is loaded with the loading of the class, and static is better than the existence of objects. Common to all objects, called by class name.

(4) Static methods can only access static members, and static methods cannot use the keywords this and super

(5) Advantages of static: object sharing, space saving

(6) Disadvantages of static: The life cycle is too long, the access is limited, and only static ones can be accessed.

(7) Typical use of static: tool class, each method in it is static, and is called by the class name

8. Static code blocks are loaded with classes

9. Static construction code blocks , static constructors , construction code blocks , execution order of constructors

Static Constructor Block > Static Constructor > Constructor Block > Constructor

10. What does Person p = new Person("zhangsan", 20) do?

Load class (.class)-- static properties or member methods into the method area -- load static code blocks -- open up memory space -- default initialization of member properties -- execute non-static construction code blocks -- initialization of constructors

2. Inheritance

(1) Improve code reusability. There is a relationship between classes and classes, resulting in polymorphism.

(2) Java supports single inheritance and does not support multiple inheritance

(3) this represents this class, and super represents the parent class. super appears on the first line in the constructor.

(4) The subclass inherits the parent class, and the member variables and functions will be inherited (whether it is static or not). The same name can be overridden. After overriding, the properties and methods of the subclass are called by default. Using super can call the properties and methods of the parent class. Static can only override static. There is no override for the constructor.

(5) When calling the constructor of the subclass, the constructor of the parent class will be called first

(6) The access rights of subclasses cannot become smaller, and the exception thrown cannot become larger

3. Abstraction

(1) There can be constructors in abstract classes, but objects cannot be created with new

(2) There can be no abstract methods in an abstract class

(3) Abstraction is expected to be implemented by subclasses, so there can be no static, final, private modification

4. Interface

(1) There are only constants and abstract methods in the interface, no constructors

(2) Interfaces can have multiple inheritance

5. Polymorphism

(1) Improve the scalability of the program

(2) Fu fu = new Fu(); Fu fu2 = new Zi();

(3)Fu f = new Zi();

Member variables are viewed on the left at compile time or at run time. The method looks on the left at compile time and on the right at runtime.

6. Internal class

(1) Inner classes can directly access members of outer classes, including private ones. Because holding a reference to the outer class: outer class name.this

(2) To access the inner class, the outer class needs to create an object of the inner class

(3) Use non-static inner classes in main: Outer.Inner in = new Outer().new Inner()

        Use static inner class in main: Outer.Inner in = new Outer.Inner()

Example of use:

public class Test{
	public static void main(String[] args) {
		//The inner class cannot be used directly, because generally non-static objects exist according to the outer class
		//The following is the format used: the left is the full name of the inner class, and the right indicates that only objects of the outer class can have objects of the inner class
		Outer.Inner in = new Outer().new Inner();
		in.function();
	}
}
class Outer{
	private int num = 2;
	//private class Inner{Inner classes can be privatized
	class Inner{
		void function(){
			//The inner class directly accesses the data of the outer class, whether it is private or not
			System.out.println("num = "+num);
			method();
		}
	}
	public void method(){
		//The outer class needs to create an object of the inner class to access the inner class
		Inner in = new Inner();
		in.function();
	}
}

(4) If a static member is defined in an inner class, then the inner class must be static. But static inner classes do not necessarily have static members. But the inner class does not need to be static when the inner class has static final modification is constant.

(5) Use of private, static and final

As long as it is in the member position, including the member position of the inner class, these three modifiers can be used. final can modify local, constructors can only use private, and classes can only use final.

(6) When a local inner class accesses a local variable, the local variable can only be final

1. public class AnonymousClass {  

2.     public Object makeInner(String localVal){  

3.         return new Object(){  

4.             private String s = localVal;  

5.             public String toString(){  

6.                 return s;  

7.             }  

8.         };  

9.     }  

10.     public static void main(String[] args) {  

11.         AnonymousClass ac = new AnonymousClass();  

12.         Object ob = ac.makeInner("23");  

13.         System.out.println(o.toString());  

14.     }  

15. }  

      The program creates a new class object, then calls the makeInner method, passes a String type parameter, enters the method, and returns an internal anonymous class. The member reference variable s in the anonymous class points to the same memory address as the parameter localVal, and in the overridden toString method, the member variable s is returned. At the end of the call, the memory space pointed to by localVal is released, return to the main program, call the toString method of ob, and output s, but the memory address pointed to by s has been released at this time, so the program will not let you do this. So why the parameters or local variables of the method where the inner class accesses must be final, because their space is not released after the method ends, and it is still valid.


















Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324874091&siteId=291194637