写法一:
    要写的类作为public calss,添加main方法用于测试

    1. import java.time.LocalDate;
    2. public class Employee {
    3. private String name;
    4. private double salary;
    5. private LocalDate hireDay;
    6. public Employee(String n, double s, int year, int month, int day) {
    7. name = n;
    8. salary = s;
    9. hireDay = LocalDate.of(year, month, day);
    10. }
    11. public String getName() {
    12. return name;
    13. }
    14. public double getSalary() {
    15. return salary;
    16. }
    17. public LocalDate getHireDay() {
    18. return hireDay;
    19. }
    20. public void raiseSalary(double byPercent) {
    21. double raise = salary * byPercent / 100;
    22. salary += raise;
    23. }
    24. /*
    25. public static void main(String[] args){
    26. // to do some tests
    27. }
    28. */
    29. }

    写法二:
    要写的类作为public class,再写一个测试类
    捕获.PNG

    写法三:
    要写的类作为public calss,测试类单独存放

    写法四:
    要写的类作为class,测试类作为public class

    1. import java.time.*;
    2. /*
    3. * This program tests the Employee class
    4. * @version 1.0 2020-05-07
    5. * @author Java Core Volume 1 page 104
    6. */
    7. public class EmployeeTest {
    8. public static void main(String[] args) {
    9. Employee[] staff = new Employee[3];
    10. staff[0] = new Employee("Alice", 7500, 1987, 12, 15);
    11. staff[1] = new Employee("Bob", 5000, 1989, 10, 1);
    12. staff[2] = new Employee("Carl", 4000, 1990, 3, 15);
    13. // raise everyone's salary by 5%
    14. for(Employee e : staff) {
    15. e.raiseSalary(5);
    16. }
    17. // print out info about all employees
    18. for(Employee e : staff) {
    19. System.out.println("name=" + e.getName() + ", salary=" + e.getSalary()
    20. + ", hireDay=" + e.getHireDay());
    21. }
    22. }
    23. }
    24. class Employee{
    25. private String name;
    26. private double salary;
    27. private LocalDate hireDay;
    28. public Employee(String n, double s, int year, int month, int day) {
    29. name = n;
    30. salary = s;
    31. hireDay = LocalDate.of(year, month, day);
    32. }
    33. public String getName() {
    34. return name;
    35. }
    36. public double getSalary() {
    37. return salary;
    38. }
    39. public LocalDate getHireDay() {
    40. return hireDay;
    41. }
    42. public void raiseSalary(double byPercent) {
    43. double raise = salary * byPercent / 100;
    44. salary += raise;
    45. }
    46. }