Java中的Getters,Setter和Constructors - 制作一辆汽车并放养它
我正尝试使用BlueJ在Java中创建一个类。我的班级名为汽车。我的目标是能够使用我的构造函数方法创建具有以下变量的汽车:年份,颜色,品牌,门数,公里数,自动(布尔值),售出(布尔值),说明和一个识别号码。所有变量都有一个设定的默认值,一个最小值和一个最大值。Java中的Getters,Setter和Constructors - 制作一辆汽车并放养它
我必须为我的方法使用getVariablename和setVariablename。我的颜色和品牌变量是int,并且我制作了方法来在我的类的表格中检索它们的String对应项。
我的问题是我不明白在一个方法中设置我的变量并将其置于另一个方法(同时确保它是可接受的值)的原则。另外,一旦我有我的Setter和Getter方法,在创建我的构造函数方法时,我需要写下什么?
到现在为止,我有这样的:
public class Automobile {
private static final String[] COLORS = { "Other", "Noir", "Blanc", "Bleu Nuit", "Bleu Clair", "Vert Pomme", "Vert Bouteille", "Taupe", "Argent", "Sable"};
private static final String[] BRANDS = { "Autre", "Mazda", "Toyota", "Ford", "GM", "Hyunday", "BMW", "SAAB", "Honda"};
public static final int COLOR_DEF = 8;
public static final int COLOR_MIN = 0;
public static final int COLOR_MAX = COULEURS.length - 1;
public static final int BRAND_DEF = 4;
public static final int BRAND_MIN = 0;
public static final int BRAND_MAX = MARQUES.length - 1;
public static final double KILO_DEFAULT = 55000;
public static final double KILO_MIN = 15000;
public static final double KILO_MAX = 140000;
public static final int TWO_DOORS = 2;
public static final int FOUR_DOORS = 4;
public static final int DOORS_DEFAULT = FOUR_DOORS;
public static final boolean AUTO_DEF = true;
public static final int YEAR_MIN = 1997;
public static final int YEAR_MAX = 2016;
public static final int YEAR_DEFAUT = 2007;
public static final String COMM_DEFAUT = "";
public static String color (int cou) {
String chainecolor = "";
if (cou >= COLOR_MIN && cou <= COLOR_MAX) {
chainecolor = COLORS[cou];
}
return chainecolor;
} //This method is to return the String value of a color from its int value using the COLORS table. If invalid it returns an empty chain.
public static String brand (int br) {
String chainebrand = "";
if (ma >= BRAND_MIN && ma <= BRAND_MAX) {
chainebrand = BRANDS[br];
}
return chainebrand;
} //same thing for the brand
public Automobile (int brand, int year, int color, boolean automatic, double kilometers,int nbrDoors, String description, boolean sold){
//To be completed
}
//here i'm supposed to create getters that return int values for everything but automatic, sold and description
public void setYear (int year) {
if (year >= YEAR_MIN && YEAR <= YEAR_MAX) {
year = year;
}
} // supposed to be the setter for my year, as long as it's within the accepted values
public void setMarque (int brand){
if (brand >= BRAND_MIN && brand <= BRAND_MAX) {
brand = brand;
}
} //same, for the brand
public void setColor (int color) {
if (color >= COLOR_MIN && color <= COLOR_MAX){
color = color;
}
}// same for the color
public void setNbrDoors (int p) {
if (p == TWO_DOORS || p == FOUR_DOORS){
p = p;
}
} // same for the door. I am forced to use (int p) as the variable for this method, which confuses me as to how I will refer to it from nbrDoors up in the Automobile constructor method
} // Automobile
所以我的困难在于:
是我为这个目的而作出有效制定者的例子吗?我不明白是否需要p = p或color = color ...
如何创建一个getter方法,该方法能够从setNbrDoors中获取变量p,并返回其值并使其具有它用于汽车构造函数中的nbrDoors?
我应该在构造函数方法中写什么,比如它能够从getters中获取它的值?
这一切都是因为第二部分是我必须要创建一些代码来要求用户输入的所有变量值,然后创建一个表来炒股用户创建的汽车。
P.S .:工作原本是法文,所以我翻译了变量和方法名称,以便我更好地理解。此外,变量名称,方法等都是强加的,我强迫这种方式完全按照这种方式。
编辑:同样,静态品牌和颜色转换的使用也被强加。这两种方法仅用于从int值返回字符串。它们不在构造函数中使用。最后,异常将在第二部分工作中使用单独的验证循环进行处理。 Automobile类实际上仅用于处理“汽车”对象的创建。
有你的代码的几个问题:
(1)您没有任何适当的实例变量(如year
,brand
,等..)汽车
(2)您没有使用this.
设置实例变量(因为您没有创建它们)enter code here
。请注意,this
始终指向当前对象,请参阅here,即当您说this.year= year
时,实际上将右手边year
赋值给当前对象的变量(左手边)year
。
您可以参考下面的代码注释:
public class Automobile {
private int year;
private int color;
private int brand;
//add other fields
public Automobile (int brand, int year, int color, boolean automatic, double kilometers,int nbrDoors, String description, boolean sold) {
if (year >= YEAR_MIN && year <= YEAR_MAX) {
this.year = year;
} else {
new IllegalArgumentException("Invalid Year Passed to construct Automobile");
}
//Similarly add other validations for brand, color, etc..
}
public void setYear (int year) {
if (year >= YEAR_MIN && YEAR <= YEAR_MAX) {
//USE 'this.' as shown below' to set the given year to 'this' object's year
this.year = year;
}
}
public int getYear() {
return year;
}
//Similarly add setters and getters for year, color, brand, etc...
}
1 - 这是更好地使用这个 P = p来你的对象属性。
2 setNbrDoors,返回一个无效的,你不能从中拿起一个变量,你应该创建一个getNbrDoors:int getNbrDoors() { return this.p; }
所以我必须用这个。在所有安装者。你能提供一个这个变量的Getter的例子吗? 此外,品牌和颜色转换的静态也是强加的。这两种方法仅用于从int值返回字符串。它们不在构造函数中使用。最后,异常将在第二部分工作中使用单独的验证循环进行处理。汽车类真的只用于制造汽车。 (我会将此添加到我的文章中) –
由于您太棒了,所以更新了上面的代码,使用getter – developer
javaguy,我还有一个额外的问题。我必须创建一个变量,该变量将作为我制作的每辆车的唯一标识号,并从1开始(所以第一辆车将是1,第二辆将是2等)。之后,我将有一种方法来克隆我创建的任何汽车,因此创建一个具有所有相同属性的新车,除了销售和识别变量。对于出售,我可以将其重置为false,但是如何每次都复制所有变量的值,同时生成新的标识号?谢谢 –