java classname.this_java 中 类名.this与类名.class
1.this指的是當前正在訪問這段代碼的對象,當在內部類中使用this指的就是內部類的對象, 為了訪問外層類對象,就可以使用外層類名.this來訪問。
一般也只在這種情況下使用這種
示例代碼,請注意第17行!代碼來自《Android應用開發揭秘》
復制代碼
public class Activity01 extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
/* 設置顯示main.xml布局 */
setContentView(R.layout.main);
/* findViewById(R.id.button1)取得布局main.xml中的button1 */
Button button = (Button) findViewById(R.id.button1);
/* 監聽button的事件信息 */
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v)
{
/* 新建一個Intent對象 */
Intent intent = new Intent();
/* 指定intent要啟動的類 */
intent.setClass(Activity01.this, Activity02.class);
/* 啟動一個新的Activity */
startActivity(intent);
/* 關閉當前的Activity */
Activity01.this.finish();
}
});
}
}
復制代碼
2.在Java中,每個class都有一個相應的Class對象,當編寫好一個類,編譯完成后,在生成的.class文件中,就產生一個Class對象,用來表示這個類的類型信息。獲得Class實例的三種方式:
1). 利用對象調用getClass()方法獲取該對象的Class實例
2). 使用Class的靜態方法forName(),用類的名字獲取一個Class實例
3). 運用.calss的方式獲取Class實例,對基本數據類型的封裝類,還可以采用.TYPE來獲取對應的基本數據類型的Class實例。
以下是TestClass.java代碼:
public class TestClass {
public static void main(String[] args) {
// 在運行期間,如果我們要產生某個類的對象,java虛擬機會檢測該類型的Class對象是否已被加載。如果沒有加載,java虛擬機會根據類的名稱找到.class文件并加載它。
//當new Point()的時候加載這個類,用forName構造實例的時候也加載該類。 只加載一次
System.out.println("before new Point()");
new Point();
System.out.println("after new Point()");
try {
Class.forName("Line");
} catch (Exception e) {
e.printStackTrace();
}
// 利用對象調用getClass()方法獲取該對象的Class實例
Point pt = new Point();
Class c1 = pt.getClass();
System.out.println(c1.getName()); // 結果:Point
// 使用Class的靜態方法forName(),用類的名字獲取一個Class實例
try {
Class c2 = Class.forName("Point");
System.out.println(c2.getName()); // 結果:Point
Point pp = (Point) c2.newInstance(); //一旦某個類型的Class對象已經被加載到內存,就可以用它來產生該類型的所有對象。
//newInstance()調用類中缺省的構造方法。
pp.output();
} catch (Exception e) {
e.printStackTrace();
}
// 運用.class的方式獲取Class實例(類)
Class c3 = Point.class;
System.out.println(c3.getName()); // 結果:Point
// 運用.calss的方式獲取Class實例(基本類型)
Class c4 = int.class;
System.out.println(c4.getName()); // 結果:int
// 運用.class的方式獲取Class實例(基本數據類型的封裝類)
Class c5 = Integer.TYPE;
System.out.println(c5.getName()); // 結果:int
Class c6 = Integer.class;
System.out.println(c6.getName()); // 結果:java.lang.Integer
}
}
class Point {
static {
System.out.println("loading point");
}
void output() {
System.out.println("x=" + x + ",y=" + y);
}
int x, y;
}
class Line {
static {
System.out.println("loading Line");
}
}
參考:http://blog.csdn.net/zmissm/article/details/11992077?locationNum=8
在一個類的內部使用this表示當前對象的引用,然而有時類的方法內部還有類的定義需要使用外部類的方法時就需要使用this對象,此時this只是內部使用類的對象,在內部類方法定義的類中如何引用外部類此時就要用到類名.this方法。類名.this表示外部類的實例。這樣說有點抽象,看下面的例子:
public classMainActivity?extends Activity {
int count=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//獲取屏幕中的按鈕控件對象
Button button=(Button)findViewById(R.id.Button01);
//為按鈕添加OnClickListener接口實現
button.setOnClickListener(new?View.OnClickListener() {
@Override
public void onClick(View v) {
//獲取線性布局對象
LinearLayout ll=(LinearLayout)findViewById(R.id.lla);
String msg=MainActivity.this.getResources().getString(R.string.button);
//創建一個Button對象
Button tempbutton=new Button(MainActivity.this);
//設置Button控件顯示的內容
tempbutton.setText(msg+(++count));
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
MainActivity是一個外部類,View.OnClickListener是一個內部類,在內部類中使用外部類方法需要使用外部類名.this引用外部類的實例。
參考:http://blog.csdn.net/wuruiaoxue/article/details/46336099
總結
以上是生活随笔為你收集整理的java classname.this_java 中 类名.this与类名.class的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 99乘法表对齐_Java实现九
- 下一篇: java恶作剧小程序_一个Java恶搞小