zoukankan      html  css  js  c++  java
  • inheritance super overrides printMethod in Superclass override重写父方法

    Core Java Web Page http://horstmann.com/corejava.html

    [

      inheritance 

    ]

    package v1ch05.inheritance;
    
    import java.time.LocalDate;
    
    public class Employee {
    
        private String name;
        private double salary;
        private LocalDate hireDay;
    
        public Employee(String name, double salary, int year, int month, int day) {
            this.name = name;
            this.salary = salary;
            hireDay = LocalDate.of(year, month, day);
        }
    
        public String getName() {
            return name;
        }
    
        public double getSalaary() {
            return salary;
        }
    
        public LocalDate getHireDay() {
            return hireDay;
        }
    
        public void raiseSalary(double byPercent) {
            double raise = salary * byPercent / 100;
            salary += raise;
        }
    }
    package v1ch05.inheritance;
    
    public class Manager extends Employee {
        private double bouns;
    
        public Manager(String name, double salary, int year, int month, int day) {
            super(name, salary, year, month, day);
            bouns = 0;
        }
    
        public double getSalary() {
            double baseSalary = super.getSalaary();
            return baseSalary + bouns;
        }
    
        public void setBouns(double b){
            bouns = b;
        }
    }
    package v1ch05.inheritance;
    
    public class ManagerTest {
        public static void main(String[] args) {
            // construct a Manager object
            Manager boss = new Manager("Carl Cracker", 80000, 1987, 12, 15);
            boss.setBouns(5000);
    
            Employee[] staff = new Employee[3];
    
            // fill the staff array qwith Manager and Employee objects
            staff[0] = boss;
            staff[1] = new Employee("harry Hacker", 5000, 1989, 10, 1);
            staff[2] = new Employee("Tommy Tester", 40000, 1990, 3, 15);
    
            // print out information about all Emplyee objects
            for (Employee e : staff)
                System.out.println("name=" + e.getName() + ",salary=" + e.getSalaary() + "");
        }
    }

    super

    Using the Keyword super (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance) https://docs.oracle.com/javase/tutorial/java/IandI/super.html

    Using the Keyword super

    Accessing Superclass Members

    If your method overrides one of its superclass's methods, you can invoke the overridden method through the use of the keyword super. You can also use super to refer to a hidden field (although hiding fields is discouraged). Consider this class, Superclass:

    public class Superclass {
    
        public void printMethod() {
            System.out.println("Printed in Superclass.");
        }
    }
    

    Here is a subclass, called Subclass, that overrides printMethod():

    public class Subclass extends Superclass {
    
        // overrides printMethod in Superclass
        public void printMethod() {
            super.printMethod();
            System.out.println("Printed in Subclass");
        }
        public static void main(String[] args) {
            Subclass s = new Subclass();
            s.printMethod();    
        }
    }
    

    Within Subclass, the simple name printMethod() refers to the one declared in Subclass, which overrides the one in Superclass. So, to refer to printMethod() inherited from SuperclassSubclass must use a qualified name, using super as shown. Compiling and executing Subclass prints the following:

    Printed in Superclass.
    Printed in Subclass
    

    Subclass Constructors

    The following example illustrates how to use the super keyword to invoke a superclass's constructor. Recall from the Bicycle example that MountainBike is a subclass of Bicycle. Here is the MountainBike (subclass) constructor that calls the superclass constructor and then adds initialization code of its own:

    public MountainBike(int startHeight, 
                        int startCadence,
                        int startSpeed,
                        int startGear) {
        super(startCadence, startSpeed, startGear);
        seatHeight = startHeight;
    }   
    

    Invocation of a superclass constructor must be the first line in the subclass constructor.

    The syntax for calling a superclass constructor is

    super();  
    
    or:
    super(parameter list);
    

    With super(), the superclass no-argument constructor is called. With super(parameter list), the superclass constructor with a matching parameter list is called.


    Note: If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the super class does not have a no-argument constructor, you will get a compile-time error.Object does have such a constructor, so if Object is the only superclass, there is no problem.

    If a subclass constructor invokes a constructor of its superclass, either explicitly or implicitly, you might think that there will be a whole chain of constructors called, all the way back to the constructor of Object. In fact, this is the case. It is called constructor chaining, and you need to be aware of it when there is a long line of class descent.

  • 相关阅读:
    从上往下,从左往右
    idea jar包破解
    StarUML 2下载、安装、破解全过程
    windows10 找回windows照片查看器的方法
    Tomcat报错Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986
    Java基于SSM在线学习系统设计与实现
    多个iframe,删除详情页时刷新同级iframe的table list
    Eclipse项目工程导入到IDEA继续开发-超详细
    MySQL用B+树做索引
    彻底禁用Chrome插件停用开发者模式提示插件version.dll
  • 原文地址:https://www.cnblogs.com/rsapaper/p/7931404.html
Copyright © 2011-2022 走看看