1、super(参数):调用基类中的某一个构造函数(应该为构造函数中的第一条语句) 2、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(); }}
jackson416(DD | 问世间小裤衩是何物,为何.....) ( ) 信誉:100 Blog 2006-11-28 11:42:40 得分: 0 的例子就是来自 thinking in javalz可以好好读读这本书
class Base { private String data; Base( String data){ this.data = data; } public void showData(){ System.out.println( data ); System.out.println( "int base" ); } } public class Derive extends Base{ public Derive( String data ){ super(data);//调用父类构造函数 } public Derive(){ this("default value");//调用自已的另外一个构造函数 } public void showData(){ super.showData();//调用父类的函数 System.out.println( "in derive" ); } public static void main( String[] args){ Derive derive = new Derive("custom value"); derive.showData(); Derive derive2 = new Derive(); derive2.showData(); } }
super是父类
比如:this(参数):调用本类中另一种形成的构造函数
super: 它引用当前对象的直接父类中的成员
这两句话怎么解释
理解不了
{
private String data;
Base( String data){
this.data = data;
}
public void showData(){
System.out.println( data );
System.out.println( "int base" );
}
}
public class Derive extends Base{
public Derive( String data ){
super(data);//调用父类构造函数
}
public Derive(){
this("default value");//调用自已的另外一个构造函数
}
public void showData(){
super.showData();//调用父类的函数
System.out.println( "in derive" );
}
public static void main( String[] args){
Derive derive = new Derive("custom value");
derive.showData();
Derive derive2 = new Derive();
derive2.showData();
}
}