*1、this:
-this能出现在实例方法和构造方法中
-this的语法是:“this.” 、“this()”
-this不能使用在静态方法中,this.在大部分情况下是可以省略的。
-this.在区分局部变量和实例变量的时候不能省略。
-this()只能出现在构造方法的第一行,通过当前的构造方法去调用“本类”中其他的构造方法,目的是代码复用。
*2、super:
-super能出现在实例方法和构造方法中
-super的语法是:“super.”、“super()”
-super不能使用在静态方法中,super.在大部分情况下是可以省略的。
-super()只能出现在构造方法的第一行,通过当前的构造方法去调用“父类”中其他的构造方法。
目的是:
创建子类对象的时候,先初始化父类型特征。
*3、结论:
当一个构造方法第一行,既没有this()又没有super()的话,默认有一个super(),表示通过当前子类的构造方法调用父类的无参数构造方法。所以必须保证父类的无参数构造方法是存在的。
*4、注意:
-this()与super()不能共存,他们都是只能出现在构造分的第一行。
-无论怎样,父类的构造方法是一定会执行的。
public class superTest01 {
public static void main(String [] args){
new B();
}
}
class A{
public A(){
System.out.println("A类的无参数构造方法");
}
//一个类如果没有手动提供任何构造方法,系统会默认提供一个无参数的构造方法。
//一个类中如果手动提供了一个构造方法,那么无参数构造系统不在提供
public A(int i){
System.out.println("A类的有参数构造方法(int)");
}
}
class B extends A{
public B(){
// super(567);//调用父类中有参数的构造方法
this("李白");
System.out.println("B类的无参数构造方法");
}
public B(String name){
super();
System.out.println("B类的有参数构造方法(String)");
}
}
运行结果:
A类的无参数构造方法 B类的有参数构造方法(String) B类的无参数构造方法
*5、一些关于构造函数与this关键字的补充:
—this可以使用在哪?
1、可以使用在实例方法中,代表当前对象【语法格式:this.】
2、可以使用在构造方法中,通过当前的构造方法调用其他构造方法【语法格式:this(实参)】重点:this()这种语法只能出现在构造函数的第一行。
public class Thisgjz04 {
private int year;
private int month;
private int day;
public Thisgjz04(){
// System.out.println();
// this(1970,1,2);错误
this(1970,4,2);//正确
// this.year=1900;
// this.month=1;
// this.day=1;正确
//以上代码可以通过调用另一个构造方法来完成,但前提是不能创建新的对象。
//new Thisgjz04(1888,1,1);错误,创建了一个全新的对象
}
//构造函数
public Thisgjz04(int year,int month,int day){
this.year=year;
this.month=month;
this.day=day;
}
public void setYear(int year){
this.year=year;
}
public void setMonth(int month){
this.month=month;
}
public void setDay(int day){
this.day=day;
}
public int getYear(){
return year;
}
public int getMonth(){
return month;
}
public int getDay(){
return day;
}
public void print_(){
System.out.println(year+"年"+month+"月"+day+"日");
}
public static void main(String [] args){
Thisgjz04 time1=new Thisgjz04();
System.out.println(time1.getYear()+" "+time1.getMonth()+" "+time1.getDay());
time1.print_();
System.out.println(time1.getYear()+"."+time1.getMonth()+"."+time1.getDay());
Thisgjz04 time2=new Thisgjz04(2008,8,8);
time2.print_();
}
}
运行结果:
1970 4 2 1970年4月2日 1970.4.2 2008年8月8日
*6、一些关于构造函数与super关键字的补充:
在适当的时间使用:super(实际参数列表);
public class superTest02 {
public static void main(String [] args){
CreditAccount ca1=new CreditAccount();
System.out.println(ca1.getActno()+","+ca1.getBalance()+","+ca1.getCredit());
CreditAccount ca2=new CreditAccount("111",234.0,45.0);
System.out.println(ca2.getActno()+","+ca2.getBalance()+","+ca2.getCredit());
}
}
class Account{
private String actno;
private double balance;
public Account(){//构造方法
}
public Account(String actno,double balance){
this.actno=actno;
this.balance=balance;
}
public void SetActno(String actno){
this.actno=actno;
}
public String getActno(){
return actno;
}
public void SetBalance(double balance){
this.balance=balance;
}
public double getBalance(){
return balance;
}
}
class CreditAccount extends Account{
private double credit;
public CreditAccount(String actno,double balance,double credit){
// this.actno=actno;
// this.balance=balance;//errror,属性为私有,只能在本类中访问
super(actno,balance);//super代表的是“当前对象this”的父类型特征
this.credit=credit;
}
public CreditAccount() {
}
public void SetCredit(double credit){
this.credit=credit;
}
public double getCredit(){
return credit;
}
}
null,0.0,0.0 111,234.0,45.0



