共计 2483 个字符,预计需要花费 7 分钟才能阅读完成。
1. 学习 LocalDate
public static void main(String\u005B\u005D args) throws IOException {LocalDate date = LocalDate.now(); | |
int month = date.getMonthValue(); | |
int today = date.getDayOfMonth(); | |
date = date.minusDays(today - 1); // Set to start of month,minusDays(int n)方法生成当前日期之后或之前 N 天的日期 | |
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();} |
输出结果
2.Java 文件名必须与 public 类的名字相匹配,在一个源文件中,只能有一个公共类。
3. 如果需要返回一个可变对象的引用,应该首先对他进行克隆(clone);
4. 一个方法不能修改一个基本数据类型的参数(即数值型或布尔型);
一个方法可以改变一个对象参数的状态;
一个方法不能让对象参数引用一个新的对象;
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); | |
tripleValue(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 tripleValue(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 // simplified Employee class | |
{ | |
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; | |
} | |
} |
5. 如果没有指定 private 或者 public,这一部分可以被同一个包中的所有方法访问;
正文完
发表至: java
2019-07-26