① java中new和this的用法和作用
new是生成一個對象實例,類名稱 對象名稱=new 類名稱(這裡面可以傳參數,也可以沒有,看你構造函數);
this 就是指當前的對象,
② Java中 A a=new B(this) 是什麼意思
這個語句拆成3部分來看
首先new b()意味著實例化b類,
而this代表著,將用到這個語句的這個類,作為參數傳遞到b的構造函數中去。
最後向上轉型變成其父類a。
③ 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方法中使用!
④ 在JAVA中new 和this的作用,用法,在什麼情況下使用
new只出現在構造方法之前用來生成對象
即:Student
a
=
new
Student();
this
1、當全局變數跟局部變數重名時,表示使用全局變數(此時this指代本類對象)
例有一類class
A{
String
name;
void
setName(String
name){
this.name
=
name;
}
}
2、構造方法相互調用,此時this指代本類類名
注意this只能放在構造方法第一句
如class
B{
String
name;
B(){
this("name");//會自動調用帶String參數的構造方法
}
B(String
name){
this.name
=
name;
}
}
⑤ 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的構造函數。
⑥ java 中this什麼作用 小白提問
this指代的是當前類本身,可以在成員變數和參數同名的時候用來區分參數和成員變數。
⑦ 在java中 Date d = new Date(this); this指的是啥
new Date(this)這個沒想起來是什麼,建議把整個類貼出來。只要記住this對應調用這個方法的實例對象就行了。所以在內部類中對應內部類的實例,如果要使用外部類的this,需要加上類名,如用Person.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作為參數傳遞
當你要把自己作為參數傳遞給別的對象時如:
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的構造函數。
⑨ JAVA中return,this,new,的用法
return:返回
其實它的作用不是結束循環的,而是結束方法的。
this:是當前類的對象引用。簡單的記,它就代表當前類的一個對象。
注意:誰調用這個方法,在該方法內部的this就代表誰。
this的場景:
解決局部變數隱藏成員變數
new:
在一個java文件中寫兩個類:一個基本的類,一個測試類。
注意:文件名稱和測試類名稱一致。
如何使用呢?
創建對象使用。
如何創建對象呢?
格式:類名對象名=new類名();
如何使用成員變數呢?
對象名.變數名
如何使用成員方法呢?
對象名.方法名(...)