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

2013/11/7

Animal vs Bee

ddvink ddvink

2013/11/7

#
Here's a MySpace-object from the World-class Where I declare two Bee's. See the code below.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MySpace here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MySpace extends World
{
    private Bee dennis;
    private Animal debbie;
    private Apple thisApple;
    
    public MySpace()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        System.out.println();
        
        
        // dennis is from class Bee
        dennis = new Bee();

        dennis.setName("Dennis");
        System.out.println(dennis.getName());

        dennis.setOursOfFlying(20);
        System.out.println(dennis.getOursOfFlying());
        
        // debbie is from class Bee.         
        debbie = new Bee();

        debbie.setName("Debbie");
        System.out.println(debbie.getName());

        addObject(dennis, 200, 200);
        addObject(debbie, 300, 300);
    }
}
Which getters and setters should be accesible for which bee? The reason for my question is that when I do a ctrl+space after dennis, I get access to getName and getOursOfFlying. When I do that do debbie, I only can access the getName. Why is that? Yours, Dennis
Zamoht Zamoht

2013/11/7

#
private Animal debbie; You keep debbie as an Animal object. Change that to 'Bee'.
danpost danpost

2013/11/7

#
I do not see you doing to debbie what you do to dennis on lines 28 and 29 (not that doing that will fix your problem -- may need to see the Bee class code). EDIT: good catch Zamoht.
ddvink ddvink

2013/11/7

#
Thanx both of you. And I won't change debbie to a bee ;-) a. She's my girlfriend and I like animals, but not bee's b. It's for testing purposes But seriously. I hoped for an answer like this one. The strange thing, In my opinion, is that when the image of debbie is on the screen, I do have access to debbie's flyingours. What's the difference? yours, Dennis
danpost danpost

2013/11/7

#
You could always typecast debbie to a Bee object when using it:
System.out.println((Bee)debbie).getOursOfFlying());
You need to login to post a reply.