4.1-互评-OO之继承与static-阅读EmployeeTest.java代码

这篇博客探讨了Java编程中的EmployeeTest类,它用于测试Employee类。代码展示了如何初始化员工对象,统一提高员工薪资5%,以及使用Date类处理雇佣日期。此外,还讨论了方法的静态性,强调非静态方法如raiseSalary()与对象状态的关联性。最后,提到了Java 8中的日期时间API,并给出了示例代码片段。
摘要由CSDN通过智能技术生成

尝试阅读EmployeeTest.java代码。

import java.util.*;

/**
 * This program tests the Employee class.
 * @version 1.11 2004-02-19
 * @author Cay Horstmann
 */
public class EmployeeTest
{
   public static void main(String[] args)
   {
      // fill the staff array with three Employee objects
      Employee[] staff = new Employee[3];

      staff[0] = new Employee("Carl Cracker", 75000, 1987, 12, 15);
      staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
      staff[2] = new Employee("Tony Tester", 40000, 1990, 3, 15);

      // raise everyone's salary by 5%
      for (Employee e : staff)
         e.raiseSalary(5);

      // print out information about all Employee objects
      for (Employee e : staff)
         System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay="
               + e.getHireDay());
   }
}

class Employee
{
   public Employee(String n, double s, int year, int month, int day)
   {
      name = n;
      salary = s;
      GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
      // GregorianCalendar uses 0 for January
      hireDay = calendar.getTime();
   }

   public String getName()
   {
      return name;
   }

   public double getSalary()
   {
      return salary;
   }

   public Date getHireDay()
   {
      return hireDay;
   }

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

   private String name;
   private double salary;
   private Date hireDay;
}

并回答:

  1. raiseSalary方法有什么用?
    答:对所有雇员按照百分比加薪

  2. 代码中3个Employee对象调用raiseSalary方法所执行的代码一样吗?返回结果一样吗?为什么?
    答:执行的代码一样,因为都是执行 e.raiseSalary(5);返回结果不一样,因为是“ raise everyone’s salary by 5%“增加每个人薪水的5%,而每个雇员的薪水不一样,所以代码执行结果每个雇员的增薪不一样。

  3. 能不能将raiseSalary定义为static?结合该例子,你觉得一般来说什么样的方法应该声明为static?
    答:不能,我觉得raiseSalary()方法不仅是类的属性,
    更是属于对象的,如果没有具体Employee对象,就不知道具体对象的薪水就无法按百分比计算增薪。
    一般来说,属于类但不属于类中具体对象的方法应该声明为static。这样不用创建对象就能调用了。

  4. 进阶要求:使用Java8中的日期类,替换掉Employee类中的hireDay。参考代码见Java8DateTimeTest.java。

package java8time;

import java.time.DayOfWeek;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class Java8DateTimeTest {
	public static boolean isMondayToFriday(){
		LocalDateTime now = LocalDateTime.now();
		DayOfWeek dayOfWeek = now.getDayOfWeek();
		System.out.println(dayOfWeek);
		if(dayOfWeek!=DayOfWeek.SATURDAY && dayOfWeek!=DayOfWeek.SUNDAY)
			return true;
		return false;
	}

	public static void main(String[] args) {
		LocalTime localTime = LocalTime.of(0, 0,0);//无时区概念
		System.out.println(localTime);
		LocalDate localDate = LocalDate.of(1998, 10, 31);
		System.out.println(localDate);
		ZonedDateTime zonedDateTime = ZonedDateTime.of(localDate,localTime,ZoneId.of("Asia/Shanghai"));//有时区概念
		System.out.println(zonedDateTime);
		
		System.out.println(zonedDateTime.toEpochSecond());机器观点当前秒数
		System.out.println(zonedDateTime.toInstant());
		System.out.println(zonedDateTime.toInstant().toEpochMilli());//机器观点当前毫秒数
		
		System.out.println(LocalTime.now());//当前时间,当前系统默认时区
		System.out.println(LocalDate.now());//当前日期,当前系统默认时区
		System.out.println(LocalDateTime.now());//当前日期与时间,当前系统默认时区
		LocalDate nowDate = LocalDate.now();
		LocalTime nowTime = LocalTime.now();
		System.out.println(OffsetDateTime.of(nowDate, nowTime, ZoneOffset.UTC));//
		System.out.println(OffsetDateTime.now().toEpochSecond());
		
		System.out.println("Year, YearMonth, Month, MonthDay");
		
		System.out.println("TemporalAmount");
		String pattern = "E MM/dd/yyyy";
		System.out.println(LocalDate.of(1989, 9, 22).plusDays(5).plusMonths(6).plusWeeks(3).format(DateTimeFormatter.ofPattern(pattern)));
		
		System.out.println(isMondayToFriday());
		
		Duration thirtyDay = Duration.ofDays(30);
        System.out.println(thirtyDay.toString());
		
	}
}

答:




 public Employee(String n, double s, int year, int month, int day)
   {
      name = n;
      salary = s;
      LocalDateTime now = LocalDateTime.now();
      GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
      // GregorianCalendar uses 0 for January
      hireDay = now ;
   }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值