无法通过 Vala 中的对象访问接口实现的方法
我想创建一个 Creature 类,它将成为所有生物(例如人类)的父类。
所以我写了一个具有适当继承性的生物类和人类类。
由于我希望所有生物都做几件事,并且不想为 Creature 类中的每个行为创建默认函数,因此我创建了一个由所有生物实现的 CreatureInterface。
这是代码:
creature.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();
}
human.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
已在人类中实现,并且是 <代码>公共。
那么,为什么它无法访问呢?
I want to create a Creature class who will be the parent class of all living creatures such as humans.
So I wrote a Creature class and a Human class with proper heritage.
As I want all the creatures to do a couple a things and I don't want to make a default function for each behaviour in the Creature class, I created a CreatureInterface that is implemented by all the living creatures.
This is the code :
creature.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();
}
human.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;
}
}
Now when I compile, I have the following error that I don't understand :
./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
as been implemented in human and is public
.
So, why is it unreachable ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
具有该方法的是 CreatureInterface,而不是 Creature。
It's CreatureInterface that has that method, not Creature.