欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java this關(guān)鍵字的使用詳解

 更新時間:2021年11月05日 10:26:36   作者:兮動人  
this 關(guān)鍵字是 Java 常用的關(guān)鍵字,可用于任何實例方法內(nèi)指向當前對象,也可指向?qū)ζ湔{(diào)用當前方法的對象,或者在需要當前類型對象引用時使用

1. 先看一段代碼,并分析問題

在這里插入圖片描述

在這里插入圖片描述

public class This01 {

    //編寫一個main方法
    public static void main(String[] args) {

        Dog dog1 = new Dog("大壯", 3);
        //dog1調(diào)用了 info()方法
        dog1.info();

    }
}

class Dog{ //類

    String name;
    int age;
    // public Dog(String dName, int  dAge){//構(gòu)造器
    // 	name = dName;
    // 	age = dAge;
    // }
    //如果我們構(gòu)造器的形參,能夠直接寫成屬性名,就更好了
    //但是出現(xiàn)了一個問題,根據(jù)變量的作用域原則
    //構(gòu)造器的name 是局部變量,而不是屬性
    //構(gòu)造器的age  是局部變量,而不是屬性
    //==> 引出this關(guān)鍵字來解決
    public Dog(String name, int  age){//構(gòu)造器
        //this.name 就是當前對象的屬性name
        this.name = name;
        //this.age 就是當前對象的屬性age
        this.age = age;
    }

    public void info(){//成員方法,輸出屬性x信息
        System.out.println(name + "\t" + age + "\t");
    }
}

在這里插入圖片描述

2. 深入理解 this

為了進一步理解 this,我們再看一個案例 (This01.java)

在這里插入圖片描述

在這里插入圖片描述

使用hashCode()看看對象的情況

在這里插入圖片描述

public class This01 { 

	//編寫一個main方法
	public static void main(String[] args) {

		Dog dog1 = new Dog("大壯", 3);
		System.out.println("dog1的hashcode=" + dog1.hashCode());
		//dog1調(diào)用了 info()方法
		dog1.info(); 

		System.out.println("============");
		Dog dog2 = new Dog("大黃", 2);
		System.out.println("dog2的hashcode=" + dog2.hashCode());
		dog2.info();
	}
}

class Dog{ //類

	String name;
	int age;

	public Dog(String name, int  age){//構(gòu)造器
		//this.name 就是當前對象的屬性name
		this.name = name;
		//this.age 就是當前對象的屬性age
		this.age = age;
		System.out.println("this.hashCode=" + this.hashCode());
	}

	public void info(){//成員方法,輸出屬性x信息
		System.out.println("this.hashCode=" + this.hashCode());
		System.out.println(name + "\t" + age + "\t");
	}
}

在這里插入圖片描述

3. this 的注意事項和使用細節(jié)

ThisDetail.java

  • this 關(guān)鍵字可以用來訪問本類的屬性、方法、構(gòu)造器
  • this 用于區(qū)分當前類的屬性和局部變量
public class ThisDetail {
    public static void main(String[] args) {
        T t = new T();
        t.f3();
    }
}

class T{

	String name = "兮動人";
    int num = 10;
    
	//this關(guān)鍵字可以用來訪問本類的屬性
	public void f3(){
	      String name = "smith";
	      //傳統(tǒng)方式
	      System.out.println("name=" + name + " num=" + num);//smith  100
	      //也可以使用this訪問屬性
	      System.out.println("name=" + this.name + " num=" + this.num);//jack 100
	}
}	    

在這里插入圖片描述

訪問成員方法的語法:this.方法名(參數(shù)列表);

public class ThisDetail {
    public static void main(String[] args) {
        T t1 = new T();
        t.f2();
    }
}

class T	{
    public void f1(){
        System.out.println("f1()方法...");
    }
    public void f2(){
        System.out.println("f2()方法...");
        //調(diào)用本類的 f1
        //第一種方式
        f1();
        //第二種方式
        this.f1();
    }
}

在這里插入圖片描述

訪問構(gòu)造器語法:this(參數(shù)列表); 注意只能在構(gòu)造器中使用(即只能在構(gòu)造器中訪問另外一個構(gòu)造器, 必須放在第一條語句)

public class ThisDetail {
    public static void main(String[] args) {

        T t2 = new T();
    }
}

class T{
    /*
	細節(jié): 訪問構(gòu)造器語法:this(參數(shù)列表);
	注意只能在構(gòu)造器中使用(即只能在構(gòu)造器中訪問另外一個構(gòu)造器)
	注意: 訪問構(gòu)造器語法:this(參數(shù)列表); 必須放置第一條語句
	 */
    public T(){
        //這里去訪問 T(String name,int age)構(gòu)造器,必須放在第一行
        this("Jack", 23);
        System.out.println("T()構(gòu)造器");

    }

    public T(String name,int age){
        System.out.println("T(String name,int age)構(gòu)造器");
    }

}

在這里插入圖片描述

this 不能在類定義的外部使用,只能在類定義的方法中使用。

4. this 的案例

TestPerson.java

定義 Person 類,里面有 name、age 屬性,并提供 compareTo 比較方法,用于判斷是否和另一個人相等,提供測試類 TestPerson 用于測試, 名字和年齡完全一樣,就返回 true, 否則返回 false

public class TestPerson { 

	//編寫一個main方法
	public static void main(String[] args) {

		Person p1 = new Person("mary", 20);
		Person p2 = new Person("mary", 20);

		System.out.println("p1和p2比較的結(jié)果=" + p1.compareTo(p2));
	}
}

/*
定義Person類,里面有name、age屬性,并提供compareTo比較方法,
用于判斷是否和另一個人相等,提供測試類TestPerson用于測試, 
名字和年齡完全一樣,就返回true, 否則返回false

 */
class Person {
	String name;
	int age;
	//構(gòu)造器
	public Person(String name, int age) {
		this.name = name;
		this.age = age;
	}
	//compareTo比較方法
	public boolean compareTo(Person p) {
		//名字和年齡完全一樣
		// if(this.name.equals(p.name) && this.age == p.age) {
		// 	return true;
		// } else {
		// 	return false;
		// }
		return this.name.equals(p.name) && this.age == p.age;
	}
}

在這里插入圖片描述

把名字或年齡改成其他不同數(shù)據(jù)

在這里插入圖片描述

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

相關(guān)文章

最新評論