4.1 面向对象程序设计概述
4.1.1 类
4.1.2 对象
4.1.3 识别类
4.1.4 类之间的关系
4.2 使用预定义类
4.2.1 对象与对象变量
4.2.2 Java类库中的LocalData类
4.2.3 更改器方法与访问器方法
import java.time.*;
/**
* @version 1.5 2015-05-08
* @author Cay Horstmann
*/
public class CalendarTest
{
public static void main(String[] args)
{
LocalDate date = LocalDate.now();
int month = date.getMonthValue();
int today = date.getDayOfMonth();
date = date.minusDays(today - 1); // Set to start of month
DayOfWeek weekday = date.getDayOfWeek();
int value = weekday.getValue(); // 1 = Monday, ... 7 = Sunday
System.out.println("Mon Tue Wed Thu Fri Sat Sun");
for (int i = 1; i < value; i++)
System.out.print(" ");
while (date.getMonthValue() == month)
{
System.out.printf("%3d", date.getDayOfMonth());
if (date.getDayOfMonth() == today)
System.out.print("*");
else
System.out.print(" ");
date = date.plusDays(1);
if (date.getDayOfWeek().getValue() == 1) System.out.println();
}
if (date.getDayOfWeek().getValue() != 1) System.out.println();
}
}
4.3 用户自定义类
4.3.1 Employee类
import java.time.*;
/**
* This program tests the Employee class.
* @version 1.12 2015-05-08
* @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
{
private String name;
private double salary;
private LocalDate hireDay;
public Employee(String n, double s, int year, int month, int day)
{
name = n;
salary = s;
hireDay = LocalDate.of(year, month, day);
}
public String getName()
{
return name;
}
public double getSalary()
{
return salary;
}
public LocalDate getHireDay()
{
return hireDay;
}
public void raiseSalary(double byPercent)
{
double raise = salary * byPercent / 100;
salary += raise;
}
}
4.3.2 多个源文件的使用
4.3.3 剖析Employee类
4.3.4 从构造器开始
4.3.5 隐式参数与显式参数
4.3.6 封装的优点
4.3.7 基于类的访问权限
4.3.8 私有方法
4.3.9 final实例域
4.3静态域和静态方法
4.4.1 静态域
4.4.2 静态常量
4.4.3 静态方法
4.4.4 工厂方法
4.4.5 main方法
4.5 方法参数
4.6 对象构造
4.6.1 重载
4.6.2 默认域初始化
4.6.3 无参数的构造器
4.6.4 显式域初始化
4.6.5 参数名
4.6.6 调用另一个构造器
4.6.7 初始化块
import java.util.*;
/**
* This program demonstrates object construction.
* @version 1.01 2004-02-19
* @author Cay Horstmann
*/
public class ConstructorTest
{
public static void main(String[] args)
{
// fill the staff array with three Employee objects
Employee[] staff = new Employee[3];
staff[0] = new Employee("Harry", 40000);
staff[1] = new Employee(60000);
staff[2] = new Employee();
// print out information about all Employee objects
for (Employee e : staff)
System.out.println("name=" + e.getName() + ",id=" + e.getId() + ",salary="
+ e.getSalary());
}
}
class Employee
{
private static int nextId;
private int id;
private String name = ""; // instance field initialization
private double salary;
// static initialization block
static
{
Random generator = new Random();
// set nextId to a random number between 0 and 9999
nextId = generator.nextInt(10000);
}
// object initialization block
{
id = nextId;
nextId++;
}
// three overloaded constructors
public Employee(String n, double s)
{
name = n;
salary = s;
}
public Employee(double s)
{
// calls the Employee(String, double) constructor
this("Employee #" + nextId, s);
}
// the default constructor
public Employee()
{
// name initialized to ""--see above
// salary not explicitly set--initialized to 0
// id initialized in initialization block
}
public String getName()
{
return name;
}
public double getSalary()
{
return salary;
}
public int getId()
{
return id;
}
}
对象析构与finalize方法
4.7 包
7.1 类的导入
4.7.2 静态导入
4.7.3 把类放入包中
4.7.4 包作用域
4.8 类路径
4.8.1 设置类路径
4.9 文档注释
4.9.1 注释的插入
4.9.2 类注释
4.9.3 方法注释
4.9.4 域注释
4.9.5 通用注释
4.9.6 包与概述注释
4.10 类设计技巧