Java 如何编写包含布尔值的构造函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21103162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to write a constructor that contains a boolean value?
提问by Sarah
This is a dumb question but it's been a long time since I've worked with java... How can I write my constructor with Boolean values or should I just write a default constructor? I have been working with C++ most recently and I've forgotten a lot of syntax for java.
这是一个愚蠢的问题,但自从我使用 java 以来已经有很长时间了......我如何使用布尔值编写构造函数,或者我应该只编写默认构造函数?我最近一直在使用 C++,但我忘记了很多 java 的语法。
This is what I have so far:
这是我到目前为止:
public class Creature {
protected int terrain;
public static final int DESERT = 0;
public static final int MOUNTAIN = 1;
public static final int FOREST = 2;
//symbols on cards
boolean flyingCreature = false;
boolean magicCreature = false;
boolean canCharge = false;
boolean rangedCombat false;
public int specialAbility = 0;
public Creature(int startTerrain, boolean flying, boolean magic, boolean charge, boolean ranged, int special){
?
}
}
I can't seem to find anything when I do a search... How do I initialize each value in my constructor? or should I just have
我在搜索时似乎找不到任何东西......我如何初始化我的构造函数中的每个值?或者我应该有
public Creature(){
startTerrain = DESERT;
flyingCreature = false;
magicCreature = false;
canCharge = false;
specialAbility = 0;
} ?
I also have several classes inheriting from this one, so I'm not sure if that makes a difference.
我也有几个继承自这个类的类,所以我不确定这是否有所不同。
采纳答案by Hugo Sousa
A boolean parameter is just like any other type.
布尔参数就像任何其他类型一样。
So, it would be like this.
所以,它会是这样的。
public Creature(int startTerrain, boolean flying, boolean magic, boolean charge, boolean ranged, int special){
terrain = startTerrain;
flyingCreature = flying;
magicCreature = magic;
canCharge = charge;
rangedCombat = ranged;
specialAbility = special;
}
If these parameters are going to be always the same on the beggining, then you can set them on a default constructor, as you said.
如果这些参数在开始时总是相同的,那么您可以在默认构造函数上设置它们,正如您所说。
Since, you have classes inheriting this one, their constructor will have to call super()
, which calls the parent class constructor. If you call it without any parameters, the base constructor of Creature will be called.
因为,你有继承这个的类,它们的构造函数必须调用super()
,它调用父类构造函数。如果不带任何参数调用它,将调用 Creature 的基本构造函数。
回答by peter.petrov
If you use the constructor which has parameters, it goes like this
如果你使用带有参数的构造函数,它会像这样
this.flyingCreature = flying;
this.magicCreature = magic;
and so on.
等等。
If you use the constructor without any parameters (the default constructor), then you need to set the class fields to some constants (like you did). So you do e.g.
如果您使用没有任何参数的构造函数(默认构造函数),那么您需要将类字段设置为一些常量(就像您所做的那样)。所以你做例如
this.flyingCreature = false;
this.magicCreature = false;
and so on.
等等。
The use of this.
is not mandatory unless you have a parameter with the same name in which case you should use this.
otherwise your initialization code will have no effect on the class field.
使用this.
不是强制性的,除非您有一个同名的参数,在这种情况下,您应该使用,this.
否则您的初始化代码将对类字段没有影响。
回答by tfandango
You probably appropriately chose the primitive type boolean in your example but since you mentioned "Boolean" with a capital "B", there is a difference. Boolean variables are nullable unlike the primitive "boolean" with a lower case "b". If you had used Booleans, you can simply initialize those like
您可能在示例中适当地选择了原始类型 boolean,但由于您提到了带有大写“B”的“Boolean”,因此存在差异。与带有小写“b”的原始“boolean”不同,布尔变量可以为空。如果您使用过布尔值,则可以简单地初始化它们,例如
Boolean boolVar = Boolean.TRUE; //or Boolean.FALSE
And they might later require null checks depending on the situation.
并且他们以后可能需要根据情况进行空检查。