[英]Effective java Item no 5 example code second edition
public class Person
private final Date birthDate;
// others fields omitted
public boolean isBabyBoomer() {
Calendar gmtCal =
Calendar.getInstance(TimeZone.getTimeZone("GMT"));
gmtCal.set(1946,Calendar.JANUARY,1,0,0,0);
Date boomStart =gmtCal.getTime();
gmtCal.set(1965,Calendar.JANUARY,1,0,0,0);
Date boomEnd = gmtCal.getTime();
return birthDate.compareTo(boomStart) >= 0 && //where is Birthdate instance
// to compare
birthDate.compareTo(boomEnd) < 0;
}
}
Given fact birthDate
is final, i would say it is passed to class via constructor,鉴于birthDate
是最终的,我会说它是通过构造函数传递给类的,
It is common practice in books and other tutorin materials to omit bits of code which are not relevant for given example to avoid creating extra confision for reader书籍和其他教程材料中的常见做法是省略与给定示例无关的代码位,以避免为读者造成额外的理解
Method compareTo
returns int
hence if you want to convert this to boolean result, you have to compare it to some number by using one of of the operators ==, !=, <, >, <=, >=
方法compareTo
返回int
因此,如果要将其转换为布尔结果,则必须使用运算符==, !=, <, >, <=, >=
之一将其与某个数字进行比较
And how you want to use simple return?你想如何使用简单的返回?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.