Java面向对象高级特性-覆盖
原创约 478 字大约 2 分钟...
Java面向对象高级特性-覆盖
注意
本博文仅供学术研究和交流参考,严禁将其用于商业用途。如因违规使用产生的任何法律问题,使用者需自行负责。
概念

示例代码
package com.tencent.chapter07.覆盖;
//员工:总的父类
public class Employee extends Object{
//属性(成员变量)
String name;
String birth;
double salary;
//无参构造函数
public Employee()
{
System.out.println("父类实例化----new Employee()");
}
//带参数构造函数
public Employee(String name,String birth,double salary)
{
this.name = name;
this.birth = birth;
this.salary = salary;
}
//行为(成员方法)
public void getDetail()
{
System.out.println("name:" + name + " birth:" + birth + " salary:" + salary);
}
public double getSalary()
{
return salary;
}
}
package com.tencent.chapter07.覆盖;
/**
* <p>Title: Officer</p>
* <p>
* Description:
//员工:在办公室办公的员工
* </p>
* @author xianxian
* @date 2023年1月4日
*/
public class Officer extends Employee{
//属性(成员变量)
String department;
//无参构造函数
public Officer()
{
super();//1.只能出现在子类的构造器中,且必须是第一行
System.out.println("子类实例化----new Officer()");
}
//带参数构造函数
public Officer(String name,String birth,double salary,String department)
{
super(name, birth, salary);
this.department = department;
}
//行为(成员方法)
public void vehicleAllowance()
{
System.out.println("Officer的交通补贴:300");
}
@Override
public void getDetail() {
//super.getDetail();
System.out.println("name:" + name + " birth:" + birth + " salary:" + salary + " department:" + department);
}
}
package com.tencent.chapter07.覆盖;
public class Test {
public static void main(String[] args) {
Officer lisi = new Officer("李四", "1996-10-22", 10000, "软件开发事业部");
lisi.getDetail();//1.当子类officer重写父类的getDetail方法之后,再调用该方法时,执行子类自己重写过的方法
}
}
分割线
相关信息
以上就是我关于 Java面向对象高级特性-覆盖 知识点的整理与总结的全部内容,希望对你有帮助。。。。。。。
扩展知识
- 重写(覆盖)与 重载的区别
分割线
Powered by Waline v2.15.4