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 Bicycleexample 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.

 
 
 

inheritance super overrides printMethod in Superclass override重写父方法的更多相关文章

  1. Java:面向对象(继承,方法的重写(overide),super,object类及object类中方法的重写,父子类代码块执行顺序)

    继承: 1.继承是对某一匹类的抽象,从而实现对现实世界更好的建模. 2.提高代码的复用性. 3.extends(扩展),子类是父类的扩展. 4.子类继承父类可以得到父类的全部属性和方法.(除了父类的构 ...

  2. 方法重写和方法重载;this关键字和super关键字

    1:方法重写和方法重载的区别?方法重载能改变返回值类型吗? 方法重写: 在子类中,出现和父类中一模一样的方法声明的现象. 方法重载: 同一个类中,出现的方法名相同,参数列表不同的现象. 方法重载能改变 ...

  3. 10 Things ASP.NET Developers Should Know About Web.config Inheritance and Overrides(转)

    10 Things ASP.NET Developers Should Know About Web.config Inheritance and Overrides Wednesday, Janua ...

  4. c#中override重写和new隐藏

    最近学习c#,昨晚看书看到多态.由于个人本身是从事java开发,于是拿来做对比便是自然的. 进入主题吧. c#中,子类要重写基类的方法,必须要基类声明中带有virtual关键字方法或者带有abstra ...

  5. 【c# 学习笔记】使用virtual和override关键字实现方法重写

    只有基类成员声明为virtual或abstract时,才能被派生类重写:而如果子类想改变虚方法的实现行为,则必须使用override关键字. public class Animal { private ...

  6. 使用super函数----增量重写普通方法和构造方法

    使用super函数----增量重写普通方法和构造方法 在子类中如果重写了超类的方法,通常需要在子类方法中调用超类的同名方法,也就是说,重写超类的方法,实际上应该是一种增量的重写方式,子类方法会在超类的 ...

  7. overload(重载) 和 override(重写)的区别

    overload(重载): 重载是基于一个类中,方法名相同,参数列表不同(如果参数列表相同时,参数的类型要不同),与返回值和访问修饰符都无关 如果在面试中就直接说:"同名不同参"  ...

  8. EF里查看/修改实体的当前值、原始值和数据库值以及重写SaveChanges方法记录实体状态

    本文目录 查看实体当前.原始和数据库值:DbEntityEntry 查看实体的某个属性值:GetValue<TValue>方法 拷贝DbPropertyValues到实体:ToObject ...

  9. java重写equals方法

    @Override public int hashCode() { return task.getId(); } @Override public boolean equals(Object obj) ...

随机推荐

  1. 【Luogu】P3052摩天大楼里的奶牛(状压DP)

    参见ZHT467的题解. f[i]表示在i这个集合下的最少分组数和当前组最少的容量. 从1到(1<<n)-1枚举i,对于每个i枚举它的子奶牛,然后重载运算符计算. 代码如下 #includ ...

  2. Spring-IOC源码解读2.1-BeanDefinition的Resource定位

    Spring通过ResourceLoader来处理得到的Resource.在前面我们知道容器初始化是以refresh()方法为入口的,内部的实现首先准备上下文,然后通过obtainFreshBeanF ...

  3. mock数据。根据表中一天的数据模拟其他日期的数据

    package test; import java.sql.*; import java.text.SimpleDateFormat; import java.util.*; import java. ...

  4. jquery知识汇总

    jQuery 选择器 选择器                  实例                                   选取 *                          $ ...

  5. ci框架——数据库(增删改查)

    1:配置数据库(application/config/database.php)修改 $db['default'] = array( 'dsn' => '', 'hostname' => ...

  6. go--常量&运算符

    常量const 1.常量声明: const ( a = b c ) fmt.Println(a) fmt.Println(b) fmt.Println(c)// ======222 ====: 2.并 ...

  7. Highcharts 动态制作3D柱状图

    学习参考菜鸟网站:http://www.runoob.com/highcharts/highcharts-tutorial.html 我是通过后端返回设备数据,进行前端出图,效果如下: 代码如下: d ...

  8. Paul Graham:梦寐以求的编程语言

    我的朋友曾对一位著名的操作系统专家说他想要设计一种真正优秀的编程语言.那位专家回答,这是浪费时间,优秀的语言不一定会被市场接受,很可能无人使用,因为语言的流行不取决于它本身.至少,那位专家设计的语言就 ...

  9. UVA 11346 Probability

    题目描述 PDF 输入输出格式 输入格式: 输出格式: 输入输出样例 输入样例#1: 3 10 5 20 1 1 1 2 2 0 输出样例#1: 23.348371% 0.000000% 100.00 ...

  10. jquery+css实现邮箱自动补全

    今天在公司做一个电子商务网站的注册会员时,要求用户在电子邮箱文本框中输入时,给与热点提示常用的电子邮箱,帮助用户选择,提高体验效果.下面是用Jquery+css实现的邮箱自动补全,供大家参考和学习. ...