Java中的this和super的用法
来源:互联网 发布:罗马圣保罗大教堂知乎 编辑:程序博客网 时间:2024/05/04 20:30
一、基础知识
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();
}}
...{ 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();
}}
- Java中的this和super的用法
- Java中的Super和this的用法
- Java中的this和super的用法
- Java中的this和super的用法
- Java类体中的this和super的用法
- Java类体中的this和super的用法
- Java类体中的this和super的用法
- Java中的关键字Super和This的用法
- java中的this 与super的用法
- Java的this和super的用法
- Java中this和super的用法
- java中this和super的用法
- java中this和super的用法
- Java 中this和super的用法
- java中this和super的用法
- java中super和this的用法
- 【JAVA】this和super关键字的用法
- Java基础:this和super的用法
- 将物理机器转化为虚拟机器
- asp.net 2.0 自定义 基于 Table 的 Profile Provide
- 段首缩进的格式指定
- 在网页中显示QQ在线状态
- 恶意代码
- Java中的this和super的用法
- java中this详解
- DOS下IIS服务命令
- 进程间通讯总结
- 几款流行的NET代码生成器
- 进程间通讯《一》
- 7章2节while循环
- 7章4节break和continue
- 7章4节实现dowhile