JAVA中this和super的用法
1、super(參數):調用父類中的某一個構造函數(應該為構造函數中的第一條語句)
2、this(參數):調用本類中另一種形成的構造函數(應該為構造函數中的第一條語句)
3、super: 它引用當前對象的直接父類中的成員(用來訪問直接父類中被隱藏的父類中成員數據或函數,基類與派生類中有相同成員定義時)
如:super.變量名 super.成員函數據名(實參)
4、this:它代表當前對象名(在程序中易產生二義性之處,應使用this來指明當前對象;如果函數的形參與類中的成員數據同名,這時需用this來指明成員變量名)
二、應用實例
class Point
{ private int x,y;
public Point(int x,int y)
{
this.x=x; //this它代表當前對象名
this.y=y;
}
public void Draw()
{
}
public Point()
{
this(0,0); //this(參數)調用本類中另一種形成的構造函數
}
}
class Circle extends Point
{
private int radius;
public circle(int x0,int y0, int r )
{
super(x0,y0); //super(參數)調用基類中的某一個構造函數
radius=r;
}
public void Draw()
{
super.Draw(); //super它引用當前對象的直接父類中的成員
drawCircle();
}}
posted on 2009-02-04 20:16 dreaming here 閱讀(244) 評論(1) 編輯 收藏 所屬分類: JAVA基礎