公共字符串 blaBla { 得到;放;在Python中
考虑以下示例以更好地理解我的问题:
public class ClassName
{
public ClassName { }
public string Val { get; set; }
...
}
ClassName cn = new ClassName();
cn.Val = "Hi StackOverflow!!";
Python 中的这段代码相当于什么?
Consider the following example to understand better my question:
public class ClassName
{
public ClassName { }
public string Val { get; set; }
...
}
ClassName cn = new ClassName();
cn.Val = "Hi StackOverflow!!";
What would be the equivalent of this code in python?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以轻松地将成员添加到任何 Python 对象,如其他答案中所示。对于更复杂的 get/set 方法(如 C# 中的方法),请参阅内置的 property :
You can easily add members to any Python object as show in other answers. For more complicated get/set methods like in C#, see the property builtin:
从这个意义上来说,Python 没有 getter 和 setter。以下代码与上面的代码等效:
请注意,python 没有提及 getters/setters;在设置之前,您甚至不需要声明
val
。要制作自定义 getter/setter,您可以这样做:Python does not have getters and setters in this sense. The following code is the equivalent to the above code:
Notice that python has no mention of getters/setters; you don't even need to declare
val
until you set it. To make custom getters/setters you could do for example this: