我应该在哪里检查输入数据?
Where am I supposed to check on input data?
我开始考虑什么时候检查输入数据。我在这种情况下:
class Father
{
string name;
public Father(string n)
{
name = n;
}
}
class Child extends Father
{
private Child(string n)
{
base(n);
}
public NewChild(string n)
{
return new Child(n);
}
}
假设我想检查来自外部的字符串是否不为空且至少有 5 个字符长。
我应该在哪里检查?
Father
class 应该在构造函数中将其作为先决条件进行检查,如果输入无效则抛出异常。
我开始考虑什么时候检查输入数据。我在这种情况下:
class Father
{
string name;
public Father(string n)
{
name = n;
}
}
class Child extends Father
{
private Child(string n)
{
base(n);
}
public NewChild(string n)
{
return new Child(n);
}
}
假设我想检查来自外部的字符串是否不为空且至少有 5 个字符长。 我应该在哪里检查?
Father
class 应该在构造函数中将其作为先决条件进行检查,如果输入无效则抛出异常。