java - 如何在Java中创建一个以枚举为参数的对象
问题描述
我有一堂课
public enum CustomerType {
GOLD, SILVER, IRON, RUSTY
}
和另一个类:
@Entity
公共类客户实现可序列化{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String firstName;
private String lastName;
private CustomerType type;
public Customer(){};
public Customer(String firstName, String lastName){
this.firstName = firstName;
this.lastName = lastName;
}
public Customer(String firstName, String lastName, CustomerType type){
this.firstName = firstName;
this.lastName = lastName;
this.type = type;
}
public String getFirstName(){ return firstName;}
public String getLastName(){ return lastName;}
public Integer getId() { return id; }
public CustomerType getType(){ return type;}
public void setId(Integer id) { this.id = id;}
public void setFirstName(String firstName){this.firstName = firstName;}
public void setLastName(String lastName ){this.lastName = lastName;}
public void setType(CustomerType type){this.type = type;}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Customer)) {
return false;
}
Customer other = (Customer) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "entity.Customer[ id=" + id + " ]";
}
}
我有一个带有参数 firstName、lastName、CustomerType 类型的构造函数
在另一个班级我想创建这样的客户
Customer c1 = new Customer("ida", "heartless", GOLD);
但它只是不会让像这样放置枚举参数。我怎样才能正确使用枚举?我错过了什么?
解决方案
看起来你只需要在你的Customer
类中导入。之后它应该看起来像这样:
Customer c1 = new Customer("ida", "heartless", CustomerType.GOLD);
推荐阅读
- python - 即使我已经有 __init__.py 文件也无法导入类
- php - 一个表中的两个数组,并排列进行比较
- java - 将mysql数据放入java tablemodel
- java - Jar 文件未连接到现有数据库
- node.js - 断点不触发。“断点已设置但尚未绑定”。码头工人,NodeJS,VSCode
- python - TypeError:不能在类似字节的对象上使用字符串模式 - Py3
- javascript - 如何在 FP 中引用当前状态?
- javascript - 有什么方法可以在动态 div 上添加 jQuery css?
- php - 将位图从 android 发送到 php 脚本
- java - 进行 Collatz contejure 时无法打印出 1