package com.xy.boda;
public class Employee extends Person{
public double salary;
public Employee(){}
public Employee(double salary){
this.salary =salary;
}
public Employee(String name, int age, double salary){
super(name, age);
this.salary=salary;
}
public double computeSalary(int hours, double rate){
double totalSalary;
totalSalary = this.salary+ hours * rate;
return totalSalary;
}
}
子类
package com.xy.boda;
public class Person {
public String name;
public int age;
public Person(){
}//无参数构造
public Person(String name, int age){
this.age =age;
this.name =name;
}
public void sayHello() {
System.out.println("MY name is" + name);
}
}
父类
主函数
package com.xy.boda;
public class EmployeeDemo {
public static void main(String[] args) {
var emp = new Employee("liu",30,5000);
System.out.println("name=" + emp.name);
System.out.println("age=" +emp.age);
emp.sayHello();
System.out.println(emp.computeSalary(10,50.0));
}
}