Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 向类动态添加属性_Java_C# - Fatal编程技术网

Java 向类动态添加属性

Java 向类动态添加属性,java,c#,Java,C#,我希望能够在运行时为现有类(对象)动态设置属性 例如,具有接口MyProperty.java,以及实现该接口的其他特定属性。然后拥有一个对象,该对象可以动态接收MyProperty的某个实例,并自动为其设置一个getter MyPropery.java[表示某些对象属性的接口] public interface MyProperty { String getValue(); } public class MyPropertyAge implements MyProperty {

我希望能够在运行时为现有类(对象)动态设置属性

例如,具有接口
MyProperty.java
,以及实现该接口的其他特定属性。然后拥有一个对象,该对象可以动态接收
MyProperty
的某个实例,并自动为其设置一个getter

MyPropery.java[表示某些对象属性的接口]

public interface MyProperty {
    String getValue();
}
public class MyPropertyAge implements MyProperty {

    private final String age;

    public MyPropertyAge(String age) {
        this.age = age;
    }

    @Override
    public String getValue() {
        return age;
    }
}
MyPropertyAge.java[某些特定对象属性]

public interface MyProperty {
    String getValue();
}
public class MyPropertyAge implements MyProperty {

    private final String age;

    public MyPropertyAge(String age) {
        this.age = age;
    }

    @Override
    public String getValue() {
        return age;
    }
}
MyObject.java[应该动态地为MyProperty设置getter的对象]

public class MyObject {

    public MyObject(Set<MyProperty> properties) {
        // receive list of properties,
        // and have getters for them
    }

    // For example, if I passed in a Set with 1 instance of "MyPropertyAge", I want to have this

    public MyPropertyAge getMyPropertyAge() {
        // implementation
    }
公共类MyObject{
公共MyObject(设置属性){
//收到财产清单,
//并为他们准备了干燥剂
}
//例如,如果我传入了一个集合,其中包含一个“MyPropertyAge”实例,我希望
公共MyPropertyAge getMyPropertyAge(){
//实施
}
}

因此,问题是根据类在构造函数中接收的属性集,动态地将属性添加到类中。类似于C#中的
dynamic
关键字:


这种事情在Java中可能发生吗?或者一些黑客也会这样做吗?

在Java中没有
动态
关键字这样的东西

但是,您可以使用内部
映射
来存储属性。例如:

MyObject.java

public class MyObject {
    private HashMap<String, Object> properties;

    //Create object with properties
    public MyObject(HashMap<String, Object> properties) {
        this.properties = properties;
    }

    //Set properties
    public Object setProperty(String key, Object value) {
        return this.properties.put(key, value); //Returns old value if existing
    }

    //Get properties
    public Object getProperty(String key) {
        return this.properties.getOrDefault(key, null);
    }
}
公共类MyObject{
私有HashMap属性;
//创建具有属性的对象
公共MyObject(HashMap属性){
这个。属性=属性;
}
//设置属性
公共对象setProperty(字符串键、对象值){
返回this.properties.put(key,value);//如果存在,则返回旧值
}
//获取属性
公共对象getProperty(字符串键){
返回this.properties.getOrDefault(key,null);
}
}
示例:Person

public static void main(String[] args) {
    //Create properties
    HashMap<String, Object> properties = new HashMap<>();

    //Add name and age
    properties.put("name", "John Doe");
    properties.put("age", 25);

    //Create person
    MyObject person = new MyObject(properties);

    //Get properties
    System.out.println(person.getProperty("age")); //Result: 25
    System.out.println(person.getProperty("name")); //Result: John Doe
}
publicstaticvoidmain(字符串[]args){
//创建属性
HashMap属性=新建HashMap();
//添加姓名和年龄
财产。投入(“姓名”、“约翰·多伊”);
财产。出售(“年龄”,25岁);
//创造人
MyObject person=新的MyObject(属性);
//获取属性
System.out.println(person.getProperty(“age”);//结果:25
System.out.println(person.getProperty(“name”);//结果:John Doe
}
有多种方法可以实现这一点,但这可能是最简单的方法。

看看Q/a,我想这就是你想要的:)