super

package bsupertest01;

//在成员方法 - super.name 不用子类改写后的name,要用回父类的name

//在构造方法 - super() 通过子类的构造方法调用父类的构造方法。要放在构造语句的第一行。
// 调用了父类中的构造方法,但并不会创建父类对象

//员工 public class Employee { String name = "张三"; //成员方法 public void work(){ System.out.println("员工在工作!"); } }
package bsupertest01;

/*
* super:不用子类改写后的东西,要用回父类的
* * 1. super不是引用类型,super中存储的不是内存地址,super指向的不是父类对象 * 2. super代表的是当前子类对象中的父类型特征 * * 3. 什么时候使用 super? * 子类和父类中都有某个数据,例如,子类和父类中都有name这个属性 * 如果要在子类中访问父类中的name属性,需要使用 super * * 4, super可以用在什么地方? * 第一:super可以用在成员方法中,不能用在静态方法中 * 第二:super可以用在构造方法中 */ public class Manager extends Employee{ String name = "李四"; //子类将父类中的work方法重写了 public void work(){ System.out.println("经理在工作!"); } //成员方法,有对象的存在 public void m1(){ //this.work(); //work(); super.work(); System.out.println(this.name); //李四 System.out.println(name); //李四 System.out.println(super.name); //张三 } /* //this和super相同,都不能使用在静态方法中
//因为没有创建对象 public static void m1(){ System.out.println(super.name); }
*/ }
package bsupertest01;

public class Test01 {

    public static void main(String[] args) {

        Manager m = new Manager();
        
        m.m1(); //员工在工作!
    }

}

 

 

package bsupertest02;

//账户
public class Account {

    private String actno;
    private double balance;
    
    //构造方法
    public Account(){
        //默认也有super(); 因为默认继承Object
        System.out.println("无参数构造方法执行");
    }
    
    public Account(String actno, double balance) {
        this.actno = actno;
        this.balance = balance;
    }
    
    //setter and getter
    public String getActno() {
        return actno;
    }
    
    public void setActno(String actno) {
        this.actno = actno;
    }
    
    public double getBalance() {
        return balance;
    }
    
    public void setBalance(double balance) {
        this.balance = balance;
    }
    
}
package bsupertest02;

/*
 * super关键字用在构造方法中:
 *     语法:super(实参);
 * 
 * 作用:通过子类的构造方法去调用父类的构造方法
 * 
 * 语法规则:
 *     一个构造方法第一行如果没有this(...);也没有显示的去调用super(...)
 *     系统会默认调用 super();
 * 
 * 注意:super(...);的调用只能放在构造方法的第一行
 * 
 * 所以super(...) 和 this(...)不能共存
 * 
 * super(...);调用了父类中的构造方法,但是并不会创建父类对象
 * 
 * 在java语言中只要创建java对象,那Object中的无参数构造方法一定会执行
 * 
 * 单例模式的缺点:单例模式的类型无法被继承,因为构造方法被私有化
 */
public class DebitAccount extends Account{

    //独特属性
    private double debit;
    
    //Constructor
    public DebitAccount(){
        //有默认的super();
    }
    
    public DebitAccount(String actno, double balance, double debit){
        /*
         * 由于这两个参数是父类的私有,不能这样调用
         * this.actno = actno;
         * this.balance = balance;
         */
        //通过子类的构造方法去调用父类的构造方法
        //作用是:给当前子类对象中的父类型特征赋值
        super(actno, balance);

        this.debit = debit;
        
    }

    //setter and getter
    public double getDebit() {
        return debit;
    }

    public void setDebit(double debit) {
        this.debit = debit;
    }

}
package bsupertest02;

public class Test02 {

    public static void main(String[] args) {

        //调用无参数构造方法
        DebitAccount da = new DebitAccount();
    }

}

 

posted @ 2020-08-13 20:09  Lerresino  阅读(160)  评论(0)    收藏  举报