Getters 和 Setters - "value" 是如何工作的,尤其是在 if 语句中?
Getters and Setters - How does "value" work, particularly in an if statement?
我一直在研究这个视频:https://www.youtube.com/watch?v=GhQdlIFylQ8&t=11038s
我正在 3:54:60 完成 "Getters & Setters"(C# 的其他主题在任何需要的上下文的描述中)。
这是我为了打印到控制台而制作的 class 以及 "Getter and Setter"。
class Song
{
private string title;
public string artist;
public int duration;
public Song(string aTitle, string aArtist, int aDuration)
{
title = aTitle;
artist = aArtist;
duration = aDuration;
}
public string Title
{
get { return title; }
set {
if (value == "Hello")
{
value = "ERROR";
} else
{
title = value;
}
}
}
}
这是 "Main" 类代码,用于打印两个 Song
objects 的标题:"hello" 和 "kashmir"。
class Program
{
static void Main(string[] args)
{
Song hello = new Song("Hello", "Adele", 400);
Song kashmir = new Song("Kashmir", "Green Day", 200);
Console.WriteLine(hello.Title);
Console.WriteLine(kashmir.Title);
Console.ReadLine();
}
}
但是,我尝试使用 "Getters and Setters" 进行试验,看看如何打印出歌曲标题以外的内容。
当我运行程序时,它打印出Hello
和Kashmir
在不同的行上。
如何让它打印 ERROR
或歌曲标题以外的其他内容(或者我可以通过其他什么方式来打印)?
您不应将 "Error" 分配给值关键字。
Value 关键字用于保存输入。如果您想在为字段赋值之前执行验证,这将很有用。如果您将某些东西分配给 Value 它将不会被 class.
持有
编辑 -
如以下评论所述 - 您的代码 未使用 属性 setter 但它是 通过构造函数设置值 .
你有两个选择 -
选项 1 - 在构造函数中添加相同的验证。
选项 2 - 在构造函数中设置属性而不是字段
选项 2 对我来说更有意义,因为它避免了代码重复。
您必须对产权进行小幅改动
public string Title
{
get { return title; }
set {
if (value == "Hello")
{
title = "ERROR";
} else
{
title = value;
}
}
}
这里发生了一件有点有趣的事情,即使是经验丰富的开发人员偶尔也会被绊倒……而且它忽略了支持字段和 属性 之间的区别。这种情况时有发生......并且绝对让开发人员感到困惑,直到他们意识到他们犯了 Visual Studio 智能感知辅助错误。如果您将支持字段命名为与 属性 名称相同的名称,则可能性加倍...仅在大小写上有所不同。
class Song
{
private string title; //--> this is the "backing field" for the property "Title"
public string artist;
public int duration;
public Song(string aTitle, string aArtist, int aDuration)
{
//title = aTitle; //--> this only sets the backing field...not the "Title" property
Title = aTitle; //--> this sets your "Title" property
artist = aArtist;
duration = aDuration;
}
下一个问题是,在 属性 set
中,value
是 incoming 建议值 属性.你不必接受它。您在 属性 set
中的工作是验证传入值,如果您愿意,将其放入支持字段中......或者 可能 放入其他内容如果你不这样做。更改传入的 value
很少是您想要做的。你几乎做对了......这是评论修复:
public string Title
{
get { return title; }
set {
if (value == "Hello")
{
//value = "ERROR"; //--> this only changes the incoming value
title = "ERROR"; //--> this sets your backing field
} else
{
title = value;
}
}
}
}
综上所述,看到构造函数只设置支持字段,而不是通过 属性 set
的有时昂贵的验证代码并不少见。很多时候,属性 是 public API 供您类型的用户设置值...而构造函数是保持内部或私有,仅用于填写已保存在某处的值......就像在数据库中一样。一个好主意? Maybe/maybe 没有。正如您刚刚发现的那样,绕过 属性 set
可能会给您留下一些可能是错误的东西。
我一直在研究这个视频:https://www.youtube.com/watch?v=GhQdlIFylQ8&t=11038s
我正在 3:54:60 完成 "Getters & Setters"(C# 的其他主题在任何需要的上下文的描述中)。
这是我为了打印到控制台而制作的 class 以及 "Getter and Setter"。
class Song
{
private string title;
public string artist;
public int duration;
public Song(string aTitle, string aArtist, int aDuration)
{
title = aTitle;
artist = aArtist;
duration = aDuration;
}
public string Title
{
get { return title; }
set {
if (value == "Hello")
{
value = "ERROR";
} else
{
title = value;
}
}
}
}
这是 "Main" 类代码,用于打印两个 Song
objects 的标题:"hello" 和 "kashmir"。
class Program
{
static void Main(string[] args)
{
Song hello = new Song("Hello", "Adele", 400);
Song kashmir = new Song("Kashmir", "Green Day", 200);
Console.WriteLine(hello.Title);
Console.WriteLine(kashmir.Title);
Console.ReadLine();
}
}
但是,我尝试使用 "Getters and Setters" 进行试验,看看如何打印出歌曲标题以外的内容。
当我运行程序时,它打印出Hello
和Kashmir
在不同的行上。
如何让它打印 ERROR
或歌曲标题以外的其他内容(或者我可以通过其他什么方式来打印)?
您不应将 "Error" 分配给值关键字。
Value 关键字用于保存输入。如果您想在为字段赋值之前执行验证,这将很有用。如果您将某些东西分配给 Value 它将不会被 class.
持有编辑 - 如以下评论所述 - 您的代码 未使用 属性 setter 但它是 通过构造函数设置值 .
你有两个选择 -
选项 1 - 在构造函数中添加相同的验证。
选项 2 - 在构造函数中设置属性而不是字段
选项 2 对我来说更有意义,因为它避免了代码重复。
您必须对产权进行小幅改动
public string Title
{
get { return title; }
set {
if (value == "Hello")
{
title = "ERROR";
} else
{
title = value;
}
}
}
这里发生了一件有点有趣的事情,即使是经验丰富的开发人员偶尔也会被绊倒……而且它忽略了支持字段和 属性 之间的区别。这种情况时有发生......并且绝对让开发人员感到困惑,直到他们意识到他们犯了 Visual Studio 智能感知辅助错误。如果您将支持字段命名为与 属性 名称相同的名称,则可能性加倍...仅在大小写上有所不同。
class Song
{
private string title; //--> this is the "backing field" for the property "Title"
public string artist;
public int duration;
public Song(string aTitle, string aArtist, int aDuration)
{
//title = aTitle; //--> this only sets the backing field...not the "Title" property
Title = aTitle; //--> this sets your "Title" property
artist = aArtist;
duration = aDuration;
}
下一个问题是,在 属性 set
中,value
是 incoming 建议值 属性.你不必接受它。您在 属性 set
中的工作是验证传入值,如果您愿意,将其放入支持字段中......或者 可能 放入其他内容如果你不这样做。更改传入的 value
很少是您想要做的。你几乎做对了......这是评论修复:
public string Title
{
get { return title; }
set {
if (value == "Hello")
{
//value = "ERROR"; //--> this only changes the incoming value
title = "ERROR"; //--> this sets your backing field
} else
{
title = value;
}
}
}
}
综上所述,看到构造函数只设置支持字段,而不是通过 属性 set
的有时昂贵的验证代码并不少见。很多时候,属性 是 public API 供您类型的用户设置值...而构造函数是保持内部或私有,仅用于填写已保存在某处的值......就像在数据库中一样。一个好主意? Maybe/maybe 没有。正如您刚刚发现的那样,绕过 属性 set
可能会给您留下一些可能是错误的东西。