9, three modifiers

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_45536396/article/details/102736085

Three modifiers

A, static static

You can modify the properties, methods

1. static properties modified static properties (class variables)

Each member variable is an object held by each of a separate space, unilaterally change the object does not affect other objects

Here Insert Picture Description

a) define the syntax

Access modifier static data type of the attribute name;

Defined outside the class, method: a defined location

Note: The access modifier static position and interchangeable.

b) Features

Static property is shared by the entire class A space, any object changes its value, on the other

Like to follow a change. (All-class share: based on all the objects of this class created a share)

Here Insert Picture Description

c) access method

1. The object name static property name;. (Not recommended)
2. The name of the class static property name;

2. static modification methods static method (class method)

a) definition syntax

Access modifier static method return type name (parameter list) {

// method to achieve

}

b) access method

1. accessing the class in the class static methods: Method name (arguments);

2. access other classes: class name method name (arguments);

//java.util.Arrays.copyOf() java.util.Arrays.sort()

//java.lang.Math.Sqrt(n); java.lang.Math.PI

c) Features

1. static method can directly access only static members, can not directly access non-static members.

Cause: When you access a static member, may not create an object, but members of the methods, member variables

The need for objects by name (a reference), so the result is ambiguous. So in a static way

Law can directly access only static members, you can not directly access non-static members.

​ [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OnAzb1EO-1571962949532)(E:\Corejava\课堂笔记及代码\day13_三个修饰符\笔记\static不能直接访问普通成员.png)]

2. 在静态方法中不能使用this|super的前缀。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yFM7pWlH-1571962949532)(E:\Corejava\课堂笔记及代码\day13_三个修饰符\笔记\static不能直接访问普通成员2.png)]

3. 静态方法可以被继承,静态方法只能被静态方法覆盖,但是没有多态(多态 的特点:指的是父类引用指向子类对象,调用父类中的方法吗,如果这个方 法被子类覆盖了,那么执行的是子类覆盖之后的方法)

Super s = new Sub(); s.m1() 执行的是Super类的方法

s.m1()会被转换成Super.m1();

3. 动态代码块|初始代码块

a) 定义的位置:类中,方法的外面

b) 作用:可以为属性进行初始化的工作

c) 执行地位:在构造方法之前执行,和初始化属性按照顺序进行初始化

的动作.

4. 静态代码块

a) 定义位置: 类中,方法的外面,被static 修饰

b) 作用:为静态属性赋值

c) 执行地位:在类加载时,和静态属性的初始化工作按照顺序进行。

类加载的时候会触发静态代码块的执行

5. 类加载

a) 概念:JVM在第一次使用某一个类的时候会通过CLASSPATH找到编译之后生

成的.class文件,然后将类中所有的信息(类名,属性,方法,构造方

法,包结构,接口…)保存到内存中。类加载只会进行一次。

b) 类加载的时机

1. 第一次创建对象时会触发类加载的进行。

2. 第一次访问静态成员时会触发类加载的进行

3. 子类类加载会触发父类类加载的先进行

4. Class.forName(“全限定名”);

注意:只是声明引用,不创建对象不会进行类加载。

6. 对象的创建过程

a) 没有继承关系的对象创建过程

Here Insert Picture Description

b)存在继承关系的对象创建过程

Here Insert Picture Description

二、abstract 抽象的

1. 概念:像却不是的,具备某种对象的特征,但好像又不完整。

2. 可以修饰类和方法

3. abstract可以修饰类 ,此类为抽象类,那么此类就是不够完整,不够具体的类,此类不能new对象。

a) 语法:abstract class 类名{}

b) 抽象类的作用:

1. 可以为子类提供共性的属性和方法,那么父类共性+子类独有=完整的子类对象

(抽象类可以被继承,抽象类存在构造方法,在子类构造方法的第一行默认存

在一个super(),代表调用父类的构造方法,那么构建子类对象之前一定会先

构建父类对象,父类共性+子类独有组合成完整的子类对象)

Here Insert Picture Description

2. 可以声明为引用,强制使用多态。

(不能保存自身类型的对象,只能保存子类类型的对象,强制使用多态)

Here Insert Picture Description

4. abstract修饰方法 抽象方法 不够完整,不够具体的方法,被abstract修饰

a) 特点

1. 一个类继承了一个抽象类,那么必须要实现抽象类中的抽象方法,否则这个类还是

一个抽象类。

2. 抽象类中可以存在普通方法,也可以没有抽象方法,但是抽象方法必须包含在抽象类中。

抽象类的用处:一般用于修饰父类 如Person Animal Shape

三、final 最终的 最后的

final能够修饰类、方法、变量

1. final修饰类 最终的类,最后的类

此类不能被继承 没有子类

System|Math|String都属于final修饰的类

2. final修饰的方法 最终的最后的方法

final修饰的方法可以被继承,但是不能被覆盖。

3. final修饰的变量 最终的变量 值不能改变 常量

a) 局部常量

赋值时机:显示初始化的时候必须赋值

b) 成员常量:

没有默认值,必须先赋值才能使用
赋值时机:必须在对象创建完毕之前赋值
1. 显示初始化
2. 动态代码块
3. 构造方法
注意:如果需要通过构造方法为final修饰的成员变量赋值,那么必须保证所有的构造方法都能正确
赋值。

c) 静态常量

没有默认值,必须手动赋值
The timing of the assignment: to be assigned before the end of the class loader
1. Display initialization
2. static code block

d) Object Constant

final modification of the basic data types of variables, the value can not be changed

final modified reference data types of variables, addresses can not be changed.

Fourth, the joint use of modifiers (private abstract static final method)

1. abstract can not be used in conjunction with private

No, reason: abstract modified method is no method body, and covers the requirements subclass inherits

Abstract method, but private modified method can not be inherited.

2. abstract can be used in combination with the static

No, reason: static modification method can be invoked through the class name, but the abstract

There is no way the body can not be called

3. abstract can not be used in conjunction with the final

No, reason: abstract needs to be covered, but the final method can be modified following the

Cheng, but it can not be covered

4. private static final and can not be used in combination

It can be used in combination twenty-two

3. abstract can not be used in conjunction with the final

No, reason: abstract needs to be covered, but the final method can be modified following the

Cheng, but it can not be covered

4. private static final and can not be used in combination

It can be used in combination twenty-two

Guess you like

Origin blog.csdn.net/weixin_45536396/article/details/102736085