super


lesson ten                             2018-04-29  14:58:07


super:

  1.可以用来修饰属性、方法、构造器

 1 public class CheckAccount extends Demo1 {
 2 
 3     public double getOverdraft() {
 4         return overdraft;
 5     }
 6 
 7     public void setOverdraft(double overdraft) {
 8         this.overdraft = overdraft;
 9     }
10 
11     private double overdraft;
12 
13     public CheckAccount(int id, double balance, double annualInterestRate,
14             double overdraft) {
15         super(id, balance, annualInterestRate);
16         this.overdraft = overdraft;
17     }
18 
19     public void withdraw(double amount) {
20         if (balance >= amount) {
21             balance -= amount;
22         } else if (overdraft + balance >= amount) {
23             overdraft = overdraft - (amount - balance);
24             balance = 0;
25         } else {
26             System.out.println("透支额度不够");
27         }
28     }
29 }
子类

  2.当子类与父类有同名的属性时,可以通过“super.该属性”显式的调用父类中声明的属性。

 1 public class Demo1 {
 2     private int id;
 3              double balance;
 4     private double annualInterestRate;
 5     
 6     public Demo1(){
 7         System.out.println("这是空参构造器");
 8     }
 9         
10     public Demo1(int id, double balance, double annualInterestRate) {
11         super();
12         this.id = id;
13         this.balance = balance;
14         this.annualInterestRate = annualInterestRate;
15     }
16 
17     public int getId() {
18         return id;
19     }
20 
21     public void setId(int id) {
22         this.id = id;
23     }
24 
25     public double getBalance() {
26         return balance;
27     }
28 
29     public void setBalance(double balance) {
30         this.balance = balance;
31     }
32 
33     public double getAnnualInterestRate() {
34         return annualInterestRate;
35     }
36 
37     public void setAnnualInterestRate(double annualInterestRate) {
38         this.annualInterestRate = annualInterestRate;
39     }
40     
41     public double getMonthlyInterest() {//月利率
42         return this.annualInterestRate/12;
43     }
44     //存钱
45     public void deposit(double amount) {
46         this.balance += amount;
47     }
48     //取钱
49     public void withdraw(double amount) {
50         if (this.balance >= amount) {
51                 this.balance -= amount;            
52         }else {
53             System.out.println("余额不足");
54         }
55     }
56 }
父类

  3.当子类重写父类的方法以后,在子类中若想再显式的调用父类的被重写的方法,就需要使用“super.该方法”


super修饰构造器: 

  1.通过在子类中使用,“super(形参列表)”来显式的调用父类中指定的构造器.

   2.在构造器内部,“super(形参列表)”必须要声明在首行!
   3.在构造器内部,“super(形参列表)”/“this(形参列表)”只能出现一个
   4.当构造器中,不显示调用“super(形参列表)”/“this(形参列表)”其中之一,
   5.默认调用父类空参的构造器。
 建议:设计一个类时,尽量要提供一个空参

 1 public class SubClass {
 2 
 3     public static void main(String[] args) {
 4         Dog dog = new Dog("XiaoMing", "DaHuang", 5);
 5     }
 6 }
 7   
 8 class Creator{
 9     private int age;
10     public int getAge() {
11         return age;
12     }
13     public void setAge(int age) {
14         this.age = age;
15     }
16     
17     public Creator(int age){
18         this.age = age;
19         System.out.println("1");
20         System.out.println("its age is "+this.age);
21     }
22 }
23 
24 class Animal extends Creator{
25     private String name;
26     public String getName() {
27         return name;
28     }
29     public void setName(String name) {
30         this.name = name;
31     }
32     
33     public Animal(String name, int age){
34         super(age);
35         this.name = name;
36         System.out.println("2");
37         System.out.println("its name is "+this.name);
38     }
39 }
40 
41 class Dog extends Animal{
42     private String hostName;
43     public String getHostName() {
44         return hostName;
45     }
46     public void setHostName(String hostName) {
47         this.hostName = hostName;
48     }
49     
50     public Dog(String hostname,String name, int age){
51         super(name, age);
52         this.hostName = hostname;
53         System.out.println("3");
54         System.out.println("Dog belong to "+this.hostName);
55     }
56 }
子类对像实例化的全过程



猜你喜欢

转载自www.cnblogs.com/Fkuennhvo/p/8971110.html