java 中的 class 中的通用 getter 方法
Generic getter method in a class in java
嘿,我已经将 class 道具定义为
`
class prop{
String name;
String address;
String city;
public void String getValue(String str){
String res=null;
if(str.equals("name"))
res="john";
if(str.equals("city"))
res="london";
return res;
}`
我已经这样定义了 getter class,是否可以用其他方式定义通用 getter 方法。
有人告诉我使用 enum class,但我不明白 enum 在这里有什么用。
函数 getValue(String str) 中的参数是一个字符串,因为它被其他 class.
class prop {
public enum Field {
NAME, ADDRESS, CITY
}
private String name;
private String address;
private String city;
public String getValue(Field field) {
switch(field) {
case NAME : return name;
case ADDRESS : return address;
case CITY : return city;
}
return null;
}
}
或者什么的。
有点奇怪的做事方式...
你可以这样尝试:
class Prop{
public enum Properties {
Name,
Address,
City
}
Map<Properties, String> propertyMap;
public Prop() {
this.propertyMap = new HashMap<Properties, String>();
}
public void String setValue(Properties prop, String value){
this.propertyMap.put(prop, value);
}
public void String getValue(Properties prop){
return this.propertyMap(prop);
}
}
要增值:
Prop prop = new Prop();
...
prop.setValue(prop.Properties.Name, "Bob");
...
String name = prop.getValue(prop.Properties.Name); //Should be Bob, assuming it did not change.
编辑:
根据您的查询,您可以这样做:
class Prop{
Map<String, String> propertyMap;
public Prop() {
this.propertyMap = new HashMap<String, String>();
}
public void String setValue(String prop, String value){
this.propertyMap.put(prop, value);
}
public void String getValue(String prop){
return this.propertyMap(prop);
}
}
这种方法的问题是无论谁调用你的代码都必须猜测或者有某种先验知识才能调用你的代码。通过枚举,您可以预先提供您的地图可以具有哪些属性。
这确实是题外话,在这种情况下我永远不会使用枚举,但你可以在枚举中存储值:
enum Properties {
Name("John"),
Address("221B Baker Street"),
City("London");
private String value;
Properties(String value0){
value = value0;
}
}
嘿,我已经将 class 道具定义为
`
class prop{
String name;
String address;
String city;
public void String getValue(String str){
String res=null;
if(str.equals("name"))
res="john";
if(str.equals("city"))
res="london";
return res;
}`
我已经这样定义了 getter class,是否可以用其他方式定义通用 getter 方法。
有人告诉我使用 enum class,但我不明白 enum 在这里有什么用。
函数 getValue(String str) 中的参数是一个字符串,因为它被其他 class.
class prop {
public enum Field {
NAME, ADDRESS, CITY
}
private String name;
private String address;
private String city;
public String getValue(Field field) {
switch(field) {
case NAME : return name;
case ADDRESS : return address;
case CITY : return city;
}
return null;
}
}
或者什么的。
有点奇怪的做事方式...
你可以这样尝试:
class Prop{
public enum Properties {
Name,
Address,
City
}
Map<Properties, String> propertyMap;
public Prop() {
this.propertyMap = new HashMap<Properties, String>();
}
public void String setValue(Properties prop, String value){
this.propertyMap.put(prop, value);
}
public void String getValue(Properties prop){
return this.propertyMap(prop);
}
}
要增值:
Prop prop = new Prop();
...
prop.setValue(prop.Properties.Name, "Bob");
...
String name = prop.getValue(prop.Properties.Name); //Should be Bob, assuming it did not change.
编辑: 根据您的查询,您可以这样做:
class Prop{
Map<String, String> propertyMap;
public Prop() {
this.propertyMap = new HashMap<String, String>();
}
public void String setValue(String prop, String value){
this.propertyMap.put(prop, value);
}
public void String getValue(String prop){
return this.propertyMap(prop);
}
}
这种方法的问题是无论谁调用你的代码都必须猜测或者有某种先验知识才能调用你的代码。通过枚举,您可以预先提供您的地图可以具有哪些属性。
这确实是题外话,在这种情况下我永远不会使用枚举,但你可以在枚举中存储值:
enum Properties {
Name("John"),
Address("221B Baker Street"),
City("London");
private String value;
Properties(String value0){
value = value0;
}
}