[Java Core Technology (Volume I)]-vscode manually compile and run inherited classes

  • Reference-P160~P161

  • There are three main classes: a test class (ManagerTest), a subclass (Manager), and a parent class (Employee)

  • Precautions:
    -1 using javac -d . *.javaprecompiled
    Insert picture description here

    The directory structure
    Insert picture description here
    is as follows: The directory structure will be generated as follows:
    Insert picture description here

  • Run after java com.inheritance.ManagerTest
    Insert picture description here

  • Attach the code of several classes

// com.inheritance.Manager.java
package com.inheritance;

public class Manager extends Employee{
    
    
  private double bonus;

  public Manager(String name, double salary, int year, int month, int day) {
    
    
    super(name, salary, year, month, day);
    bonus = 0;
  }

  public double getSalary() {
    
    
    double baseSalary = super.getSalary();
    return baseSalary + bonus;
  }

  public void setBonus(double b) {
    
    
    bonus = b;
  }
}
// com.inheritance.Employee.java
package com.inheritance;

import java.time.*;

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 getSalary() {
    
    
    return salary;
  }

  public LocalDate getHireDay() {
    
    
    return hireDay;
  }

  public void raiseSalary(double byPercent) {
    
    
    double raise = salary * byPercent / 100;
    salary += raise;
  }

}
// com.inheritance.ManagerTest.java
package com.inheritance;

public class ManagerTest {
    
    
  public static void main(String[] args) {
    
    
    Manager boss = new Manager("Carl Cracker", 80000, 1987, 12, 15);
    boss.setBonus(5000);

    Employee[] staff = new Employee[2];

    staff[0] = new Employee("Harry Hacher", 50000, 1989, 10, 1);
    staff[1] = new Employee("Tommy Tester", 40000, 1990, 3, 15);

    System.out.println("name: " + boss.getName() + ", salary: " + boss.getSalary());

    for(Employee e : staff) {
    
    
      System.out.println("name: " + e.getName() + ", salary: " + e.getSalary());
    }
  }
}

Guess you like

Origin blog.csdn.net/piano9425/article/details/110458993