这听起来像一个基本的东西,我不能让它工作。
我们假设我需要一个应用程序来存储我公司的所有工作人员。 他们做不同的事情,他们的工作价格取决于专业的具体情况。 对于每个工人,我需要一个价格。
我做的事:
创建Worker的接口
public interface Worker { int getPrice(); }创建一个LanguageTranslator类,其工资取决于已翻译的文本的NumberOfLines。 该类实现了Worker。 当然,对于其他专业人士,我会有其他影响价格的因素。
public class LanguageTranslator implements Worker { private int numberOfLines; @Override public int getPrice() { return this.numberOfLines * 14; } public void setNumberOfLines(int numberOfLines) { this.numberOfLines = numberOfLines; } }现在,我想将所有工作者存储在List,Map或任何集合中,并且可以直接访问setNumberOfLines方法。
public static void main(String[] args) { List<Worker> workers = new ArrayList<Worker>(); Worker worker = new LanguageTranslator(); workers.add(worker); //How to get to LanguageTranslator.setNumberOfLines() method? } }如果我直接去收集
workers.get(0).[only interface methods visible];只有接口方法可用。 有没有办法获得特定的LanguageTranslator方法?
我想这可能是非常糟糕的设计。 我可以搜索的任何关键字?
提前致谢。
This sounds like a basic stuff and I cannot get it work.
Let's assume I need an application that stores all the workers in my company. They do different stuff and the price for their job depends on the specifics of the profession. For each worker I need a price.
What I do:
Create an interface of a Worker
public interface Worker { int getPrice(); }Create a class of LanguageTranslator who's salary depends on NumberOfLines of text which is translated. The class implements Worker. Of course, for other proffesions I will have other factors influencing the price.
public class LanguageTranslator implements Worker { private int numberOfLines; @Override public int getPrice() { return this.numberOfLines * 14; } public void setNumberOfLines(int numberOfLines) { this.numberOfLines = numberOfLines; } }And now, I want to store all the workers in a List, Map or whatever collection and have direct access to setNumberOfLines method.
public static void main(String[] args) { List<Worker> workers = new ArrayList<Worker>(); Worker worker = new LanguageTranslator(); workers.add(worker); //How to get to LanguageTranslator.setNumberOfLines() method? } }If I go directly to the collection
workers.get(0).[only interface methods visible];only interface methods are available. Is there any way to get to the specific LanguageTranslator methods?
I suppose this may be very bad design. Any keywords I could search for?
Thanks in advance.
最满意答案
当你从列表中获取它时,你需要转换每个对象。 在你提到的评论中,你将有不同的职业,稍后将被添加。 由于您不知道将其强制转换为哪种类型,因此您需要进行检查。
Object object = workers.get(0); if(object instanceof LanguageTranslator) { LanguageTranslator lt = (LanguageTranslator) object; lt.setNumberOfLines(7); } else if(object instanceof Accountant) { Accountant acc = (Accountant) object; acc.doSomething(); } etc...You'll need to cast each object as you get it from the list. In the comments you mentioned that you'll have different professions that will later be added. Since you don't know which type to cast it as, you'll need to check.
Object object = workers.get(0); if(object instanceof LanguageTranslator) { LanguageTranslator lt = (LanguageTranslator) object; lt.setNumberOfLines(7); } else if(object instanceof Accountant) { Accountant acc = (Accountant) object; acc.doSomething(); } etc...更多推荐
发布评论