java - Java调用继承的方法只使用一个对象
问题描述
如何仅使用任何一个类的对象调用 2 个类(继承)的方法。
考虑代码
class Employee
{
void display()
{
System.out.println("Name of class is Employee");
}
void calcSalary()
{
System.out.println("Salary of employee is 10000");
}
}
class Engineer extends Employee
{
void display()
{
System.out.println("Name of class is Engineer");
}
void calcSalary()
{
System.out.println("Salary of engineer is 20000");
}
}
如果我将一个对象声明为
Employee ob = new Employee();
ob.display(); //prints "Name of class is Employee"
ob.calcSalary(); //prints "Salary of employee is 10000"
相似地
Engineer ob = new Engineer();
ob.display(); //prints "Name of class is Engineer"
ob.calcSalary(); //prints "Salary of engineer is 20000"
如何仅使用单个实例(任一类)获得所有这 4 个输出?
解决方案
通过添加super.display()
inhereted 方法的第一行,您可以调用父类的方法。
推荐阅读
- office-js - 尝试通过 v2.0 REST API 获取不可变 ID
- hibernate - 原因:javax.validation.ConstraintViolationException:类验证失败
- javascript - document.getElementById().value 不返回文本框值
- google-cloud-platform - 在不删除和重新创建表的情况下截断 Google 扳手表
- c# - 在我的 c# 应用程序中自动拨打选定的电话号码
- reactjs - 我们如何向 react-native 应用程序发送恒定的图像流并不断更新图像源?
- java - 有没有办法让詹金斯在 X 分钟内不记录任何内容的阶段失败?
- ios - 如何在视图的选定位置设置按钮?
- asp.net-core - 我可以使用 WebCompiler 使用 Github Actions 和 ASP.NET Core 3.1 编译 SASS 文件吗?
- sql - 为什么这个带有 in 子句的查询不返回错误?