javathis是什麼
1. 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;
                 }
            }
2. java this什麼意思
java中的this隨處可見,用法也多,現在整理有幾點:
1. this是指當前對象自己。
當在一個類中要明確指出使用對象自己的的變數或函數時就應該加上this引用。如下面這個例子中: 
public class Hello {
    String s = "Hello";
    public Hello(String s) {
        System.out.println("s = " + s);
        System.out.println("1 -> this.s = " + this.s);
        this.s = s;
        System.out.println("2 -> this.s = " + this.s);
    }
    public static void main(String[] args) {
        Hello x = new Hello("HelloWorld!");
    }
}
運行結果: 
s = HelloWorld! 
1 -> this.s = Hello 
2 -> this.s = HelloWorld!
在這個例子中,構造函數Hello中,參數s與類Hello的變數s同名,這時如果直接對s進行操作則是對參數s進行操作。若要對類Hello的成員變數s進行操作就應該用this進行引用。運行結果的第一行就是直接對構造函數中傳遞過來的參數s進行列印結果; 第二行是對成員變數s的列印;第三行是先對成員變數s賦傳過來的參數s值後再列印,所以結果是HelloWorld!
2. 把this作為參數傳遞
當你要把自己作為參數傳遞給別的對象時,也可以用this。如:
public class A {
    public A() {
        new B(this).print();
    }
    public void print() {
        System.out.println("Hello from A!");
    }
}
public class B {
    A a;
    public B(A a) {
        this.a = a;
    }
    public void print() {
        a.print();
        System.out.println("Hello from B!");
    }
}
運行結果: 
  Hello from A! 
  Hello from B!  
在這個例子中,對象A的構造函數中,用new B(this)把對象A自己作為參數傳遞給了對象B的構造函數。
3. java中this的用法
1. this指當前對象。
當在一個類中要明確指出使用對象變數或函數時加上this引用。如下面例子中: 
public class Hello { 
    String s = "Hello"; 
    public Hello(String s){ 
        System.out.println("s = " + s); 
        System.out.println("1 -> this.s = " + this.s); 
        this.s = s; 
        System.out.println("2 -> this.s = " + this.s); 
    } 
    public static void main(String[] args) { 
    Hello x=new Hello("HelloWorld!"); 
    } 
}
運行結果: 
s = HelloWorld! 
1 -> this.s = Hello 
2 -> this.s = HelloWorld!
在這個例子中,構造函數Hello中,參數s與類Hello的變數s同名,這時直接對s進行操作則是對參數s進行操作。對類Hello的成員變數s進行操作就應該用this進行引用。運行結果的第一行就是直接對構造函數中傳遞過來的參數s進行列印結果;
第二行是對成員變數s的列印;第三行是先對成員變數s賦傳過來的參數s值後再列印,所以結果是HelloWorld!
2. this作為參數傳遞
當你要把自己作為參數傳遞給別的對象時如:
public class A { 
  public A() { 
    new B(this).print(); 
  } 
  public void print() { 
    System.out.println("Hello from A!"); 
  } 
} 
public class B { 
  A a; 
  public B(A a) { 
    this.a = a; 
  } 
  public void print() { 
    a.print(); 
    System.out.println("Hello from B!"); 
  } 
} 
運行結果: 
  Hello from A! 
  Hello from B! 
在這個例子中,對象A的構造函數中,new
B(this)把對象A作為參數傳遞給了對象B的構造函數。
4. java中「this」的用法是什麼
一、指自己所在的對象。
比如在一個方法中,調用其他對象的變數或方法時,可以使用那個對象的對象名,比如aa.abc();
而調用自己所在對象的方法或變數時,不知道別人給起了什麼名,所以直接用this.abc()就可以了。
二、看一個小例子中「this」的用法!
/**
 * @author feng-neusoft
 * 
 * 本示例為了說明this的三種用法!
 */
package test;
public class ThisTest {
    private int i=0;
    //第一個構造器:有一個int型形參
    ThisTest(int i){
       this.i=i+1;//此時this表示引用成員變數i,而非函數參數i
       System.out.println("Int constructor i——this.i:  "+i+"——"+this.i);
       System.out.println("i-1:"+(i-1)+"this.i+1:"+(this.i+1));
       //從兩個輸出結果充分證明了i和this.i是不一樣的!
    }
    //  第二個構造器:有一個String型形參
    ThisTest(String s){
       System.out.println("String constructor:  "+s);
    }
    //  第三個構造器:有一個int型形參和一個String型形參
    ThisTest(int i,String s){
       this(s);//this調用第二個構造器
       //this(i); 
       /*此處不能用,因為其他任何方法都不能調用構造器,只有構造方法能調用他。
       但是必須注意:就算是構造方法調用構造器,也必須為於其第一行,構造方法也只能調
       用一個且僅一次構造器!*/
       this.i=i++;//this以引用該類的成員變數
       System.out.println("Int constructor:  "+i+"/n"+"String constructor:  "+s);
    }
    public ThisTest increment(){
       this.i++;
       return this;//返回的是當前的對象,該對象屬於(ThisTest)
    }
    public static void main(String[] args){
       ThisTest tt0=new ThisTest(10);
       ThisTest tt1=new ThisTest("ok");
       ThisTest tt2=new ThisTest(20,"ok again!");
System.out.println(tt0.increment().increment().increment().i);
       //tt0.increment()返回一個在tt0基礎上i++的ThisTest對象,
       //接著又返回在上面返回的對象基礎上i++的ThisTest對象!
    }
}
運行結果:
Int constructor i——this.i:  10——11
String constructor:  ok
String constructor:  ok again!
Int constructor:  21
String constructor:  ok again!
14
細節問題注釋已經寫的比較清楚了,總結一下,其實this主要要三種用法:
1、表示對當前對象的引用!
2、表示用類的成員變數,而非函數參數,注意在函數參數和成員變數同名是進行區分!其實這是第一種用法的特例,比較常用,所以那出來強調一下。
3、用於在構造方法中引用滿足指定參數類型的構造器(其實也就是構造方法)。但是這里必須非常注意:只能引用一個構造方法且必須位於開始!
還有就是注意:this不能用在static方法中!所以甚至有人給static方法的定義就是:沒有this的方法!雖然誇張,但是卻充分說明this不能在static方法中使用!
5. java 中 .this有什麼用
Student.this是一個Student實例的引用,並沒有進行初始化,正確的說,他早已進行了初始化。
Student.this的應用與this不太一樣。
舉個.this的應用例子:
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MyApplet extends Applet{
public void init(){
Button b=new Button("print");
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
MyApplet.this.print(e);
/*!!看到吧,想要調用print(),就可以這樣。
我的例子還不太到位,因為直接print(e);也可以。
但是假如ActionEvent也有print方法,而我想掉用MyApplet的print就可以這樣用;還有假如我需要調用一個方法為print(MyApplet m);,他需要將MyApplet實例作為參數傳進去,也可以這樣用print(MyApplet.this);。
*/
}
});
setLayout(new BorderLayout());
add(b,"Center");
}
public void print(ActionEvent e){
this.showStatus(String.valueOf(e.getWhen()));
}
}
也就是說.this應用於同一文件內的類或內部類或匿名內部類,一般是不需要這樣用的。
說了這么多不知道你明白了嗎,這個用法不常用,不明白也不是很礙事,到了必須要用他的時候自然會了解。
6. java中的this是什麼意思
this代表當前對象的意思。
這個this肯定是用在某個類的方法里吧。呵呵,我們繼續往下說。
舉個例子:
public class persion{
  private String name=null;
  private int age;
  public void setName(String name){
      this.name=name;
}
  public String getName(){
   return name;
}
}
 在這個類中setName方法中第一個變數用this是為了和本方法的參數this做區別,表示這個name指的是Person這個類的name屬性,而不是name參數,如果去掉這個this,執行完這個方法後,Person類的name屬性仍然為null
getName方法返回的Person類的name,之所以不用this是因為沒有必要,因為編譯器會知道這個name就指的是Person的name而不是其他的name(因為找個方法沒有name變數和它混淆)。當然,這個方法你也可以顯示的使用return this.name,沒錯。 
這些都是最基本的了。開始學只要記住這個就可以了。慢慢的其他的用法也就無師自通了!
7. Java中,關鍵字this是什麼作用
代表當前對象
舉例--類People3,main方法裡面創建2個對象p1,p2,調用有參構造函數,進行初始化賦值
public class People3 {
	private String name;
	private int age;
	
	public People3(){}
	
	public People3(String name,int age){
		this.name=name;//當前對象的姓名
		this.age=age;
	}
	
	public void say(){
		System.out.println(this.name+"調用say方法");//哪個對象調用這個方法,就輸出對象的名字
	}
	
	public static void main(String[] args) {
		People3 p1=new People3("張三",20);
		People3 p2 = new People3("李四",21);
		p1.say();
		p2.say();
	}
}
8. 在java中類名.this得到的是什麼
你這個問題應該是問在3g平台下面把,那麼得到的是上下文對象-context。在j2se得到是當前類的一個對象
9. java中this的作用是什麼呀怎麼用
當類中有兩個同名變數:一個屬於類的成員變數;另一個屬於某個特定的方法(方法中的局部變數),可使用this區分成員變數和局部變數。使用this還可以簡化構造方法的調用。
