導航:首頁 > 編程語言 > javaclassthis

javaclassthis

發布時間:2022-08-08 16:28:05

A. java中類名.this和這個類的對象有區別嗎

this只能用於方法方法體內。當一個對象創建後,Java虛擬機就會給這個對象分配一個引用自身的指針,這個指針的名字就是 this。
因此,this只能在類中的非靜態方法中使用,靜態方法和靜態的代碼塊中絕對不能出現this。
並且this只和特定的對象關聯,而不和類關聯,同一個類的不同對象有不同的this

B. java,類名.this

有時候,我們會用到一些內部類和匿名類。當在匿名類中用this時,這個this則指的是匿名類或內部類本身。這時如果我們要使用外部類的方法和變數的話,則應該加上外部類的類名。如下面這個例子:
public
class
A
{
int
i
=
1;
public
A()
{
Thread
thread
=
new
Thread()
{
public
void
run()
{
System.out.println("內部類run");
A.this.run();//調用外部類的run方法,輸出
外部類run
};
this.run();//調用內部類的run方法
thread.start();
}
public
void
run()
{
System.out.println("外部類run");
}
}
在上面這個例子中,
thread是一個匿名類對象,在它的定義中,它的run函數里用到了外部類的run函數。這時由於函數同名,直接調用就不行了。這時有兩種辦法,一種就是把外部的
run
函數換一個名字,但這種辦法對於一個開發到中途的應用來說是不可取的。那麼就可以用這個例子中的辦法用
外部類的類名加上this引用來說明要調用的是外部類的方法run。

C. java的.class和.this是什麼什麼是封閉類為什麼只有封閉類才可以使用.this

你可以這樣理解,.this是指當前運行時實例化的這個特定對象,因為只有封裝的類才能實例化出對象,所以是有封裝類才能使用.this;.class是運行時這個實例化對象的類信息,用於反射

D. 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);
}
}

E. 在java中類名.this得到的是什麼

這個一般在內部類裡面用。。類名.this表示 那個類名所代表的類的對象。。。

比如class A {
public void method(){
A.this就是表示A的對象。。在這種情況下和this是一樣的
}

class B {
void method1() {
A.this還是表示A的對象。。但是這里是在內部類裡面。。所以這里的this就是內部類B的對象了。。但是我們經常會在內部類裡面調用外部的東西。。所以就用A.this這種方式就行了
}
}
}

F. JAVA中Class c = this;什麼意思

將本類的實例賦給引用c
若該類名為A,引用為a,當執行到這句時,a的引用就賦給了c,誰執行,就把誰賦給c

G. 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的話,代表方法中的變數。
}

閱讀全文

與javaclassthis相關的資料

熱點內容
做解壓的東西x 瀏覽:983
解壓縮exe文件 瀏覽:62
汽車沒有解壓行嗎 瀏覽:320
海南省分布式伺服器雲主機 瀏覽:31
世紀江湖聊天室源碼 瀏覽:248
阿里網盤的文件如何解壓 瀏覽:781
簡單淘寶客源碼 瀏覽:680
煎餅解壓視頻教程全集 瀏覽:340
平行線pdf 瀏覽:631
android鎖屏不退出程序運行 瀏覽:635
sap連接的伺服器地址是 瀏覽:426
linuxshell腳本從入門到精通 瀏覽:725
python進制均值 瀏覽:629
pdfformac 瀏覽:318
用虛擬伺服器是什麼目的 瀏覽:192
壓縮機阿里巴巴 瀏覽:637
主圖指標源碼回踩 瀏覽:162
怎麼驗證伺服器埠 瀏覽:612
如何添加密碼卡 瀏覽:671
2021好聲音在哪個app觀看 瀏覽:126