當前位置:首頁 » 編程語言 » java界面的this

java界面的this

發布時間: 2023-09-17 13:01:38

java中this的用法

java中this有兩種用法:
1、代表當前類
public class Dog{
private String name;
private float age;
public setName(String name){
this.name = name;
}
.......
}
這里的this就代表的當前的這個Dog類。this.name可以理解為dog.name,只是理解,不是等於。
2、在構造函數中的使用
public class Dog{
private String name;
private int age;
//有一個參數的構造函數
public Dog(String name){
this.name = name;
}
public Dog(String name,int age){
this.name = name;
this.age = age;
}
//這個無參構造方法里調用的有兩個參數的構造方法,這個也就是this的第二種用法了!
public Dog(){
this("nihao",20);
}
}

② java里this是什麼意思

Java關鍵字this只能用於方法方法體內。

個對象創建後,Java虛擬機(JVM)就會給這 個對象分配一個引用自身的指針,這個指針的 名字就是 this。因此,this只能在類中的非靜 態方法中使用,靜態方法和靜態的代碼塊中絕 對不能出現this,這在「Java關鍵字static、fina l 使用總結」一文中給出了明確解釋。並且this只 和特定的對象關聯,而不和類關聯,同一個類 的不同對象有不同的this。

③ Java裡面的this關鍵字是什麼意思

this關鍵字可以簡單的理解為,誰調用this所在的方法,this就是誰。
類的構造函數與getter、setter方法常用到this關鍵字(JavaBean)

JavaBean是一種可重用的Java組件,它可以被Applet、Servlet、SP等Java應用程序調用.也可以可視化地被Java開發工具使用。它包含屬性(Properties)、方法(Methods)、事件(Events)等特性。
public class Person {
//==========靜態屬性===========
//--想學編程的可以來我這看看----
private String name; //姓名
private int age; //年齡
private String gender; //性別

//==========動態行為===========
public void readBook(String book) {
System.out.println("reading " + book);
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;//this.name就是上面的private String name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public String getGender() {
return gender;
}

public void setGender(String gender) {
this.gender = gender;
}
}
官方定義
this 是自身的一個對象,代表對象本身,可以理解為:指向對象本身的一個指針。
this 的用法在 Java 中大體可以分為3種:
1.普通的直接引用
這種就不用講了,this 相當於是指向當前對象本身。
2.形參與成員名字重名,用 this 來區分:
class Person {
private int age = 10;
public Person(){
System.out.println("初始化年齡:"+age);}
public int GetAge(int age){
this.age = age;
return this.age;
}
}
public class test1 {
public static void main(String[] args) {
Person Harry = new Person();
System.out.println("Harry's age is "+Harry.GetAge(12));
}
}

希望對您有所幫助!~

④ java里的「this」到底是什麼意思

this為一系統資源,只允許用戶讀而不允許寫,它存放當前對象的地址(引用)。

this變數有以下作用:

1. 構造方法重用:

public class Rectangle{
public Rectangle(Location at, Shape size) {…}
public Rectangle(Shape size,Location at){
this(at, size); }
public Rectangle(Location at) {
this(at, new Shape(100,100));
}
public Rectangle(Shape size) {
this(size, new Location(1,1));
}
public Rectangle() {
this(new Location(1,1), new Shape(100,100));
}
}

2、消除歧義:

Location{
private int x;
private int y;
public Location(int x,int y) {
this.x=x;
this.y=y;
}
……
}

3、返回對象-鏈式方法調用:

public class Count {
private int i = 0;
Count increment() {
i++;
return this; //返回對象的地址,所以我們可以鏈式訪問
}
void print() {
System.out.println("i = " + i);
}
}
public class CountTest{
public static void main(String[] args) {
Count x = new Count();
x.increment().increment().print();
}
}

4、作為參數傳遞"this」變數-進行回調:

假設有一個容器類和一個部件類,在容器類的某個方法中要創建部件類的實例對象,而部件類的構造方法要接受一個代表其所在容器的參數。例如:
class Container
{
Component comp;
public void addComponent()
{
comp = new Component(this); //代表你所創建的對象,因為它要用到.
}
}
class Component
{
Container myContainer;
public Component(Container c)
{
myContainer = c;
}
}

其中我們開發中最常用到的地方是第二點,消除歧義。

比方說有類
public class A
裡面有幾個變數
private String aa,
private String bb;

this 在這里就代表A ,其實它是對對象A的引用。
我們在用到aa或者bb的時候,this.aa 和 直接用aa 是沒有區別的。
但是假如你在某個方法里也有個變數aa,比如:
public void dosomething(String aa){
this.aa = aa;
//這個時候就this.aa 代表對象A中的變數,而不加this的話,代表方法中的變數。
}

熱點內容
怎樣編程選股 發布:2025-02-02 08:22:02 瀏覽:415
電腦web應用伺服器 發布:2025-02-02 08:05:31 瀏覽:811
電腦存儲內存多少合適 發布:2025-02-02 08:00:15 瀏覽:110
登錄界面android 發布:2025-02-02 07:53:23 瀏覽:842
編譯時註解與運行時註解 發布:2025-02-02 07:53:14 瀏覽:818
怎樣登陸ftp 發布:2025-02-02 07:44:44 瀏覽:637
瘋狂點擊腳本 發布:2025-02-02 07:38:10 瀏覽:73
pss演算法 發布:2025-02-02 07:30:55 瀏覽:748
發信息腳本 發布:2025-02-02 07:03:07 瀏覽:741
l2l3緩存 發布:2025-02-02 06:56:47 瀏覽:524