Object-oriented common keywords

A, this keyword

  1. Definitions

    • this can be called class attributes, methods and constructors
    • It represents the current object or objects currently being created
      • It internally method, i.e. the method belongs to an object reference
      • It constructor is used internally to indicate the object of this configuration is initializing

  2, this call structure

    • this call properties / methods
      • ① In the method of the class, we can use "this. Properties" or "this. Method" approach, calling the current object property or method. However, under normal circumstances, we have chosen to omit "this.". In special cases, and if the parameter class of the same name attribute method, we must explicitly use "this. Variables" manner, show that this property is variable, rather than parameter


      • ② in the class's constructor, we can use "this. Properties" or "this. Method" approach, calling an object property or method is currently being created. However, under normal circumstances, we have chosen to omit "this.". In special cases, if the parameter type and an attribute of the same name constructor, we must explicitly use "this. Variables" manner, it shows that this property is variable, rather than parameter

       

 

    • this call the constructor
      • ① constructor in class, can explicitly use "the this (parameter list)" mode, call other constructor class specified in the present

        

 

      • Can not be "the this (parameter list)" method call itself constructor ② constructor
      • ③ if a class declares constructor of n, there is a maximum of n-1 using the constructor "this (parameter list)." Can not appear the cycle call
      • ④ "this (parameter list)" must be declared at the beginning of the line constructor of the class
      • ⑤ internal constructor, can only declare a "this (parameter list)", used to call other constructors

Two, super keyword

  1. Definitions

    • super understood as: the parent class
    • Designating operation using the super to invoke the parent class
      • It can be used to access the super properties defined in the parent class
      • The method can be used to call super member defined in the parent class
      • super it is used to call the parent class constructor subclass constructor
    • super traceability is not limited to the direct parent

  2, super call structure

    • super调用属性/方法
      • ① 在子类的方法或构造器中,通过"super.属性"或者"super.方法"的方式,显式的调用父类中声明的属性或方法。但是很多情况下都可以省略"super."

        

 

      • ② 当子类和父类中定义了同名的属性时,若想在子类中调用父类中声明的属性,则必须显式的使用"super.属性"的方式,表明调用的是父类中声明的属性。说明:子类的属性不会覆盖父类中同名属性。在堆空间中,两个属性都会存在
      • ③ 当子类重写了父类中的方法以后,若想在子类的方法中调用父类中被重写的方法时,则必须显式的使用"super.方法"的方式,表明调用的是父类中被重写的方法
    • super调用构造器
      • ① 在子类的构造器的首行,显示的使用“super(形参列表)”的方式,调用父类指定的构造器

        

      • ② 针对于子类的某一个构造器而言,只能最多声明一个“super(形参列表)”
      • ③ 在子类的构造器中,针对于"this(形参列表)"或"super(形参列表)"只能二选一,不能同时出现
      • ④ 在子类构造器的首行,没有显式的声明"this(形参列表)"或"super(形参列表)",则默认调用的是父类中空参的构造器:super()。此时父类中必须存在空参构造器,不然报错

        

      • ⑤ 在子类的多个构造器中,至少一个构造器的首行使用了"super(形参列表)"。其他构造器均为this(形参列表)(n-1)

  3、this和super的区别

    

三、static关键字

  1、定义

    • 静态的,在Java类中,可用static修饰属性、方法、代码块、内部类。没有构造器
    • 被修饰后的成员具备以下特点:
      • ①随着类的加载而加载
      • ②优先于对象存在
      • ③修饰的成员,被所有对象所共享
      • ④访问权限允许时,可不创建对象,直接被类调用

  2、static修饰属性

    • 1)按是否使用static修饰
      • 静态变量(或类变量):使用static修饰的变量。当创建类的多个对象,多个对象共享同一个静态变量。当通过某一个对象修改静态变量时,会导致其他对象调用此静态变量时,是已经修改过了的
      • 实例变量(非静态属性):不使用static修饰的属性。当创建类的多个对象,每个对象都独立的拥有一套类中的非静态属性。当修改其中一个对象中的非静态属性时,不会导致其他对象中同样的属性值的修改
    • 2)按内存分配
      • 实例变量:随着对象的创建而产生,分配在堆空间中
      • 静态变量
        • ① 静态变量随着类的加载而加载。可以通过"类.静态变量"的方式进行调用

           

        • ② 静态变量的加载要早于对象的创建
        • ③ 由于类只会加载一次,则静态变量在内存中也只会存在一份:存在方法区的静态域中
        • ④ 静态属性举例:System.out; Math.PI
    • 3)静态变量内存解析

   

  3、static修饰方法:静态方法 VS 非静态方法

    • 随着类的加载而加载,可以通过"类.静态方法"的方式进行调用

      

    • 静态方法中:只能调用静态的属性或静态的方法。不可以调用非静态的属性和非静态的方法

      

    • ③ 非静态方法中:既可以调用非静态的属性和非静态的方法。也可以调用静态的属性或静态的方法

      

    • static注意点:在静态的方法内,不能使用this关键字、super关键字

  4、static使用环境

    • 如何判定属性是否需要声明为static的?
      • ① 属性是可以被多个对象所共享的,不会随着对象的不同而不同的
      • ② 类中的常量也常常声明为static
    • 如何判定方法是否需要声明为static的?
      • ① 操作静态属性的方法,通常设置为static的
      • ② 工具类中的方法,习惯上声明为static的。 比如:Math、Arrays、Collections

四、final关键字:

    • 定义:表示“最终的”,可以用来修饰的结构:类、方法、变量

  1、 final修饰类

    • 此类不可以被继承。比如:String / StringBuffer类

     

  2、final修饰方法

    • 此方法不能被重写。比如:Object类的getClass()

   

  3、final修饰变量

    • final标记的变量(成员变量或局部变量)即称为常量。名称大写,且只能被赋值一次。比如:Math类中的PI
    • 1)final修饰属性
      • final标记的成员变量必须在声明时或在每个构造器中或代码块中显式赋值,然后才能使用
      • 可以考虑的赋值的位置:
        • ①显式初始化

          

        • ②代码块中初始化

 

        • ③构造器中

 

        •  方法中不行

 

    • 2) final修饰局部变量
      • final修饰方法体中的局部变量

      • final修饰形参时,当调用此方法时,给常量形参赋一个实参,一旦赋值以后,就只能在方法体内使用此形参,但不能进行重新赋值

    • 3)static final 用来修饰属性:全局常量

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/zhufeng123/p/12347753.html