Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java中的最佳实践布尔值_Java - Fatal编程技术网

java中的最佳实践布尔值

java中的最佳实践布尔值,java,Java,快速提问 这是 this.setPregnant(isPregnant = true); …和这个一样吗 this.setPregnant(true); 哪一种练习更好 @ScheduledMethod(start = 3) public void mate() { if (this.isFemale == true) { Context context = ContextUtils.getContext(this); Geography<Ag

快速提问

这是

this.setPregnant(isPregnant = true);
…和这个一样吗

this.setPregnant(true);
哪一种练习更好

@ScheduledMethod(start = 3) 
public void mate() {
    if (this.isFemale == true) {
        Context context = ContextUtils.getContext(this);
        Geography<Agent> geography = (Geography)context.getProjection("Geography");
        Geometry geom = geography.getGeometry(this);
        // get coordinates of the female
        Coordinate femCoord = geom.getCoordinates()[0];
        List<Agent> males = new ArrayList<Agent>();
        //create an envelope around the female
        Envelope envelope = new Envelope (femCoord.x + 0.9, femCoord.x - 0.9, femCoord.y + 0.9, femCoord.y - 0.9);
        //get all the males around the female
        for(Agent male: geography.getObjectsWithin(envelope, Agent.class)) {
            if(male.isFemale != true)
                //add them to a list
                males.add(male);
        }

        //randomly choose one, set isPregnant to be true and move to his coordinates
        int index = RandomHelper.nextIntFromTo(0, males.size() -1);
        Agent mate = males.get(index);
        Context matecontext = ContextUtils.getContext(mate);
        Geography<Agent> mategeography = (Geography)matecontext.getProjection("Geography");
        Geometry mategeom = mategeography.getGeometry(mate);
        Coordinate mate = mategeom.getCoordinates()[0];

        this.setPregnant(isPregnant = true);
        // or this.setPregnant(true);

        moveTowards(mate);

        System.out.println("Female can now lay eggs...");

    }
}
@ScheduledMethod(start=3)
公众假期{
if(this.isFemale==true){
Context-Context=contextTILS.getContext(this);
地理=(地理)上下文.getProjection(“地理”);
Geometry geom=geography.getGeometry(this);
//获取女性的坐标
坐标femCoord=geom.getCoordinates()[0];
列表列表=新的ArrayList();
//围绕内螺纹创建一个封套
信封信封=新信封(femCoord.x+0.9,femCoord.x-0.9,femCoord.y+0.9,femCoord.y-0.9);
//让所有的男性都围着女性
for(代理:geography.getObjectsWithin(信封,Agent.class)){
if(male.isFemale!=真)
//将它们添加到列表中
男性。添加(男性);
}
//随机选择一个,将isPregnant设置为true并移动到其坐标
int index=RandomHelper.nextIntFromTo(0,males.size()-1);
代理配偶=雄性。获取(索引);
Context-matecontext=ContextUtils.getContext(mate);
地理mategeography=(地理)matecontext.getProjection(“地理”);
几何体mategeom=mategeography.getGeometry(mate);
坐标匹配=mategeom.getCoordinates()[0];
此.setpregned(isPregnant=true);
//或者这个。setpregned(真);
走向(配偶);
System.out.println(“雌性现在可以产卵了…”);
}
}

从来没有见过像setter方法的顶级语法。底部的方法更具可读性和实用性,因此避免使用另一种方法。

对于setter方法,从来没有见过类似顶部的语法。底部的一个更具可读性和实用性,因此避免使用另一个。

不,不是。他们是不同的,, 第一种方法将布尔值“isPregnant”设置为true,然后将其传递给“setpregned”方法,该示例是一种糟糕的做法

(大多数公司风格指南通常都有一句话:“千万不要把任务和操作混在一起,这会使代码更难阅读。”)


第二个是明确的(但不执行赋值),我们可以假设set怀孕方法再次执行赋值(但不能确定)

否。他们是不同的,, 第一种方法将布尔值“isPregnant”设置为true,然后将其传递给“setpregned”方法,该示例是一种糟糕的做法

(大多数公司风格指南通常都有一句话:“千万不要把任务和操作混在一起,这会使代码更难阅读。”)


第二个是明确的(但不执行赋值),我们会假设setpregned方法再次执行赋值(但不能确定)

如果您在具有
isPregnant
属性的类中,您可以直接分配属性。不需要方法调用

isPregnant = true;

如果您在具有
isPregnant
属性的类中,则可以直接指定属性。不需要方法调用

isPregnant = true;

不要使用上面的一个。只是一个意见。可读性差得多。不,在不知道SetPigned做什么的情况下,这两个人可能在做不同的事情。第一个将true赋值给isPregnant,然后将该值传递给该方法,第二个将true传递给该方法……@MadProgrammer仍然是它的混合赋值和操作。@DamianNikodem没有参数,确实建议使用它,只是它们是一样的thing@DamianNikodem如果希望独立于setPregnant设置isPregnant标志,那么,正如您所说,isPregnant应该首先设置,然后传递给setPregnant,但这只是猜测工作;)不要使用上面的一个。只是一个意见。可读性差得多。不,在不知道SetPigned做什么的情况下,这两个人可能在做不同的事情。第一个将true赋值给isPregnant,然后将该值传递给该方法,第二个将true传递给该方法……@MadProgrammer仍然是它的混合赋值和操作。@DamianNikodem没有参数,确实建议使用它,只是它们是一样的thing@DamianNikodem如果希望独立于setPregnant设置isPregnant标志,那么,正如您所说,isPregnant应该首先设置,然后传递给setPregnant,但这只是猜测工作;)语法是有效的,但被认为是不好的实践。(因为它执行赋值,然后将参数传递给setpregned方法。)海报可能会错误地认为java支持命名参数(在ada中,调用setpregned(pregned=>true,hasBeenConfirmedByTest=>true)可以提高可读性。好的。我只是从来没有被教过或意识到第一个实现。同样在第一个实现中,将相同的东西设置为true两次不是吗?@brtballin在java中,目前还不存在这样的功能(JDK8是最新一代),尽管有很多语言支持命名参数(C#,ada,objective C..)如果你没有看到内联赋值,那么你没有在“野外”看到它的原因是因为它的实践非常糟糕,但是有一行代码如“while((a=myMethod(C,b))>0”是合法的。语法是有效的,但被认为是实践不佳。(因为它执行赋值,然后将参数传递给setpregned方法。)海报可能会错误地认为java支持命名参数(在ada中,调用setpregned(pregned=>true,hasBeenConfirmedByTest=>true)可以提高可读性。好的。我只是从来没有被教过或意识到第一个实现。同样在第一个实现中,将相同的东西设置为true两次不是吗?@brtballin在java中这样的功能目前还不存在(JDK8是最新一代),尽管有很多语言支持