继承或枚举

use*_*618 7 c# inheritance modeling

我必须为应用程序构建新模型,而不是更好的方法:

使用继承或使用enum作为对象类型:

例如 :

图书

class Book
{
public string Name {get;set;}

public string Author {get;set;}

public int NumberOfPages {get;set;}

}

public class Encyclopedie:Book
{

}

public class Novel:Book
{

}
Run Code Online (Sandbox Code Playgroud)

或更好地使用:

class Book
{

public BookType Type {get;set;}

public string Name {get;set;}

public string Author {get;set;}

public int NumberOfPages {get;set;}

}

public enum BookType
{
Encyclopedie = 0,
Novel = 1,
...
}
Run Code Online (Sandbox Code Playgroud)

Ode*_*ded 22

如果不同类型具有显着差异(如何处理它们并对其进行处理),请使用继承.也就是说,如果你要使用多态,你应该使用继承.

如果您只需要一种方法来区分不同类型的书籍,请使用Enum.