Class 精灵&x27;s的可写属性

Class 精灵&x27;s的可写属性,class,properties,genie,Class,Properties,Genie,我从报纸上读到的 仅供阅读:vala public int b { get; private set; } 在精灵中: prop readonly b: int 对于writeonly: 瓦拉: 精灵:[此行:语法错误] prop writeonly b: int 如何在精灵中声明一行写入的属性? 也许是什么 prop XXX b: int 我们可以编写四行writeonly属性: class Wonly _b: int prop b: int set

我从报纸上读到的

仅供阅读:vala

public int b { get; private set; }
在精灵中:

prop readonly b: int
对于writeonly: 瓦拉:

精灵:[此行:语法错误]

prop writeonly b: int
如何在精灵中声明一行写入的属性? 也许是什么

prop XXX b: int
我们可以编写四行writeonly属性:

class Wonly
    _b: int
    prop b: int
        set
            _b = value

init
    var w = new Wonly
    // print w.b // ERROR!! writeonly!!
    w.b = 456   // OK

但是如何编写一行writeonly属性呢?

我会在这里尽力回答您的问题,但是您的问题的某些方面还不清楚。也许多提供一点上下文,更好地解释您计划要实现的目标,以及多一点上下文化的代码会有所帮助

也就是说,我假设您正在询问Genie中类属性的语法

属性是对您开发的类的用户隐藏实现细节的方法。根据瓦拉的说法,这一举动也被称为计算机科学中的最新进展

在Vala中,房地产将按照以下方式在a中定义:

static int current_year = 2525;
class Person : Object {
    private int year_of_birth = 2493;

    public int age {
        get { return current_year - year_of_birth; }
        set { year_of_birth = current_year - value; }
    }
}
在精灵中,它看起来是这样的:

class Foo : Object

    prop name : string

    prop readonly count : int

    [Description(nick="output property", blurb="This is the output property of the Foo class")]    
    prop output : string
        get
            return "output"
        set
            _name = value
现在是只写属性。这是一个有点争议的问题的基础上和问题,所以。它似乎只有在你不打算读你写的东西时才有用。但正如您在上面的问题中所看到的,大多数答案都建议创建方法,而不是使用只写属性

这将引导我们了解您所指的语法:

public int b { private get; set; } 
您声明这是Vala中只写属性的语法,这似乎是正确的。这是因为,通过将get设置为private,可以防止用户读取该值。您可以在Vala中将get或set设置为private,也就是说,在Vala中,您可以简单地删除private get部分

这就是我不确定的地方,但我建议您在代码中尝试一下。基于Vala通过将私有getter或setter从set块中移除来设置它们的能力,我怀疑这同样适用于Genie

我从以下代码中删除了get的设置,并对其进行了编译:

[indent=4]

class Foo : Object

    prop name : string

    prop readonly count : int

    [Description(nick="output property", blurb="This is the output property of the Foo class")]
    prop output : string
        set
            _name = value

init
    var foo = new Foo()

也许这就是你想要的,但我不确定它在真正的代码中是否能工作。如果没有,也许你最好用方法来代替。

我会尽力回答你的问题,但是你问题的某些方面还不清楚。也许多提供一点上下文,更好地解释您计划要实现的目标,以及多一点上下文化的代码会有所帮助

也就是说,我假设您正在询问Genie中类属性的语法

属性是对您开发的类的用户隐藏实现细节的方法。根据瓦拉的说法,这一举动也被称为计算机科学中的最新进展

在Vala中,房地产将按照以下方式在a中定义:

static int current_year = 2525;
class Person : Object {
    private int year_of_birth = 2493;

    public int age {
        get { return current_year - year_of_birth; }
        set { year_of_birth = current_year - value; }
    }
}
在精灵中,它看起来是这样的:

class Foo : Object

    prop name : string

    prop readonly count : int

    [Description(nick="output property", blurb="This is the output property of the Foo class")]    
    prop output : string
        get
            return "output"
        set
            _name = value
现在是只写属性。这是一个有点争议的问题的基础上和问题,所以。它似乎只有在你不打算读你写的东西时才有用。但正如您在上面的问题中所看到的,大多数答案都建议创建方法,而不是使用只写属性

这将引导我们了解您所指的语法:

public int b { private get; set; } 
您声明这是Vala中只写属性的语法,这似乎是正确的。这是因为,通过将get设置为private,可以防止用户读取该值。您可以在Vala中将get或set设置为private,也就是说,在Vala中,您可以简单地删除private get部分

这就是我不确定的地方,但我建议您在代码中尝试一下。基于Vala通过将私有getter或setter从set块中移除来设置它们的能力,我怀疑这同样适用于Genie

我从以下代码中删除了get的设置,并对其进行了编译:

[indent=4]

class Foo : Object

    prop name : string

    prop readonly count : int

    [Description(nick="output property", blurb="This is the output property of the Foo class")]
    prop output : string
        set
            _name = value

init
    var foo = new Foo()

也许这就是你想要的,但我不确定它在真正的代码中是否能工作。如果不是这样,也许你最好改用方法。

对不起!Luís,我的问题没有被澄清!,我必须更新它!。很抱歉Luís,我的问题没有被澄清!,我必须更新它!。我明白您现在想要什么了,似乎不可能有一行writeonly属性定义。但不要相信我的话,因为我真的开始学习语言了。希望其他人能帮你解决问题。让我们等等。我现在知道你想要什么了,似乎不可能有一行writeonly属性定义。但不要相信我的话,因为我真的开始学习语言了。希望其他人能帮你解决问题。让我们等等。