开发者

Extending a class in Java

I got a list of objects like this

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

And a child class

class M开发者_如何学GoyPage extends Page
{

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

Is it possible somehow to convert the Page objects into MyPage objects?

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 (e.g. 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:

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. 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. 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; then you can call newFunction directly on Page references without needing to downcast.

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:

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, i.e.

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. 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. 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:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜