亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java類中this關(guān)鍵字與static關(guān)鍵字的用法解析

 更新時間:2022年09月07日 10:08:54   作者:三天曬網(wǎng)且從不打魚  
這篇文章主要介紹了Java類中this關(guān)鍵字與static關(guān)鍵字的用法解析,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下

前言

今天給大家總結(jié)介紹一下Java類中this關(guān)鍵字和static關(guān)鍵字的用法。

this關(guān)鍵字用法:

  • this.屬性可以調(diào)用類中的成員變量
  • this()可以調(diào)用類中的構(gòu)造方法

1:修飾屬性,表示調(diào)用類中的成員變量。

代碼示例:

public class Student {

    public String name;
    public int age;
    public String school;

    public Student(String name, int age, String school) {
        this.name = name;
        this.age = age;
        this.school = school;
    }
}

因為程序的就近匹配原則,編譯器會從調(diào)用代碼處的最近位置查找有無匹配的變量或者方法,若找到直接使用最近的變量或方法。所以如果上述代碼中的帶參構(gòu)造方法不使用this的話我們在使用該構(gòu)造方法時會遇到無法賦值的問題。

2:this修飾方法

this可用于構(gòu)造函數(shù)之間的相互調(diào)用,可以減少構(gòu)造函數(shù)代碼的耦合性,使代碼看起來更加整潔(不寫重復代碼很重要)。

未使用this前:

public class Student {

    public String name;
    public int age;
    public String school;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public Student(String name, int age, String school) {
        this.name = name;
        this.age = age;
        this.school = school;
    }

}

使用this后:

public class Student {

    public String name;
    public int age;
    public String school;

    public Student() {
    }

    public Student(String name, int age) {
        this();
        this.name = name;
        this.age = age;
    }

    public Student(String name, int age, String school) {
        this(name,age);
        this.school = school;
    }

}

PS:

  • 1.this調(diào)用構(gòu)造方法必須放在當前構(gòu)造方法的首行調(diào)用,否則會報錯。
  • 2.對構(gòu)造方法的調(diào)用不能成"環(huán)”必須線性調(diào)用,否則會陷入調(diào)用死循環(huán)。

3:this表示當前對象的引用

當前是通過哪個對象調(diào)用的屬性或者方法,this就指代哪一個對象。

代碼示例:

public class Student {

    public String name;
    public int age;
    public String school;

    public void show(){
        System.out.println(this);
    }

    public static void main(String[] args) {
        Student stu1 = new Student();
        stu1.show();
        System.out.println(stu1);
        System.out.println("————————————");
        Student stu2 = new Student();
        stu2.show();
        System.out.println(stu2);
        System.out.println("————————————");
        Student stu3 = new Student();
        stu3.show();
        System.out.println(stu3);
    }
}

輸出結(jié)果:

static關(guān)鍵字用法:

在Java的類中,若static修飾類中屬性,稱之為類的靜態(tài)屬性/類屬性,它和具體的對象無關(guān),該屬性存儲在JVM的方法區(qū)(不同于堆區(qū)和棧區(qū)的另一個區(qū)域),類中的所有對象共享同一個方法區(qū)(類中的常量和靜態(tài)變量儲存在方法區(qū)中),直接使用類名稱來訪問靜態(tài)變量,不推薦使用某個對象來訪問。

只要類一定義,JVM就會為static修飾的類屬性分配空間,它和類是綁定的,使用static修飾變量的好處是當我們需要修改一個值時可以更加方便,比如學生類中的學校屬性,若學校改名字了,我們沒有使用static修飾,那么我們就要給每個學生丟修改一次,但是使用了static則只需要修改一次。

相關(guān)問題:Java方法中是否可以定義靜態(tài)變量?

解答:靜態(tài)變量,當類定義時和類一塊加載到內(nèi)存中了,而調(diào)用方法至少是在類定義之后才能調(diào)用的,先后順序不一樣,就是說還沒調(diào)用方法便已經(jīng)執(zhí)行了方法里面的定義變量,這是不合理的。

到此這篇關(guān)于Java類中this關(guān)鍵字與static關(guān)鍵字的用法解析的文章就介紹到這了,更多相關(guān)Java類this關(guān)鍵字與static關(guān)鍵字內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論