使用预定义类
package 使用预定义类;
import java.time.*;
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();
}
}
用户自定义类
package 用户自定义类;
import java.time.*;
public class EmployeeTest {
public static void main(String[] args) {
// TODO 自动生成的方法存根
//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 info 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;
}
}
静态域与静态方法
public class StaticTest {
public static void main(String[] args) {
//fill the staff array with three Employee objects
Employee[] staff = new Employee[3];
staff[0] = new Employee("Tom", 40000);
staff[1] = new Employee("Dick", 60000);
staff[2] = new Employee("Harry", 65000);
//print out information about all Employee objects
for(Employee e : staff) {
e.setId();
System.out.println("name=" + e.getName() + ",id=" + e.getId() + ",salary=" + e.getSalary());
}
int n = Employee.getNextId(); //calls static method
System.out.println("Next available id=" + n);
}
}
class Employee{
private static int nextId = 1;
private String name;
private double salary;
private int id;
public Employee(String n, double s) {
name = n;
salary = s;
id = 0;
}
public String getName() {
return name;
}
public double getSalary() {
return salary;
}
public int getId() {
return id;
}
public void setId() {
id = nextId; //set id to next avaliable id
nextId++;
}
public static int getNextId() {
return nextId; //return static field
}
public static void main(String[] args) { //unit test
Employee e = new Employee("Harry", 50000);
System.out.println(e.getName() + " " + e.getSalary());
}
}
方法参数
package 方法参数;
public class ParamTest {
public static void main(String[] args) {
/*
* Test 1: Methods can't modify numeric parameters
*/
System.out.println("Testing tripleValue:");
double percent = 10;
System.out.println("Before: percent=" + percent);
tripValue(percent);
System.out.println("After: percent=" + percent);
/*
* Test 2: Methods can change the state of object parameters
*/
System.out.println("\nTesting tripleSalary:");
Employee harry = new Employee("Harry", 50000);
System.out.println("Before: salary=" + harry.getSalary());
tripleSalary(harry);
System.out.println("After: salary=" + harry.getSalary());
/*
* Test 3: Methods can't attach new objects to object parameters
*/
System.out.println("\nTesting swap:");
Employee a = new Employee("Alice", 70000);
Employee b = new Employee("Bob", 60000);
System.out.println("Before: a=" + a.getName());
System.out.println("Before: b=" + b.getName());
swap(a, b);
System.out.println("After: a=" + a.getName());
System.out.println("After: b=" + b.getName());
}
public static void tripValue(double x) { //doesn't work
x = 3 * x;
System.out.println("End of method: x=" + x);
}
public static void tripleSalary(Employee x) { //works
x.raiseSalary(200);
System.out.println("End of method: salary=" + x.getSalary());
}
public static void swap(Employee x, Employee y) {
Employee temp = x;
x = y;
y = temp;
System.out.println("End of method: x=" + x.getName());
System.out.println("End of method: y=" + y.getName());
}
}
class Employee{
private String name;
private double salary;
public Employee(String n, double s) {
name = n;
salary = s;
}
public String getName() {
return name;
}
public double getSalary() {
return salary;
}
public void raiseSalary(double byPercent) {
double raise = salary * byPercent / 100;
salary += raise;
}
}
对象构造
package 对象构造;
import java.util.Random;
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 = ""; //实例域初始化
private double salary;
//静态初始化块
static {
Random generator = new Random();
nextId = generator.nextInt(10000);
}
//对象初始化块
{
id = nextId;
nextId++;
}
//三个重载构造函数
public Employee(String n, double s) {
name = n;
salary = s;
}
public Employee(double s) {
//调用 Employee(String, double)
this("Employee #" + nextId, s);
}
//默认构造函数
public Employee() {
//name initalized to ""
//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;
}
}
包
package com.horstmann.corejava;
import java.time.*;
public class Employee {
private String name;
private double salary;
private LocalDate hireDay;
public Employee(String name, double salary, int year, int month, int day) {
this.name = name;
this.salary = salary;
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;
}
}
import com.horstmann.corejava.*;
import static java.lang.System.*;
public class PackageTest {
public static void main(String[] args) {
//because of the import statement, we don't have to use com.horstmann.corejava.Employee here
Employee harry = new Employee("Harry Hacker", 50000, 1989, 10, 1);
harry.raiseSalary(5);
//because of the static import statement, we don't have to use System.out here
out.println("name=" + harry.getName() + ",salary=" + harry.getSalary());
}
}