简体   繁体   English

Java“扩展”对象

[英]Java “extending” an object

I have only recently gotten back into Java programming and am currently stuck with a certain problem. 我只是最近才回到Java编程领域,目前遇到了某些问题。

I have an object of a certain type: 我有某种类型的对象:

StandardClass stdClass = (StandardClass)request.getAttribute(ATTR);  

But now I need to make the Object stdClass an Observer of some Observable. 但是现在我需要使Object stdClass成为某些Observable的观察者。
Since StandardClass doesn't implement the Observer interface I can't add it as an observer directly. 由于StandardClass没有实现Observer接口,因此我无法将其直接添加为观察者。 So I tried: 所以我尝试了:

ExtendedClass extends StandardClass implements Observer { ... }

But I haven't been able to find a way to cast the original object into the new class. 但是我还没有找到将原始对象转换为新类的方法。

Possibly this whole approach is totally wrong and I currently can't see the forest for the trees, so I'd appreciate any input on how to solve this kind of problem. 可能整个方法是完全错误的,并且我目前看不到树木茂盛的森林,因此,我很感激任何有关如何解决此类问题的建议。

Thank you very much in advance for your time and help, I really appreciate it!! 预先非常感谢您的时间和帮助,我非常感谢!!

Cheers, 干杯,
Thomas 托马斯

You won't be able to cast the original object into an ExtendedClass , because it is not an instance of that class. 您将无法将原始对象转换为ExtendedClass ,因为它不是该类的实例。 The existence of a subclass does not mean that superclass instances can be somehow coerced into these subclasses. 子类的存在并不意味着可以以某种方式将超类实例强制为这些子类。 In this case, your stdClass object is an instance of StandardClass only, so it cannot be downcast successfully. 在这种情况下,您的stdClass对象仅是StandardClass一个实例,因此无法成功向下转换。

EDIT to clarify: Of course, some objects stored in StandardClass variables might well actually be ExtendedClass objects. 编辑以澄清:当然,存储在StandardClass变量中的某些对象实际上很可能是ExtendedClass对象。 My understanding was that you haven't modified the actual object stored in the request, merely created a subclass. 我的理解是,您尚未修改存储在请求中的实际对象,仅创建了一个子类。 When you said you couldn't perform the cast, I presumed that you mean the cast failed at runtime with a ClassCastException (some casts will be rejected by the compiler because they cannot succeed, eg an Integer can never be cast to a String , but this shouldn't be one of those cases). 当您说无法执行转换时,我想您是说转换在运行时失败,并带有ClassCastException (某些转换将被编译器拒绝,因为它们无法成功,例如, Integer不能转换为String ,但是这不应该是这些情况之一)。 If you created the subclass and modified your application such that the object stored in the request was created as an ExtendedClass object, then casting should succeed. 如果创建了子类修改了应用程序,以使存储在请求中的对象被创建为ExtendedClass对象,则转换应成功。

To achieve what you want to do, a cleaner approach would be some sort of wrapper class , which contains a StandardClass instance and implements the Observer methods itself. 为了实现您想做的事情,一种更简洁的方法是使用某种包装器类 ,该包装器类 包含一个StandardClass实例并实现Observer方法本身。 It can then call appropriate methods on StandardClass in response to the observed events. 然后,它可以响应于观察到的事件在StandardClass上调用适当的方法。

If the object at request.getAttribute(ATTR) is truly an Observable , then you can just cast it as so: 如果request.getAttribute(ATTR)处的对象确实是Observable ,则可以按以下方式进行强制转换:

Observable obs = (Observable) request.getAttribute(ATTR);

The compiler will not allow you to cast from one type to an incompatible type, if it knows that StandardClass is not an Observable . 如果编译器知道StandardClass不是Observable ,则不允许您将其从一种类型转换为不兼容的类型。 Or, you could simply declare the reference as ExtendedClass . 或者,您可以简单地将引用声明为ExtendedClass

With your approach: 使用您的方法:

request.getAttribute(ATTR);

Should be returning an object type of ExtendedClass in order to cast it to an Observer for example: 为了将其强制转换为Observer,应返回ExtendedClass的对象类型,例如:

Observer observer = (Observer) request.getAttribute(ATTR);

If you need a visual, take a look at the at the Observer Pattern Diagram 如果需要视觉效果,请查看“观察者模式图”。

http://upload.wikimedia.org/wikipedia/commons/8/8d/Observer.svg http://upload.wikimedia.org/wikipedia/commons/8/8d/Observer.svg

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM