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.
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
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)
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) 没有继承关系的对象创建过程
b)存在继承关系的对象创建过程
二、abstract 抽象的
1. 概念:像却不是的,具备某种对象的特征,但好像又不完整。
2. 可以修饰类和方法
3. abstract可以修饰类 ,此类为抽象类,那么此类就是不够完整,不够具体的类,此类不能new对象。
a) 语法:abstract class 类名{}
b) 抽象类的作用:
1. 可以为子类提供共性的属性和方法,那么父类共性+子类独有=完整的子类对象
(抽象类可以被继承,抽象类存在构造方法,在子类构造方法的第一行默认存
在一个super(),代表调用父类的构造方法,那么构建子类对象之前一定会先
构建父类对象,父类共性+子类独有组合成完整的子类对象)