尝试阅读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;
}
并回答:
-
raiseSalary方法有什么用?
答:对所有雇员按照百分比加薪 -
代码中3个Employee对象调用raiseSalary方法所执行的代码一样吗?返回结果一样吗?为什么?
答:执行的代码一样,因为都是执行 e.raiseSalary(5);返回结果不一样,因为是“ raise everyone’s salary by 5%“增加每个人薪水的5%,而每个雇员的薪水不一样,所以代码执行结果每个雇员的增薪不一样。 -
能不能将raiseSalary定义为static?结合该例子,你觉得一般来说什么样的方法应该声明为static?
答:不能,我觉得raiseSalary()方法不仅是类的属性,
更是属于对象的,如果没有具体Employee对象,就不知道具体对象的薪水就无法按百分比计算增薪。
一般来说,属于类但不属于类中具体对象的方法应该声明为static。这样不用创建对象就能调用了。 -
进阶要求:使用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 ;
}