【问题标题】:Get Object from Field从字段中获取对象
【发布时间】:2013-02-24 22:28:11
【问题描述】:

澄清:

我不知道对象名称。这就是问题所在。我正在创建一个这样的对象:

`新对象(字符串属性);

我正在尝试在另一个类中运行代码,例如:

***.getStuff();

其中的诀窍是,对象没有名称。但我知道什么是字符串属性

问题:有什么方法可以在不使用可怕的 for 循环的情况下完成此操作?


这个问题说起来有点棘手,但我会尽力而为。我想要的是获得一个与特定字段匹配的对象,而不会造成混乱的 for 循环。大致如下:

对象 A 具有字段字符串名称。

String nameObj = "Tickle";

对象 A 的名称为“Tickle”

if(nameObj.equals(Object A)){
//bla bla
}

非常混乱的措辞,是的。对于那个很抱歉。我想在我的代码中使用对象 A 而不必弄清楚它是哪个对象,假设我只有它的名称。我想我正在寻找使用 for 循环的捷径。

请随时询问有关我在寻找什么的问题。很抱歉这个措辞糟糕的问题。

糟糕的编码,但这就是我要找的......

nameObj.getName().getObjectA();

【问题讨论】:

  • 那么 A 是您需要转换的字符串吗?或者是您拥有的某个自定义对象,其中包含一个名为 Name 的字符串字段,您想检查是否相等? “你想做什么?不要看代码。”
  • A 是一个自定义对象,带有一个名为 name 的字符串字段。我不太想检查平等。我只想在我的代码中使用它,而不必弄清楚它是哪个对象。
  • 好吧,如果字段是公开的,那么 A.Field。如果不是,您将需要一个公共 get 方法。

标签: java object field


【解决方案1】:

如果你有一堆有名字的对象,并且你想通过它的名字来获取一个对象,我建议你查一下HashMap这个类。 HashMap 允许您将对象放在键下,当您为哈希映射提供键时,它会返回与该键关联的对象。因此,在您的示例中,键将是字符串名称。

【讨论】:

  • 对象没有名称。抱歉,请检查顶部的说明。我认为这可能会更好。你的回答还能回答问题吗?
  • @Samuel Knox:如果您希望对象被另一个类实例访问,那么在构造期间或使用方法将其传递给实例。如果由于某种原因你不能这样做,那么你可以使用你知道的关于对象的任何属性作为 hashmap 的键。
  • 好的,这很酷。我只是希望有一些单行来避免 for 循环混乱。但我会继续走那条路。谢谢!
  • @Samuel Knox 除非它们很慢,否则不要对使用 for 循环感到难过 :) 很多时候,似乎避免 for 循环的技术实际上只是将它们写在其他地方(如 LINQ in C#)。 HashMap 和其他地图/字典类型的集合通过键直接访问值。
【解决方案2】:

看一下这个实现,它演示了@Patashu 所说的,创建一个到对象的映射,在这种情况下,我只是在顶部添加一个抽象类。

import java.util.HashMap;

public class FindMeBaby {

    public static void main(String[] args) {
        Factory.add(new NiceGuy("first one"));
        Factory.add(new FirstChild("ok im late"));

        System.out.println(Factory.get("first one")
                .getVeryImportantInformationThatOnlyThisClassKnows());

    }
}


abstract class ParentOfAll {
    protected String id;

    public ParentOfAll(String id) {
        this.id = id;
    }

    public  String getId(){
        return id;
    }

    public abstract String getVeryImportantInformationThatOnlyThisClassKnows();

}

class FirstChild extends ParentOfAll {

    public FirstChild(String id) {
        super(id);
    }

    public String getVeryImportantInformationThatOnlyThisClassKnows() {
        return "this is a secret";
    }
}

class NiceGuy extends ParentOfAll {

    public NiceGuy(String id) {
        super(id);
    }

    public String getVeryImportantInformationThatOnlyThisClassKnows() {
        return "to say the true, i'm not that nice :)";
    }

}

class Factory {
    private static HashMap allTheObjects = new HashMap();

    public static Object add(ParentOfAll object) {
        allTheObjects.put(object.getId(), object);
        return object;
    }

    public static ParentOfAll get(String key) {
        return (ParentOfAll) allTheObjects.get(key);
    }
}

【讨论】:

    【解决方案3】:

    这是另一个版本,相同的实现具有更透明的方法,没有工厂类,父级本身将跟踪实例并保存在一个列表中。

    import java.util.HashMap;
    
    public class FindMeBaby {
    
        public static void main(String[] args) {
            NiceGuy foo = new NiceGuy("first one");
            FirstChild bar = new FirstChild("ok im late");
    
            System.out.println(ParentOfAll.get("first one")
                    .getVeryImportantInformationThatOnlyThisClassKnows());
    
        }
    }
    
    abstract class ParentOfAll {
        protected String id;
    
        public ParentOfAll(String id) {
            this.id = id;
            add(this);
        }
    
        public String getId() {
            return id;
        }
    
        public abstract String getVeryImportantInformationThatOnlyThisClassKnows();
    
        private static HashMap allTheObjects = new HashMap();
    
        private static Object add(ParentOfAll object) {
            allTheObjects.put(object.getId(), object);
            return object;
        }
    
        public static ParentOfAll get(String key) {
            return (ParentOfAll) allTheObjects.get(key);
        }
    
    }
    
    class FirstChild extends ParentOfAll {
    
        public FirstChild(String id) {
            super(id);
        }
    
        public String getVeryImportantInformationThatOnlyThisClassKnows() {
            return "this is a secret";
        }
    }
    
    class NiceGuy extends ParentOfAll {
    
        public NiceGuy(String id) {
            super(id);
        }
    
        public String getVeryImportantInformationThatOnlyThisClassKnows() {
            return "to say the true, i'm not that nice :)";
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-31
      • 2021-03-19
      • 1970-01-01
      • 1970-01-01
      • 2021-08-23
      • 1970-01-01
      • 2023-03-04
      相关资源
      最近更新 更多