勇气给我前进的力量作文

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

构造函数中的super和this的使⽤
super⽤于调⽤⽗类构造函数的部分,其必须出现在构造函数的第⼀⾏。

super在调⽤时第⼀件事就是去执⾏⽗类构造函数的部分,所执⾏的⽗类构造函数与super()括号中的参数相对应。

this⽤于在⼀个构造函数中调⽤同⼀个类另⼀个构造函数,其也必须是第⼀⾏语句。

super和this不能同时出现在⼀个构造函数中,其两个在使⽤时必须出现在构造函数的第⼀⾏语句,其区别为super调⽤⽗类构造函数,this调⽤⾃⾝类的构造函数。

为便于理解,可以将this理解为⾃⼰,也就是这个类本⾝,因此调⽤this是调⽤⾃⼰类的构造函数。

public abstract class Animal {
private String name;
public String getName() {
return name;
}
public Animal() {
}
public Animal(String theName) {
name=theName;
}
public Animal(String x,String getname) {
}
}
public class Hippo extends Animal{
String name;
public Hippo() { //编写此构造函数⽗类需也有⽆参构造函数
String test;
}
public Hippo(String name) {
super(name); //使⽤super⽅法将name传给⽗类Animal
System.out.println("big");
}
public Hippo(String x,String name) {
this(name); //this为调⽤只有⼀个string参数的构造函数,即调⽤上⾯的构造函数输出big
//super(name); //super和this只能调⽤⼀个
System.out.println(x);
}
}
输出结果为
big
Buffy。

相关文档
最新文档