Java类体中的this和super的用法

时间:2008-03-25 15:54:54  来源:  作者:

一、基础知识

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();
    }
}
 



Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=2214996


文章评论

共有 位网友发表了评论 查看完整内容