Java: final and inner classes

Up and down transformation of objects

Upward transformation :

父类 对象名 = new 子类();

Upward transition must be safe (small range -> large range)

Downcast :
restore upcast (similar to forced conversion) (can only be restored to the original subclass object)

子类 对象名 = (子类)原本父类对象;

(Reverting to other subclass objects will not report an error, but there will be a class conversion exception at runtime)

instanceof :
Know which subclass a parent class refers to (safely cast down)

对象 instanceof 类型;//得到一个布尔值

final keyword

Represents the final, immutable
class

public final class{
    
    };
//当前类不能有任何子类,也不能进行覆盖重写

method

public class 父类{
    
    
	public final 返回类型 方法名(参数){
    
    };
}//子类中不能对此方法进行覆盖重写

The final and abstract keywords of the class and method cannot be used at the same time (contradictory)

Local variable

final 数据类型 变量名 = 初始值;
//此变量只能赋值一次,一次赋值,终身赋值

For reference types, it means that the address value is immutable.
For class objects, it cannot be new again (member variables are variable)

Member variables

public class 类名{
    
    
	public final 成员变量 = 初始值;}
//必须手动赋初始值,否则默认的0将被用作final值

The constructor must also be assigned an initial value, the Setter function will report an error


Permission modifier

public > protected > (default) > private

public protected (default) private
The same class (itself) ☑️ ☑️ ☑️ ☑️
Same package (neighbor) ☑️ ☑️ ☑️
Different package subclasses (derived) ☑️ ☑️
Different packages are not subcategories (unfamiliar) ☑️

Inner class

One class contains another class

Member inner class : defined in the class

修饰符 class 类名{
    
    
	修饰符 class 类名;}

Free access for internal and external use, and internal objects for external use
(1. Create internal class objects in member methods)
(2. 外部类.内部类 对象名 = new 外部类(). new 内部类();)

The member variable of the inner class has the same name as the member variable of the outer class:

内部类中调用:外部类名称.this.外部类成员变量;

When assigning the value of the inner class to the outer class, you need to first create the object of the
 
inner class . Local inner class : defined in the member method

修饰符 class 类名{
    
    
	修饰符 返回值类型 方法名(参数){
    
    
		class 局部内部类名{
    
    }
		}}

You can only create an object in a method and use the
local internal class. If you want to access the local variable of the method, then the variable must be effectively final (you can not write final, but you cannot change the value) (local variables are on the stack, the class object In the heap, you need to ensure that the copied data remains unchanged)
 

Permission modifier rules when defining a class:
outer class: public/(default)
member inner class: public/protected/(default)/private
local inner class: cannot write, can only be used in methods (not defualt)

Anonymous inner class (belonging to local inner class)

The class name is omitted
, the implementation class of the interface or the subclass of the parent class only needs to be used only once

接口 对象名 = new 接口(){
    
    
//覆盖重写所有抽象方法};

An object of an anonymous inner class can only be used once after creation (use the definition implementation class multiple times)

Anonymous inner class + anonymous object

new 接口(){
    
    
//覆盖重写所有抽象方法}.调用方法;

An anonymous object can only call a method once


ArrayList & List

List is an interface, and ArrayList is an implementation class of List interface
ArrayList implements dynamic array


I finally finished learning Java~ The teacher is very humorous (and I really like Zhao Liying)
Sophomore year is also starting, although there is no atmosphere of class.
New semester rushes!! (๑•̀ㅂ•́)و✧

Guess you like

Origin blog.csdn.net/Rachel_IS/article/details/104490929