属性的语法更简单? [英] Simpler syntax for properties?

查看:51
本文介绍了属性的语法更简单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有其他人认为能够写出类似

这样的东西会很好...


public SomeType P {v; }


....而不是这个?


public SomeType P

{

得到{return v; }

set {v = value; }

}


我意识到属性构造允许''get''和'set''做多于

只是分配或返回一个值,但在大多数情况下 - 当我不想做什么时

其他任何东西 - 标准形式似乎有点啰嗦。

P.

解决方案



" Paul E Collins" <音响****************** @ CL4.org>在消息中写道

news:c5 ********** @ sparta.btinternet.com ...

还有其他人认为这样会很好吗能够写出像这样的东西......

公共SomeType P {v; }

...而不是这个?

公共SomeType P
{
get {return v; }
设置{v =值; }


我意识到属性构造允许''get''和'set''做更多
而不仅仅是分配或返回一个值,但在大多数情况下 - 当我不想做任何其他事情时 - 标准形式似乎有点啰嗦。




我绝对同意,语法很长,并不像应该的那样清晰。我实际上可能是少数几个认为字段应该降级到

稀疏使用并且编译器语言本应该付出更多努力的人之一

以消除手动声明字段的需要。


我看到一些语法表明添加了属性关键字

和\或字段关联关键字。我个人喜欢:

公共财产SomeType P {get; set;} //新关键字



public implicit SomeType P {get; set;} //保存一个关键字,但是重载一个

现有的一个

或者只是

public SomeType P {get; set;} / /没有关键字,但它是否像上面的那样明确

并且人们会误以为它是抽象的吗?

其中编译器生成自己的字段来存储支持类型。 P =

< value>在声明类调用set中,如果它可用,但在属性为get时由编译器转换为字段赋值。类似的
如果仅设置属性,则可以为get执行
语义。对于

类中的行为有多明显,有一些问题。但是我觉得它比试图将get,set和字段

关联混合到属性中更清晰。


Paul E Collins写道:< blockquote class =post_quotes>
是否有人认为能够写出像
这样的东西会很好...

公共SomeType P {v; }

...而不是这个?

公共SomeType P
{
get {return v; }
设置{v =值; }


我意识到属性构造允许''get''和'set''做的不仅仅是分配或返回一个值,但在大多数情况下案例 - 当我不想做其他任何事情时 - 标准形式似乎有点啰嗦。




什么是错的w /然后公开数据成员?


public SomeType P;


Julie< ju *** @ nospam.com>写道:

什么是错误的/只是暴露数据成员呢?

公共SomeType P;




Everything :)


如果你想稍后更改实现,你就不能。

如果你想限制读或写的访问权限只有,你不能。

如果你想验证集合上的值,你就不能。

如果你想在调试过程中追踪使用,你可以't。


除非某些东西实际上是常数(无论是实际的const还是

只是静态,只读和不可变的东西),我总是保持

私人。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复小组,请不要给我发邮件


Does anyone else think it would be nice to be able to write something like
this ...

public SomeType P { v; }

.... rather than this?

public SomeType P
{
get { return v; }
set { v = value; }
}

I realise that the property construct allows ''get'' and ''set'' to do more than
just assign or return a value, but in most cases - when I don''t want to do
anything else - the standard form seems slightly long-winded.

P.

解决方案


"Paul E Collins" <fi******************@CL4.org> wrote in message
news:c5**********@sparta.btinternet.com...

Does anyone else think it would be nice to be able to write something like
this ...

public SomeType P { v; }

... rather than this?

public SomeType P
{
get { return v; }
set { v = value; }
}

I realise that the property construct allows ''get'' and ''set'' to do more
than
just assign or return a value, but in most cases - when I don''t want to do
anything else - the standard form seems slightly long-winded.



I definatly agree, the syntax is long and not as clear as it should be. I
actually may be one of the few that thinks fields should be relegated to
sparse use and that the compiler\language should have made a greater effort
to remove the need for manual declaration of fields.

I''ve seen some syntax that suggests the addition of a property keyword
and\or a field associative keyword. Personally I like:
public property SomeType P{get;set;} //new keyword
or
public implicit SomeType P{get;set;} // saves a keyword, but overloads an
existing one
or just
public SomeType P{get;set;} //no keyword, but is it as clear as the above
and will people mistake it for abstract?
where the compiler generates its own field to store the backing type. P =
<value> in the declaring class calls set if its available but is transformed
into a field assign by the compiler when the property is get only. Similar
semantics could be performed for get if the property is set only. There is
some problem with that as far as how apparent the behaviour is within the
class. However I find it cleaner than trying to mix get, set, and a field
association into the property.


Paul E Collins wrote:


Does anyone else think it would be nice to be able to write something like
this ...

public SomeType P { v; }

... rather than this?

public SomeType P
{
get { return v; }
set { v = value; }
}

I realise that the property construct allows ''get'' and ''set'' to do more than
just assign or return a value, but in most cases - when I don''t want to do
anything else - the standard form seems slightly long-winded.



What''s wrong w/ just exposing the data member then?

public SomeType P;


Julie <ju***@nospam.com> wrote:

What''s wrong w/ just exposing the data member then?

public SomeType P;



Everything :)

If you want to later change the implementation, you can''t.
If you want to limit access to read or write only, you can''t.
If you want to validate values on set, you can''t.
If you want to trace use during debug, you can''t.

Unless something is effectively constant (whether an actual const or
just something which is static, readonly and immutable), I always keep
it private.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


这篇关于属性的语法更简单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆