当我在属性中设置条件时,是否必须再次在自定义构造函数中设置相同的条件?
When I make conditions within properties, Do I have to make same conditions in Custom constructor again?
我的完整问题是:
当我在属性中设置条件时,我是否必须再次在自定义构造函数中设置相同的条件,或者我可以以某种方式使用 属性 自定义构造函数?
如果我有这样的代码:
class Program
{
struct Student
{
private int _id;
private string _name;
private int _age;
public int ID // Property
{
get
{
return _id;
}
set
{
if (value <= 0)
{
Console.WriteLine("You cannot assign id less then 1");
Console.ReadLine();
Environment.Exit(0);
}
else
{
_id = value;
}
}
}
public string NAME // Property
{
get
{
return _name;
}
set
{
if (String.IsNullOrEmpty(value))
{
_name = "No Name";
}
else
{
_name = value;
}
}
}
public int AGE // Property
{
get
{
return _age;
}
set
{
if(value <= 0)
{
Console.WriteLine("Your age cannot be less then 1 year.");
Console.ReadLine();
Environment.Exit(0);
}
else
{
_age = value;
}
}
}
public Student(int initID, string initName, int initAge) // Defining custom constructor
{
if (initID <= 0)
{
Console.WriteLine("You cannot assign id less then 1");
Console.ReadLine();
Environment.Exit(0);
}
if (String.IsNullOrEmpty(initName))
{
_name = "No Name";
}
if (initAge <= 0)
{
Console.WriteLine("Your age cannot be less then 1 year.");
Console.ReadLine();
Environment.Exit(0);
}
_id = initID;
_name = initName;
_age = initAge;
}
public void Status() // struct member - method
{
Console.WriteLine("ID: {0}", _id);
Console.WriteLine($"Name: {_name}");
Console.WriteLine($"Age: {_age}\n");
}
}
static void Main(string[] args)
{
Student s1 = new Student(1, "James", 10);
s1.Status();
Console.ReadLine();
}
}
如你所见,我在属性内设置了一些条件,比如ID不能为0且小于0,但是当我想使用自定义构造函数时,我必须再次设置这个条件。这是唯一的方法如何做到这一点?或者是另一种方式?
甚至自定义构造函数是否与封装一起使用以及何时具有属性?
感谢您的回答。 :)
首先:使用 class
而不是 struct
只要你没有很好的理由使用 struct
.
这是使用方法而不是 public 属性的一个很好的例子。
仅包含年龄的简短示例以说明我在说什么。
public class Student
{
private int _age = 1;
public Student(int initAge)
{
SetAge(initAge);
}
public void SetAge(int age)
{
if (age <= 0)
{
Console.WriteLine("Your age cannot be less then 1 year.");
Console.ReadLine();
Environment.Exit(0);
}
else
{
_age = age;
}
}
public int GetAge()
{
return _age;
}
}
当您必须在分配值之前检查内容时,我建议使用方法来设置和获取值。干净多了。
您可以像这样扩展您的设置方法
public bool TrySetAge(int age)
和 return 对或错。当可以设置值时为真,否则为假。
您不需要重新发明完整的逻辑。您可以在构造函数中调用属性 setter 并依赖其验证逻辑:
public Student(int initID, string initName, int initAge) // Defining custom constructor
{
this.ID = unitID;
this.NAME = initName;
this.AGE = initAge;
}
现在,当任何参数错误时,属性 setter 会报错,无需在构造函数中再次检查。
顺便说一句,我不会因为错误的参数而退出应用程序。但是,您可以抛出一个 ArgumentException
并在调用代码中捕获它,或者使用来自@Mighty Badaboom 的方法和 TryParse
-模式。
除非您在构造函数中的条件不同,否则没有理由重复 setter 代码。
public Student(int initID, string initName, int initAge) // Defining custom constructor
{
ID = initID;
NAME = initName;
AGE = initAge;
}
或者您可以删除构造函数并改用 public 属性的标准语法。
var student = new Student
{
ID = id,
NAME = name,
AGE = age
};
我的完整问题是:
当我在属性中设置条件时,我是否必须再次在自定义构造函数中设置相同的条件,或者我可以以某种方式使用 属性 自定义构造函数?
如果我有这样的代码:
class Program
{
struct Student
{
private int _id;
private string _name;
private int _age;
public int ID // Property
{
get
{
return _id;
}
set
{
if (value <= 0)
{
Console.WriteLine("You cannot assign id less then 1");
Console.ReadLine();
Environment.Exit(0);
}
else
{
_id = value;
}
}
}
public string NAME // Property
{
get
{
return _name;
}
set
{
if (String.IsNullOrEmpty(value))
{
_name = "No Name";
}
else
{
_name = value;
}
}
}
public int AGE // Property
{
get
{
return _age;
}
set
{
if(value <= 0)
{
Console.WriteLine("Your age cannot be less then 1 year.");
Console.ReadLine();
Environment.Exit(0);
}
else
{
_age = value;
}
}
}
public Student(int initID, string initName, int initAge) // Defining custom constructor
{
if (initID <= 0)
{
Console.WriteLine("You cannot assign id less then 1");
Console.ReadLine();
Environment.Exit(0);
}
if (String.IsNullOrEmpty(initName))
{
_name = "No Name";
}
if (initAge <= 0)
{
Console.WriteLine("Your age cannot be less then 1 year.");
Console.ReadLine();
Environment.Exit(0);
}
_id = initID;
_name = initName;
_age = initAge;
}
public void Status() // struct member - method
{
Console.WriteLine("ID: {0}", _id);
Console.WriteLine($"Name: {_name}");
Console.WriteLine($"Age: {_age}\n");
}
}
static void Main(string[] args)
{
Student s1 = new Student(1, "James", 10);
s1.Status();
Console.ReadLine();
}
}
如你所见,我在属性内设置了一些条件,比如ID不能为0且小于0,但是当我想使用自定义构造函数时,我必须再次设置这个条件。这是唯一的方法如何做到这一点?或者是另一种方式?
甚至自定义构造函数是否与封装一起使用以及何时具有属性?
感谢您的回答。 :)
首先:使用 class
而不是 struct
只要你没有很好的理由使用 struct
.
这是使用方法而不是 public 属性的一个很好的例子。
仅包含年龄的简短示例以说明我在说什么。
public class Student
{
private int _age = 1;
public Student(int initAge)
{
SetAge(initAge);
}
public void SetAge(int age)
{
if (age <= 0)
{
Console.WriteLine("Your age cannot be less then 1 year.");
Console.ReadLine();
Environment.Exit(0);
}
else
{
_age = age;
}
}
public int GetAge()
{
return _age;
}
}
当您必须在分配值之前检查内容时,我建议使用方法来设置和获取值。干净多了。
您可以像这样扩展您的设置方法
public bool TrySetAge(int age)
和 return 对或错。当可以设置值时为真,否则为假。
您不需要重新发明完整的逻辑。您可以在构造函数中调用属性 setter 并依赖其验证逻辑:
public Student(int initID, string initName, int initAge) // Defining custom constructor
{
this.ID = unitID;
this.NAME = initName;
this.AGE = initAge;
}
现在,当任何参数错误时,属性 setter 会报错,无需在构造函数中再次检查。
顺便说一句,我不会因为错误的参数而退出应用程序。但是,您可以抛出一个 ArgumentException
并在调用代码中捕获它,或者使用来自@Mighty Badaboom 的方法和 TryParse
-模式。
除非您在构造函数中的条件不同,否则没有理由重复 setter 代码。
public Student(int initID, string initName, int initAge) // Defining custom constructor
{
ID = initID;
NAME = initName;
AGE = initAge;
}
或者您可以删除构造函数并改用 public 属性的标准语法。
var student = new Student
{
ID = id,
NAME = name,
AGE = age
};