我正在努力学习Java。我想在构造函数中有一个枚举作为参数。但是我收到了一个错误。
public class Person {
private int age, weight, height;
private String name;
private enum gender {MALE, FEMALE}
public Person(int age, int weight, int height, String name, enum gender) {
this.age = age;
this.weight = weight;
this.height = height;
this.name = name;
this.gender = gender;
}
}
我如何处理性别?我尝试过性别,但也没用。
答案 0 :(得分:8)
首先,您需要创建gender
...
private gender aGender;
然后,您需要更改构造函数以引用类型为gender
public Person(int age, int weight, int height, String name, gender aGender) {
然后您需要将参数分配给您的字段...
this.aGender = aGender;
您gender
enum
也应该是public
public enum gender {
MALE, FEMALE
}
否则,没有人能够使用它
例如......
public class Person {
private int age, weight, height;
private String name;
private gender aGender;
public enum gender {
MALE, FEMALE
}
public Person(int age, int weight, int height, String name, gender aGender) {
this.age = age;
this.weight = weight;
this.height = height;
this.name = name;
this.aGender = aGender;
}
}
您可能希望阅读Code Conventions for the Java TM Programming Language,这样可以让人们更轻松地阅读您的代码并让您阅读其他代码
答案 1 :(得分:2)
如果您定义了enum Gender
,则可以直接将Gender
传递给构造函数,更改为
public Person(int age, int weight, int height, String name, Gender gender)
答案 2 :(得分:1)
您定义的枚举类型名称为gender eGender
,因此您需要将其作为
private enum Gender {MALE, FEMALE}
只是评论。根据惯例,所有自定义类型(类名,接口名,枚举名)都应以大写字母开头。
所以在这种情况下,如果你这样命名你的枚举类型会更好。
UPDATE TBL1 SET DATE1=DATE2, DATE2=DATE1 WHERE DATE2 > DATE1;