This site requires JavaScript, please enable it in your browser!
Greenfoot back
K_O_P
K_O_P wrote ...

2012/6/17

More classes expected

K_O_P K_O_P

2012/6/17

#
Hi! I've a problem in one of my simulations. I've got an interface class named PurseOwner and of course the class Actor. Now I need a constructor for, that expects an Object, that is a subclass of both. (instance of PurseOwner and Actor). How can I do this?
SPower SPower

2012/6/17

#
Do you mean an interface or a class with:
K_O_P wrote...
...I've got an interface class named PurseOwner...
If you mean an interface, create a class like this:
public class TheClass extends Actor implements PurseOwner
{
   // code
}
Use objects of that class. I hope this helps
K_O_P K_O_P

2012/6/17

#
No, what I meant is, that I've got a constructor of a class
public class Investor  extends Customer
{
    public PurseOwner Object;

    public Investor(PurseOwner Object)
    {
        this.Object=Object;
        Init();
    }
[...]
}
And what do I have to write instead of PurseOwner, when 'Object' has to be an instance of Actor AND PurseOwner?
SPower SPower

2012/6/17

#
Make PurseOwner a subclass of actor, and just stay using PurseOwner. In java, there is no multiple inheritance like this:
public class MyClass extends Actor, PurseOwner
{
     [...]
}
That just doesn't exist.
K_O_P K_O_P

2012/6/17

#
But PurseOwner is an interface class and has to be an interface class... cause not all classes, that implements PurseOwner are subclasses of Actor... And I want to know what I have to write, that it is only possible to specify Objects that are subclasses of Actor AND implements the PurseOwner class.
SPower SPower

2012/6/17

#
By interface class, do you mean that you wrote this:
public interface PurseOwner
{
   //....
}
instead of:
public class PurseOwner
{
    //...
}
If yes, you could do this: Create a class, which implements the PurseOwner interface. Also let it be a subclass of Actor. Use that class, that's the best thing I can think of right now.
K_O_P K_O_P

2012/6/17

#
mhh... Thanks for you help! That was a thing, I although thought about... But that don't solves the problem... Cause there are different classes that are subclasses of Actor, which should not implement PurseOwner, but some of their subclasses implemts it...
davmac davmac

2012/6/17

#
You can't do this. The closest you could get is to have a "PurseOwnerActor" interface which extends the "PurseOwner" interface, and have your actors which implement PurseOwner actually implement PurseOwnerActor instead. Then specify PurseOwnerActor as the type when you want to require that the object must be both a PurseOwner and an Actor. The compiler won't enforce this, of course, but most of the time you shouldn't really need it to.
K_O_P K_O_P

2012/6/17

#
Ok, thank you! I'll try this.
You need to login to post a reply.