我想在我的班级名称中更具描述性。我有三个相同的类,如下所示:
public class Dog
{
public string name;
public int id;
public Dog(){}
public Dog(DataRow dr)
{
name = dr["Name"];
id = dr["Id"];
}
}
public class Cat
{
public string name;
public int id;
public Cat(){}
public Cat(DataRow dr)
{
name = dr["Name"];
id = dr["Id"];
}
}
public class Horse
{
public string name;
public int horseId;
public Horse(){}
public Horse(DataRow dr)
{
name = dr["Name"];
horseId= dr["horseId"];
}
}
如何通过使用基类来避免代码重复?什么是最佳实践?请注意,马有一个名为 HorseId 而不是 Id 的属性。谢谢。