多个子类,如何实例任何? [英] Multiple subclasses, how to instance any of them?

查看:485
本文介绍了多个子类,如何实例任何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这将有点难以解释,但我会尽我所能。

This will be a bit hard to explain, but I will try my best.

我想要有多个项目,当点击时,每个都有不同的结果。对于其余的,他们都有相同的变量。所以基本上,我有一个类Item,和子类Ball,Rope,Book,和许多,更多的来。我想要能够轻松添加项目,如果我想。 Item类有一个字符串名称,字符串描述和一个方法'onUse',每个子类重写。

I want to have multiple items that, when clicked, each have a different result. For the rest, they all have the same variables. So basically, I have a class Item, and subclasses Ball, Rope, Book, and many, many more to come. I want to be able to easily add items if I want to. The Item class has a String name, String description and a method 'onUse' that each of the subclasses override.

我想把这个作为动态处理,我想在我的Frame / Activity(这是为Android,但适用于Java)执行该方法一个单一的方法。

I would like to handle this as dynamical as possible, meaning I would like a single method in my Frame/Activity (This is for Android but Java applies) to execute the method.

我使用这个单一的方法:

I'm using this for a single method:

public void useItem(Item i)
{
    i.onUse();
}

我的问题是我有不同的项目保存在数据库中。我想从数据库中随机获取一个项目并实例化它;除非我没有办法知道哪个子类实现它。我尝试保存数据库中的数据类型,但是没有真正工作... Pseudocode:

My problem is that I have the different items saved in a database. I want to randomly grab one of the items from the database and instance it; except I have no way of knowing which subclass to instance it as. I tried saving the datatype in the database but that's not really working out... Pseudocode:

//Database cursor stuff
//...
                String itemType = c.getString(6);

                //Get the class name if possible
                Class itemClass= null;
                try {
                    itemClass = Class.forName(itemType);
                } catch (ClassNotFoundException)
                    e.printStackTrace();
                }

            Item l = null;
            // Check here what type it is!
            if (itemClass.equals(Ball.class)) {
                l = new Ball(name,description);

            } else if(lootClass.equals(Book.class)){

                l = new Book(name,description); 
            }

看到这么多不同的项目,将不得不迭代通过加载类(加我通常是ClassNotFoundException)

Seeing as there are so many different items this isn't really great as I would have to iterate through loads of classes (plus I often the get ClassNotFoundException)

你说什么,StackOverflow?什么是最好的方法?

What say you, StackOverflow? What would be the best approach?

推荐答案

工厂模式可能是要走的路。如果您在数据库中存储完全限定名,那么您可以创建一个读取值并使用以下内容实例化一个新项的工厂:

The factory pattern is probably the way to go. If you store the fully qualified name in the DB then you can create a factory that reads the value and instantiates a new Item using the following:

Item item = (Item) Class.forName(theDBField).newInstance();
item.setName(name);
item.setDescription(desc);

这意味着更简洁的工厂不必知道它可以构建的所有类。我真的不喜欢每次我添加一个新的实现更新一个工厂,这是一个简单的方法来避免它。

Which means a much more concise factory that doesn't have to know about all the classes it can build. I really dislike having to update a factory everytime I add a new implementation and this is a simple way to avoid it.

为此,你必须添加getter和setter到项目界面。

to do this you will have to add getters and setters to the item interface.

这篇关于多个子类,如何实例任何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆