简体   繁体   中英

Passing a custom object in Java

I know this is probably pretty basic, and I'm ashamed for not knowing how to do this (and worse yet, being unsuccessful in searching for a solution).

How would I pass an object of type thing from A to C?

public class A extends B {

}

public class B {

    public class thing {

    }

}

public class C extends JFrame {

}

I have access to thing in A because I'm extending B, but I'm unable to extend B when using class C because I need to extend JFrame.

EDIT

Sorry for the vagueness. Class A has a collection of objects of type thing and I want to iterate through those objects in class C.

EDIT 2

And of course... the obvious choice. Make thing its own class... :(

Admitting shame for the sake of those who may also have an issue like this.

First, if it's an inner class - you probably shouldn't access it from the outside...
But, if you insist, you can try to do as follows:

public class A extends B {

}

public class B {

    public class thing {

    }

    private thing mything = new thing();
    public thing getThing(){
      return mything;
    }

}

public class C extends JFrame {
      A a = new A();
      Object thing = a.getThing();
}

accessing an inner class from the outside is generally a bad idea, but if you must i wold suggest looking at the delegate design pattern like in this article :

delegate design pattern

You probably trying to use multi-inheritance which is not possible in java! you can use interfaces to have 2 different behavior or Use some design patterns to achieve such a goal.

It's really unclear what you are trying to do.however see composite or factory-method patterns i think it could help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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