The use of this, static, final, and super keywords in java

Table of contents

1. this

1 Introduction

2.Format

3. Actual use

4.Examples

2. static

1 Introduction

2. Name

3. Actual use

4.Examples

 3. final

1 Introduction

2.Use place

3.Examples

4. super

1 Introduction

2.Format

3.Examples


1. this

1 Introduction

When you sometimes need to refer to the object that is using the method in a class-defined method, you can use the keyword this to represent the object.

2.Format

this.变量名
this.方法名(参数列表)

3. Actual use

  (1). It is illegal to define two local variables with the same name in the same method body. But local variables, including input parameters accepted by methods, can have the same name as member variables of the class. At this time, you can use the this keyword to directly reference the object to distinguish local variables from global variables.

  (2). Used in construction methods. Member methods in the class can be called through this, and this can enable the constructor to call other constructors with the same name.

4.Examples

  (1). Distinguish between local variables and global variables

public class javaTest{
    String a;
    char b;
    int c;

    public void setTest1(String aa,char bb,int cc){
        this.a=aa;    //this可以删除,因为全局变量名与局部变量名不同
        this.b=bb;
        this.c=cc;
    }
    public void setTest2(String aa,char bb,int cc){
        a=aa;    //与setTest1进行对比
        b=bb;
        c=cc;
    }
    public void setTest3(String a,char b,int c){
        this.a=a;    //this不可以删除,因为全局变量名与局部变量名相同
        this.b=b;
        this.c=c;
    }
    public static void main(String args[]){
        javaTest s=new javaTest();
        s.setTest1("hello",'x',10);
        System.out.println("执行setTest1方法后 a="+s.a+" b="+s.b+" c="+s.c);

        s.setTest2("hello",'Y',10);
        System.out.println("执行setTest2方法后 a="+s.a+" b="+s.b+" c="+s.c);

        s.setTest3("hello",'Z',10);
        System.out.println("执行setTest3方法后 a="+s.a+" b="+s.b+" c="+s.c);
    }
}

   Output result:

  (2).Construction method

package Study;

public class javaTest{
    String a;
    int b;

    public javaTest(){
        a="张三";
        b=14;
    }
    public javaTest(String a,int b){
        this();   //通过this调用无参构造方法,即调用上面的方法
        System.out.println("a="+this.a+" b="+this.b);
    }
    public javaTest(int b){
        this.b=b;
        System.out.println("b="+this.b);
    }
    public static void main(String args[]){
        javaTest s=new javaTest("李四",30);  //调用第二个构造方法
        javaTest x=new javaTest(30);   //调用的第三个构造方法
    }
}

Output results

2. static

1 Introduction

In general, accessing members of a class must be done by instantiating the class. But sometimes you want to define any object that is completely independent of the class. In this case, you only need to add static before the declaration of the class member.

2. Name

Members with the keyword static have static attributes.

static+member variable=static member variable (class variable)

static+member method=static member method (class method)

3. Actual use

Static is not used to modify a class, but only to modify the members of the class. It is shared among all instances of the class.

For static members of public type, they can be called directly using the class name outside the class without initialization.

4. Two principles of class methods

(1). Class methods cannot access non-static variables and methods of the class to which they belong, but can only access local variables, parameters and static variables within the method body.

(2). This and super keywords cannot appear in class methods.

4.Examples

Program 1

public class javaTest{
    static String name="王五";    //静态成员变量
    public static String setName(String n){   //静态成员方法
        name=n;
        return name;
    }
}

Program 2 (call program 1 through program 2)

public class javaTest2 {
    String testNmae="张三";
    public static void main(String args[]){
        javaTest2 s=new javaTest2();
        System.out.println("未调用前testName的值:"+s.testNmae);
        s.testNmae=javaTest.name;      //类变量的调用(这时类外不需要初始化)
        System.out.println("调用类变量后的testName的值:"+s.testNmae);
        s.testNmae=javaTest.setName("李四");   //类方法的调用
        System.out.println("调用类方法后的testName的值:"+s.testNmae);
    }
}

Output results

 3. final

1 Introduction

When final modifies the basic data type, the variable becomes a constant and the value cannot be changed; when final modifies the reference data type, the value of the variable cannot be changed, that is, the object memory address value stored in the value remains unchanged and the variable cannot be changed. To point to other objects, but the members within the object can change

2.Use place

Can be used to modify variables, methods and classes

3.Examples

Program 1

public class javaTest{
    static String name="王五";    //静态成员变量
    public static String setName(String n){   //静态成员方法
        name=n;
        return name;
    }
}

Program 2

public class javaTest1{
    final static int number=100;

    public static void main(String args[]){
        //下列语句删除注释后发生编译错误,final修饰的变量number无法再次赋值
        //javaTest1.number=200;

        final javaTest s1=new javaTest();      //实例化程序1
        javaTest1 s2=new javaTest1();

        //下列语句删除注释后发生编译错误,final修饰的变量s1无法再次赋值 
        //s1=s2;
    }
}

4. super

1 Introduction

The super keyword represents a reference to the parent class of a class. Generally speaking, super has two general forms:

(1). Used to access parent class members that are hidden by members of the subclass.

(2). You can call the constructor of the parent class

If a subclass and a parent class have member variables or methods with the same name, you can use the super keyword to refer to members of the parent class.

2.Format

super.成员变量名
super.成员方法名

3.Examples

Program 1

public class javaTest{
    String name;
    char sex;
    int age;

    public String getName(){
        return name;
    }
    public char getSex(){
        return sex;
    }
    public int getAge(){
        return age;
    }
}

Program 2

public class javaTest2 extends javaTest {
    String studentID;
    String major;
    String name;   //定义一个与父类成员同名的变量name
    //定义一个和父类成员同名的成员方法getName()
    public String getName(){
        return "王五";
    }
    public String getName2(){
        super.name="张三";   //获得父类被隐藏的成员变量name
        return super.getName();  //调用父类被隐藏的成员方法getName()
    }
    public String getMajor(){
        return major;
    }
    public String getStudentID(){
        return studentID;
    }
}

Program 3

public class javaTest3 {
    public static void main(String[] args){
        javaTest2 s1=new javaTest2();
        s1.name="赵六";
        System.out.println("学生姓名是:"+s1.name);
        System.out.println("学生姓名是:"+s1.getName());
        System.out.println("学生姓名是:"+s1.getName2());
    }
}

Program 2 continues program 1, and then program 3 calls the member variables and methods in programs 1 and 2.

Output results

 

Guess you like

Origin blog.csdn.net/weixin_47406082/article/details/123654259