4.1-互评-OO之继承与static

问答

尝试阅读EmployeeTest.java代码。并回答:

1. raiseSalary方法有什么用?

提高每个雇员的工资(5%的增长)。

2. 代码中3个Employee对象调用raiseSalary方法所执行的代码一样吗?返回结果一样吗?为什么?

执行的代码一样,返回结果不一样,因为他们的基础工资不同,在涨幅相同的情况下自然结果不同。

3. 能不能将raiseSalary定义为static?结合该例子,你觉得一般来说什么样的方法应该声明为static?

在本例中不能,因为salary不是静态的,静态的方法仅能访问静态的数据。一般有两种情况:(1)有时希望定义一个方法,使它的使用完全独立于本类的任何对象;(2)当一个方法需要初始化加载,或者是经常被调用的时候也可以加上static。

4. 进阶要求:使用Java8中的日期类,替换掉Employee类中的hireDay。

替换后运行图片如下:在这里插入图片描述

修改后的代码如下(已经删除了原先的所有的注释,现在存留的注释都表示的是该位置上的原有的代码):

import java.util.*;
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 EmployeeTest
{
    
    
   public static void main(String[] args)
   {
    
    
      
      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);

      
      for (Employee e : staff)
         e.raiseSalary(5);

      
      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;
      LocalDate date = LocalDate.of(year, month, day);// GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
      hireDay = date;// hireDay = calendar.getTime();
   }

   public String getName()
   {
    
    
      return name;
   }

   public double getSalary()
   {
    
    
      return salary;
   }

   public LocalDate getHireDay()  // public Date getHireDay()
   {
    
                                  //{
    
    
      return hireDay;            //return hireDay;
   }                             //}
   

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

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

附件

EmployeeTest.java

import java.util.*;

/**
 * This program tests the Employee class.
 * @version 1.11 2004-02-19
 * @author Cay Horstmann
 */
public class TEST
{
    
    
   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;
}

Java8DateTimeTest.java

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());
		
	}
}

猜你喜欢

转载自blog.csdn.net/qq_51726651/article/details/120694651
oo