简体   繁体   English

用Java扩展类

[英]Extending a class in Java

I got a list of objects like this 我得到了这样的对象列表

ArrayList <Page> pageList = aForeignObject.getAllPages();

And a child class 和一个儿童班

class MyPage extends Page
{

    public void newFunction()
    {
        // A new Feature
    }
}

Is it possible somehow to convert the Page objects into MyPage objects? 有可能以某种方式将Page对象转换为MyPage对象吗?

I would love to do sth like this: 我很乐意这样做:

MyPage page = pages.get(1); // This will obviously fail 
page.newFunction();

If the Page objects coming out of the getAllPages() method are actually MyPage objects, then simply use a cast (eg MyPage page = (MyPage) pages.get(1); . If they're not (as is likely if you're using external code), you can't use sub classes. What you could do, however, is use composition: 如果来自getAllPages()方法的Page对象实际上是MyPage对象,那么只需使用MyPage page = (MyPage) pages.get(1);例如MyPage page = (MyPage) pages.get(1);如果它们不是(如果你可能的话)重新使用外部代码),你不能使用子类。但是,你可以做的是使用组合:

class MyPage{
    private Page p;

    public MyPage(Page aPage){
        p = aPage;
    }

    public void newFunction(){
        //stuff
    }

    public Object oldFunction(){
        return p.oldFunction();
    }
}

Then you could do stuff like: 然后你可以做以下事情:

MyPage page = new MyPage(pages.get(1));
page.newFunction();

The short solution is casting: 简短的解决方案是铸造:

 
 
 
 
  
  
  MyPage page = (MyPage)pages.get(1);
 
 
  

Note that this requires that the object in question is really of type MyPage - otherwise the cast fails with a ClassCastException . 请注意,这要求所讨论的对象实际上是 MyPage类型 - 否则转换会因 ClassCastException失败。 If you are not sure about it, you may check the type of the object first: 如果您不确定,可以先检查对象的类型:

 
 
 
 
  
  
  Page elem = pages.get(1); if (elem instanceof MyPage) { MyPage page = (MyPage)elem; ... }
 
 
  

However, the need to do such downcasts is often a sign that your class design could be improved. 然而,做这种向下倾斜的需要通常表明你的课堂设计可以改进。 If aForeignObject.getAllPages() always returns a list of MyPage objects, you should change its return type accordingly. 如果 aForeignObject.getAllPages()始终返回 MyPage对象的列表,则应相应地更改其返回类型。 Otherwise, if newFunction makes sense in the interface of Page (even as abstract or with an empty default implementation), you should consider adding it there; 否则,如果 newFunctionPage的界面中有意义(即使是抽象的或使用空的默认实现),你应该考虑在那里添加它; then you can call newFunction directly on Page references without needing to downcast. 然后你可以直接在 Page引用上调用 newFunction而无需向下转换。

Update: so you actually have Page objects which you want to convert into MyPage objects... The technical solution to this would be a converting constructor : 更新:所以你实际上有你要转换成MyPage对象的Page对象......对此的技术解决方案是转换构造函数

 class MyPage extends Page { MyPage(Page other) { // deep copy internal state } ... } 

Although technically this is working, I would still rethink the design approach which requires such solutions, if possible. 虽然从技术上说这是有效的,但如果可能的话,我仍然会重新考虑需要这些解决方案的设计方法。 If you use third-party code you can't change, this is not an option though. 如果您使用第三方代码,则无法更改,但这不是一个选项。

This is obviously messy but can work... 这显然是凌乱但可以工作......

Page page = pages.get(1);
if (page instanceof MyPage) {
  ((MyPage)page).newFunction();
}

You can hold a list of all classes that extends Page, ie 您可以保存extends Page的所有类的列表,即

List <? extends Page> pageList = aForeignObject.getAllPages(); //Bear in mind that Page objects will never be found here.

Now, if the list only contains MyPage, then you can use my example above. 现在,如果列表只包含MyPage,那么您可以使用上面的示例。 Like I said, it's not an effective nor is it an efficient solution. 就像我说的那样,它不是有效的,也不是一种有效的解决方案。

I think the solution is to implement your "new feature" in Page . 我认为解决方案是在Page实现您的“新功能”。 I had also a few of these situations before and for me, implementing it in the base class was the solution. 我之前也有过一些这样的情况,对我来说,在基类中实现它是解决方案。 But I don't know exactly in what kind of situation your are.... 但我不确切地知道你是什么样的情况....

Second solution (not recommend): make your "new feature" a static method. 第二种解决方案(不推荐):使您的“新功能”成为静态方法。 Something like this: 像这样的东西:

public static void feature(Page page)
{
     ....
}

Then, you can make a shortcut in MyPage: 然后,您可以在MyPage中创建一个快捷方式:

public void invokeFeatureMethod()
{
     feature(this);
}

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

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