0

我想创建一个 Creature 类,它将成为人类等所有生物的父类。

所以我写了一个具有适当遗产的 Creature 类和一个 Human 类。

因为我希望所有生物都做几件事,并且我不想为 Creature 类中的每个行为创建一个默认函数,所以我创建了一个由所有生物实现的 CreatureInterface。

这是代码:

生物.vala:

// Different kind of genders
public enum GENDER
{
    MALE,
    FEMALE
}

// Different kind of moods
public enum MOOD
{
    HAPPY,
    SAD,
    NEUTRAL
}

// Different kind of body size for basic physical representation
public enum BODY_SIZE
{
    STANDARD,
    TALL,
    SMALL
}

// Different kind of body weight for basic physical representation
public enum BODY_WEIGHT
{
    STANDARD,
    FAT,
    THICK
}

public class Creature
{

    // Physic

    protected BODY_SIZE _body_size = BODY_SIZE.STANDARD;
    protected BODY_WEIGHT _body_weight = BODY_WEIGHT.STANDARD;

    // Mental

    protected MOOD _mood = MOOD.NEUTRAL;

    // Social

    protected GENDER _gender = GENDER.MALE;
    protected string _name = "";
    protected string _family_name = "";

    protected Creature _mother = null;
    protected Creature _father = null;
    protected List<Creature> _children = null;

    // Reproduction

    protected int _number_of_babies_by_pregnancy = 0;
    protected int _uncommon_number_of_babies_by_pregnancy = 0;
    protected int _very_uncommon_number_of_babies_by_pregnancy = 0;
    protected int _pregnancy_duration = 0; // In days

    public Creature ()
        {

            if ( Random.int_range(0, 2) == 1.0 )
            {
                this._gender = GENDER.MALE;
            }
            else
            {
                this._gender = GENDER.FEMALE;
            }

        }

    ~Creature ()
        {
            stdout.printf( "I'm dying" );
        }



}

public interface CreatureInterface
{

    // Generate a name with specific rules for species
    protected abstract void generateName();

    // Get a goal for the next action
    public abstract void getAGoal();

}

人类.vala:

public class Human : Creature, CreatureInterface
{

    public Human ()
    {

        // Get a name for our new human being
        this.generateName();

        // Social

        string name = this._name;
        string family_name = this._family_name;

        if ( this._gender == GENDER.MALE )
        {
            stdout.printf( @"Say \"hello\" to $family_name $name, a human male baby.\n" );
        }
        else
        {
            stdout.printf( @"Say \"hello\" to $family_name $name, a human female baby.\n" );
        }

        // Reproduction

        this._number_of_babies_by_pregnancy = 1;
        this._uncommon_number_of_babies_by_pregnancy = 2;
        this._very_uncommon_number_of_babies_by_pregnancy = 3;
        this._pregnancy_duration = 275; // 9 months

    }

    /**
     * Destructor
     */
    ~Human ()
    {
    }

    public void generateName()
    {

        if ( this._gender == GENDER.MALE )
        {
            this._name = "Jhon";
        }
        else
        {
            this._name = "Jane";
        }

        this._family_name = "Doe";

    }

    public void getAGoal()
    {
        stdout.printf("I need a goal...");
    }

}

main.vala:

public class Main
{
    public static int main (string[] args)
        {

            stdout.printf( "Genesis\n" );

            Creature john_doe = new Human();
            john_doe.getAGoal();

            return 0;
        }
}

现在,当我编译时,出现以下我不明白的错误:

./src/main.vala:9.4-9.20: error: The name `getAGoal' does not exist in the con
text of `Creature?'                                                          
                        john_doe.getAGoal();
                        ^^^^^^^^^^^^^^^^^
Compilation failed: 1 error(s), 0 warning(s)
make: *** [build] Erreur 1

getAGoal已在人类中实施,并且是public.

那么,为什么它无法访问?

4

1 回答 1

1

具有该方法的是 CreatureInterface,而不是 Creature。

于 2011-07-30T08:26:44.230 回答