一. 论述题(共1题,5分)
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;
}
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());
}
}
1.?(论述题, 5分)
尝试阅读EmployeeTest.java代码。并回答: 1. raiseSalary方法有什么用?
用来计算涨工资后的工资的。用处:可以在批量更新员工的工资的时候使用。
在上面代码中即计算员工涨了5%后的工资。
2. 代码中3个Employee对象调用raiseSalary方法所执行的代码一样吗?返回结果一样吗?为什么?
执行的代码一样,均调用了e.raiseSalary方法。返回结果不一样,因为3个Employee对象本身的工资(salary)就不一样,在都增加了5%后的薪水后,返回的薪水值还是不一样,但是执行的代码时一样的。
3. 能不能将raiseSalary定义为static?结合该例子,你觉得一般来说什么样的方法应该声明为static?
不能。因为static方法不能调用非静态属性。若将raiseSalary定义为static会出现如图所示的报错
?若依照提示将salary也定义为static运行后则会出现如下图所示的错误结果
由图可知,三个对象的薪水都变成了相同的结果。因为salary先是变成了staff[2]中的40000,最后再变成46305.0,最后的结果都变成了46305.0。raise和salary的变化对应如下:
raise=2000.0 //40000*0.05
salary=42000.0 //40000+2000.0
raise=2100.0
salary=44100.0
raise=2205.0
salary=46305.0
所以不能将raiseSalary定义为static。
用static修饰的方法可以用 类名.方法 直接调用,不用的一定要先实例化一个对象然后才可以调用。需要调用静态属性时可以调用静态方法。经常用到的方法,可以声明为static,这样省去了每次new对象的内存空间,因为非static方法,需要new对象(实例化对象)才能调用此方法。声明为static的变量实质上就是全局变量。
4. 进阶要求:使用Java8中的日期类,替换掉Employee类中的hireDay。参考代码见Java8DateTimeTest.java。
|