使用字符串和值创建预定义类型的字段,java
例如,我希望能够编写一个以字符串和整数作为参数的方法,然后我希望在该类中创建一个具有整数值的变量,以便以后调用。例如:使用字符串和值创建预定义类型的字段,java,java,Java,例如,我希望能够编写一个以字符串和整数作为参数的方法,然后我希望在该类中创建一个具有整数值的变量,以便以后调用。例如: public void setInt(String identifier, Integer) { } 如果我那时打电话 setInt("age", 25); //stores 25 with identifier "age" 它将创建一个名为age的变量,稍后我可以通过调用 getInt("age") //would return 25 我该怎么做呢?如果你有地图,你可
public void setInt(String identifier, Integer) {
}
如果我那时打电话
setInt("age", 25); //stores 25 with identifier "age"
它将创建一个名为age的变量,稍后我可以通过调用
getInt("age") //would return 25
我该怎么做呢?如果你有地图,你可以这样做,或者如果你使用反射。最好的方法是只为每个实例变量创建一个getter和setter对:
private int age;
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
如果您有一张如下所示的地图,您可以实现以下目标:
Map<String, Object> properties = new HashMap<>();
public void setInt(String property, int value) {
properties.put(property, value);
}
public int getInt(String property) {
return (int) properties.get(property);
}
// Usage
setInt("age", 25);
int age = getInt("age");
Map properties=newhashmap();
公共void setInt(字符串属性,int值){
出售(财产、价值);
}
公共整型getInt(字符串属性){
return(int)properties.get(property);
}
//用法
setInt(“年龄”,25岁);
int age=getInt(“年龄”);
如果你有地图,你可以这样做,或者如果你使用反射。最好的方法是只为每个实例变量创建一个getter和setter对:
private int age;
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
如果您有一张如下所示的地图,您可以实现以下目标:
Map<String, Object> properties = new HashMap<>();
public void setInt(String property, int value) {
properties.put(property, value);
}
public int getInt(String property) {
return (int) properties.get(property);
}
// Usage
setInt("age", 25);
int age = getInt("age");
Map properties=newhashmap();
公共void setInt(字符串属性,int值){
出售(财产、价值);
}
公共整型getInt(字符串属性){
return(int)properties.get(property);
}
//用法
setInt(“年龄”,25岁);
int age=getInt(“年龄”);
如果你有地图,你可以这样做,或者如果你使用反射。最好的方法是只为每个实例变量创建一个getter和setter对:
private int age;
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
如果您有一张如下所示的地图,您可以实现以下目标:
Map<String, Object> properties = new HashMap<>();
public void setInt(String property, int value) {
properties.put(property, value);
}
public int getInt(String property) {
return (int) properties.get(property);
}
// Usage
setInt("age", 25);
int age = getInt("age");
Map properties=newhashmap();
公共void setInt(字符串属性,int值){
出售(财产、价值);
}
公共整型getInt(字符串属性){
return(int)properties.get(property);
}
//用法
setInt(“年龄”,25岁);
int age=getInt(“年龄”);
如果你有地图,你可以这样做,或者如果你使用反射。最好的方法是只为每个实例变量创建一个getter和setter对:
private int age;
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
如果您有一张如下所示的地图,您可以实现以下目标:
Map<String, Object> properties = new HashMap<>();
public void setInt(String property, int value) {
properties.put(property, value);
}
public int getInt(String property) {
return (int) properties.get(property);
}
// Usage
setInt("age", 25);
int age = getInt("age");
Map properties=newhashmap();
公共void setInt(字符串属性,int值){
出售(财产、价值);
}
公共整型getInt(字符串属性){
return(int)properties.get(property);
}
//用法
setInt(“年龄”,25岁);
int age=getInt(“年龄”);
您可以持有一个映射
数据成员,并使用它存储值:
public class SomeClass {
// could (should?) be initialized in the ctor
private Map<String, Integer> map = new HashMap<>();
public void setInt (String identifier, int value) {
// This assumes identifier != null, for clarity
map.put (identifier, value);
}
public int getInt (String identifier) {
return map.get (identifier);
}
公共类SomeClass{
//可以(应该?)在ctor中初始化吗
私有映射映射=新的HashMap();
公共void setInt(字符串标识符,int值){
//为清楚起见,这假定标识符!=null
map.put(标识符、值);
}
公共整数getInt(字符串标识符){
返回map.get(标识符);
}
您可以持有一个映射
数据成员,并使用它存储值:
public class SomeClass {
// could (should?) be initialized in the ctor
private Map<String, Integer> map = new HashMap<>();
public void setInt (String identifier, int value) {
// This assumes identifier != null, for clarity
map.put (identifier, value);
}
public int getInt (String identifier) {
return map.get (identifier);
}
公共类SomeClass{
//可以(应该?)在ctor中初始化吗
私有映射映射=新的HashMap();
公共void setInt(字符串标识符,int值){
//为清楚起见,这假定标识符!=null
map.put(标识符、值);
}
公共整数getInt(字符串标识符){
返回map.get(标识符);
}
您可以持有一个映射
数据成员,并使用它存储值:
public class SomeClass {
// could (should?) be initialized in the ctor
private Map<String, Integer> map = new HashMap<>();
public void setInt (String identifier, int value) {
// This assumes identifier != null, for clarity
map.put (identifier, value);
}
public int getInt (String identifier) {
return map.get (identifier);
}
公共类SomeClass{
//可以(应该?)在ctor中初始化吗
私有映射映射=新的HashMap();
公共void setInt(字符串标识符,int值){
//为清楚起见,这假定标识符!=null
map.put(标识符、值);
}
公共整数getInt(字符串标识符){
返回map.get(标识符);
}
您可以持有一个映射
数据成员,并使用它存储值:
public class SomeClass {
// could (should?) be initialized in the ctor
private Map<String, Integer> map = new HashMap<>();
public void setInt (String identifier, int value) {
// This assumes identifier != null, for clarity
map.put (identifier, value);
}
public int getInt (String identifier) {
return map.get (identifier);
}
公共类SomeClass{
//可以(应该?)在ctor中初始化吗
私有映射映射=新的HashMap();
公共void setInt(字符串标识符,int值){
//为清楚起见,这假定标识符!=null
map.put(标识符、值);
}
公共整数getInt(字符串标识符){
返回map.get(标识符);
}
看一看s。它不会创建变量,但会提供所需的功能。看一看s。它不会创建变量,但会提供所需的功能。看一看s。它不会创建变量,但会提供所需的功能。看一看s。它不会创建变量,但会提供所需的功能您可以使用所需的功能。