Shiqianfeng Week 4 Notes summary

The static keyword:
the difference between static and instance 1:
I. instance properties of each object is held by each independent space (more than), unilaterally modify the object, without affecting other objects.
II. Static property is held in common by the entire class shared space (a), any object modification will affect other objects.
2. static concept:
I. static properties and methods can be modified, that is, static properties (class attribute), static method (class method)
II is an all-static class members shared by all objects, only one whole class, not because of. create multiple objects to produce multiple copies.
III. Instead of creating objects, but also by class name, direct access to static members.
IV Experience: When accessing static properties and methods, directly through the "static class name property name." And "static class name method name." (Recommended)
3. Static characteristics:
I. static method allows direct access to static members.
II. Static methods can not directly access non-static members.
III. Static methods are not allowed in this or super keyword.
IV. Static methods can be inherited, can not cover, no polymorphism.

  1. Class loader:
    I.JVM first use of a class, the class of .class file is loaded into memory, save it.
    . II loading time points:
    1) create the object.
    2) create a sub-class object.
    3) call the static properties and methods.
    4) Class.forName ( "fully qualified name"); // active load a class.

  2. Static block of code:
    I. The class loading, static code to trigger the execution block (only once).
    II perform position: static properties after initialization.
    III role: to assign the static properties, or initial behavior necessary.

  3. Summary:
    static members modified static member without creating an object, it can be accessed directly through the class name.
    Static methods can not directly access non-static members.
    Static methods can not use this or super.
    Static methods can be inherited and can not rewrite, no polymorphism.
    Static block of code is executed when the class is loaded and executed only once.
    Here Insert Picture Description
    Here Insert Picture Description
    Final:
    I. modified class: class can not be inherited
    II modification method: This method can not be covered.
    III modified variables: This variable value is unchanged - Constant (no initial value, allowing only assigned once).
    1) local constants: display initialize
    2) assignment example constants: the display initialize, dynamic code block constructor.
    Requirements:
    A) Examples of constant values deadline:. Constructor until completion, as examples can be constant values.
    B). If the instance is assigned in a constant constructor must ensure that all construction methods can be correctly assigned.
    3) Constant static assignment: the display initialize, static code block.
    Requirements:
    A) Static constants assignment deadline:. Before class loading is completed (by class name before the call), you can assign a static constant.

IV constants different types of features:
1) constant basic data types: immutable values.
2) reference data type constant: an address immutable.

interface:

  1. 接口的语法:
    I. 相当于特殊的抽象类,定义方式、组成部分,与抽象类类似。
    II. 接口中只能定义公开静态常量(变量)
    III. 接口中只能定义公开抽象方法(方法)
    IV. 接口不是类

  2. 接口与抽象类的异同:
    I. 相同:
    1). 可以编译成字节码文件
    2). 不能创建对象。(接口不是类,不是模板的概念,也没有构造方法)
    3). 可以声明引用。
    4). 具备Object定义的方法。
    II. 不同:
    1). 接口中的属性只能是公开静态常量(隐式使用public static final修饰)
    2). 接口中的方法只能是公开抽象方法(隐式使用public abstract修饰)
    3). 没有构造方法、没有动态代码块、没有静态代码块

  3. 接口的概念:
    I. 接口是种能力和约定
    1). 接口的定义:能力
    2). 方法的定义:约定
    II. 经验:Java为单继承,当父类的方法种类无法满足子类需求时,可实现接口扩充子类能力。
    III. 经验:接口支持多实现,可为类扩充多种能力。

  4. 接口的规范:
    I. 任何类在实现接口时,必须实现接口中所有的抽象方法,否则此类为抽象类。
    II. 实现接口中的抽象方法时,访问修饰符必须是public。

  5. 接口引用:
    I. 同父类一样,接口也可声明为引用,并指向实现类对象。
    II. 注意:
    1). 仅可调用接口中所声明的方法,而不可调用实现类中独有的方法。
    2). 可强转回实现类的本身类型,进行独有的属性和方法的调用。(强转前通过instanceof判断)

  6. 接口的多态:
    I. 不再关注具体的类型,而是关注行为
    II. 未完待续

  7. 常见关系:
    I. 类与类:单继承,extends父类名称
    II. 类与接口:多实现,implements 接口名称1,接口名称2,接口名称3
    III. 接口与接口:多继承,extends 父接口名称1,父接口名称2

  8. 常量接口:
    将多个常用于表示状态和固定值的变量,以形态常量的形式定义在接口中统一管理,提高代码的可读性。

  9. 接口:
    I. 接口是一种标准。
    II. 耦合度:模块与模块之间的关联程度,关联的越密切,耦合越高,关联的越松散,耦合越低。
    Here Insert Picture Description
    Here Insert Picture Description
    一、内部类:

  10. 概念:在一个类的内部,再定义一个完整的类。

  11. 特点:
    I. 编译之后可生成独立的字节码文件。
    II. 内部类可直接访问外部类的私有成员,而不破坏封装。
    III. 可为外部类提供必要的内部功能组件。

  12. 分类:
    I. 成员:
    1). 在类的内部定义,与实例变量、实例方法同级别的类。
    2). 属于外部类的一个实例部分,创建内部类对象,必须依赖外部类对象。
    3). Outer out = new Outer();
    Outer.Inner in = out.new Inner();
    4). 当外部类、内部类存在重名属性时,有限访问内部类属性,通过外部类类名.this.外部类实例属性
    5). 成员内部类不能定义静态成员。
    II. 静态:
    1). 不依赖外部类对象,可直接创建或通过类名访问,也可声明静态成员。
    2). Outer.Inner.静态成员
    Outer.Inner in = new Outer.Inner();
    III. 局部:
    1). 定义在外部类的方法中,作用范围和创建对象的范围仅限当前方法中。
    2). 局部内部类访问外部类局部变量时,因无法保障变量的生命周期与自身相同,所以修饰为final。
    3). 隐藏类的信息、限制类的使用范围。
    IV. 匿名:
    1). 没有类名的局部内部类
    2). 必须继承一个父类或实现一个接口。
    3). 定义类、实现类、创建对象的语法合并,只能创建一个该类的对象。
    4). 优:减少代码量,书写的思路流畅。
    5). 劣:可读性较差。

Released three original articles · won praise 0 · Views 152

Guess you like

Origin blog.csdn.net/qq_40091389/article/details/104580559