this 是当前对象 super 可以调用父类的方法 当你是继承类的时候 并且重载了父类的方法的时候当你要用父类的方法的时候 这时就需要super关键字 指明是调用父类的方法。
class thisDemo { int a; int b; thisDemo() { this(2,3); System.out.println("a="+this.a+" "+"b="+this.b); } thisDemo(int a,int b) { this.a=a; this.b=b; } public static void main(String[] args) { new thisDemo(); } }
public class Father { String name; String age; public Father() { name = "jason"; age = "46"; System.out.println( name + " " + age ); } }public class Son extends Father { String name; String age;
public Son() { name = "peter"; age = "20"; System.out.println( name + " " + age ); } //注意this public Son( String name, String age ) { //这个this表示这个实例,则this.name表示这个实例的name属性, //即前面声明的String name,也因此该构造函数中在name前面加上 //this.就不会去操作其参数name了,那样就没有意义,比如 //name = name,这实际是在操作传进来的那个参数 this.name = name; this.age = age; System.out.println( name + " " + age ); } //注意super public Son() { //如果使用super关键字,就会调用父类的构造函数,此处的输出 //就变成了:jason 46 super(); } }
http://www.google.cn/search?complete=1&hl=zh-CN&newwindow=1&rls=com.microsoft%3A*%3AIE-SearchBox&rlz=1I7GZAZ&q=java+super+this&meta=
super 可以调用父类的方法
当你是继承类的时候 并且重载了父类的方法的时候当你要用父类的方法的时候 这时就需要super关键字 指明是调用父类的方法。
{
int a;
int b;
thisDemo()
{
this(2,3);
System.out.println("a="+this.a+" "+"b="+this.b);
}
thisDemo(int a,int b)
{
this.a=a;
this.b=b;
}
public static void main(String[] args)
{
new thisDemo();
}
}
public class Father
{
String name;
String age; public Father()
{
name = "jason";
age = "46";
System.out.println( name + " " + age );
}
}public class Son extends Father
{
String name;
String age;
public Son()
{
name = "peter";
age = "20";
System.out.println( name + " " + age );
} //注意this
public Son( String name, String age )
{
//这个this表示这个实例,则this.name表示这个实例的name属性,
//即前面声明的String name,也因此该构造函数中在name前面加上
//this.就不会去操作其参数name了,那样就没有意义,比如
//name = name,这实际是在操作传进来的那个参数
this.name = name;
this.age = age;
System.out.println( name + " " + age );
} //注意super
public Son()
{
//如果使用super关键字,就会调用父类的构造函数,此处的输出
//就变成了:jason 46
super();
}
}
super表示父类对象
在每一个子类的构造方法中,隐含包含一个super(),表示自动调用父类无参的构造方法
this除了this. 还有.this