一、基礎(chǔ)知識
1、super(參數(shù)):調(diào)用基類中的某一個(gè)構(gòu)造函數(shù)(應(yīng)該為構(gòu)造函數(shù)中的第一條語句)
2、this(參數(shù)):調(diào)用本類中另一種形成的構(gòu)造函數(shù)(應(yīng)該為構(gòu)造函數(shù)中的第一條語句)
3、super: 它引用當(dāng)前對象的直接父類中的成員(用來訪問直接父類中被隱藏的父類中成員數(shù)據(jù)或函數(shù),基類與派生類中有相同成員定義時(shí))
如:super.變量名
super.成員函數(shù)據(jù)名(實(shí)參)
4、this:它代表當(dāng)前對象名(在程序中易產(chǎn)生二義性之處,應(yīng)使用this來指明當(dāng)前對象;假如函數(shù)的形參與類中的成員數(shù)據(jù)同名,這時(shí)需用this來指明成員變量名)
二、應(yīng)用實(shí)例
class Point
{ PRivate int x,y;
public Point(int x,int y)
{
this.x=x; //this它代表當(dāng)前對象名
this.y=y;
}
public void Draw()
{
}
public Point()
{
this(0,0); //this(參數(shù))調(diào)用本類中另一種形成的構(gòu)造函數(shù)
}
}
class Circle extends Point
{
private int radius;
public circle(int x0,int y0, int r )
{
super(x0,y0); //super(參數(shù))調(diào)用基類中的某一個(gè)構(gòu)造函數(shù)
radius=r;
}
public void Draw()
{
super.Draw(); //super它引用當(dāng)前對象的直接父類中的成員
drawCircle();
}} 新聞熱點(diǎn)
疑難解答
圖片精選