如何获取存储在集合中的对象的特定方法?(How to get to specific method of object stored in collection?)

编程入门 行业动态 更新时间:2024-10-28 10:36:43
如何获取存储在集合中的对象的特定方法?(How to get to specific method of object stored in collection?)

这听起来像一个基本的东西,我不能让它工作。

我们假设我需要一个应用程序来存储我公司的所有工作人员。 他们做不同的事情,他们的工作价格取决于专业的具体情况。 对于每个工人,我需要一个价格。

我做的事:

创建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...

更多推荐

本文发布于:2023-07-21 07:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1208073.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   方法   specific   method   stored

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!