【发布时间】:2017-07-02 13:56:03
【问题描述】:
我正在尝试使用另一个类中的另一个对象来检查一个人的年龄,出于某种原因,areThey 布尔值无论如何都会返回 false。我实现的以下代码是_
public class main {
public static void main(String[] args){
Obj object = new Obj();
object.areTheyOldEnough();
if(object.areTheyOldEnough() == true){
System.out.println("They are old enough!");
}else{
System.out.println("They are not old enough!");
}
}
}
public class Obj {
private int age = 15;
private boolean areThey;
public boolean areTheyOldEnough() {
if (age > 12) {
boolean areThey = true;
}
return areThey;
}
}
【问题讨论】:
-
调用
Object类的实例并不是一个好主意。您应该阅读命名约定。 -
独立你的问题你应该知道:类名应该是名词,大小写混合,每个内部单词的第一个字母大写。 (oracle.com/technetwork/java/codeconventions-135099.html)。我的意思是你应该定义你的类:public class Obj
-
除此之外
if (booleanMethod() == true)也是不好的做法。你看,if (areTheyOldEnough())更容易阅读!