Java 从字段中获取对象

Java 从字段中获取对象,java,object,field,Java,Object,Field,澄清: 我不知道物体的名称。这就是问题所在。我正在创建这样一个对象: `新对象(字符串属性) 我正在尝试在另一个类中运行代码,例如: ***.getStuff(); 诀窍在于,对象没有名称。但我知道字符串属性是什么 问题是:有没有办法不使用可怕的for循环来实现这一点 这个问题有点难懂,但我会尽力的。我想要的是得到一个与特定字段匹配的对象,而不产生混乱的for循环。大致如下: 对象A具有字段字符串名称 String nameObj = "Tickle"; 对象A的名称为“Tickle” 非

澄清:

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

`新对象(字符串属性)

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

***.getStuff();
诀窍在于,对象没有名称。但我知道字符串属性是什么

问题是:有没有办法不使用可怕的for循环来实现这一点


这个问题有点难懂,但我会尽力的。我想要的是得到一个与特定字段匹配的对象,而不产生混乱的for循环。大致如下:

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

String nameObj = "Tickle";
对象A的名称为“Tickle”

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

请随时问我要找什么。对不起,这个措辞很糟糕的问题

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

nameObj.getName().getObjectA();

如果您有一组具有名称的对象,并且希望通过名称获取对象,我建议您查找类
HashMap
HashMap
允许您将对象放在键下,当您给HashMap一个键时,它将返回与该键关联的对象。因此,在您的示例中,键将是字符串名称。

如果您有一组具有名称的对象,并且希望通过名称获取对象,我建议您查找类
HashMap
HashMap
允许您将对象放在键下,当您给HashMap一个键时,它将返回与该键关联的对象。因此,在您的示例中,键应该是字符串名称。

看看这个实现,它演示了@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);
    }
}

以这个实现为例,它演示了@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);
    }
}

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

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 :)";
    }

}

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

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 :)";
    }

}

那么,一个字符串是否需要转换?或者是一个自定义对象,其中包含一个名为Name的字符串字段,您希望检查是否相等?“你想做什么?不要看代码。”A是一个自定义对象,有一个名为name的字符串字段。我不太想检查平等性。我只想在我的代码中使用它,而不必弄清楚它是哪一个对象。好的,如果字段是公共的,那么就用一个字段。如果不是,您将需要一个公共get方法。那么您需要强制转换的字符串是什么呢?或者是一个自定义对象,其中包含一个名为Name的字符串字段,您希望检查是否相等?“你想做什么?不要看代码。”A是一个自定义对象,有一个名为name的字符串字段。我不太想检查平等性。我只想在我的代码中使用它,而不必弄清楚它是哪一个对象。好的,如果字段是公共的,那么就用一个字段。如果不是,则需要一个公共get方法。该对象没有名称。对不起,请查看顶部的澄清。我想这可能会更好。你的答案仍然回答了这个问题吗?@Samuel Knox:如果你想让另一个类实例访问该对象,那么在构造过程中或使用方法将其传递给该实例。如果你因为某种原因不能这样做,那么你可以把你知道的关于对象的任何属性作为hashmap的键。我只是希望有一个班轮,以避免循环混乱。但我会继续走那条路。谢谢@Samuel Knox不会因为使用for循环而感到不快,除非它们很慢:)很多时候,似乎避免使用for循环的技术实际上只是把它们写在其他地方(比如C#中的LINQ)。HashMap和其他map/dictionary类型的集合通过封面下的键直接访问值。对象没有名称。对不起,请查看顶部的澄清。我想这可能会更好。你的答案仍然回答了这个问题吗?@Samuel Knox:如果你想让另一个类实例访问该对象,那么在构造过程中或使用方法将其传递给该实例。如果你因为某种原因不能这样做,那么你可以把你知道的关于对象的任何属性作为hashmap的键。我只是希望有一个班轮,以避免循环混乱。但我会继续走那条路。谢谢@Samuel Knox不会因为使用for循环而感到不快,除非它们很慢:)很多时候,似乎避免使用for循环的技术实际上只是把它们写在其他地方(比如C#中的LINQ)。HashMap和其他map/dictionary类型的集合在封面下通过键直接访问值。