继承或枚举 [英] Inheritance or enum

查看:86
本文介绍了继承或枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须为应用程序构建新模型,但我没有更好的方法:

I must structure new model for application and I don't what is better:

使用继承或将枚举用作对象类型:

using inheritance or using enum as type of object:

例如:

书籍

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
{

}

或更好地使用:

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,
...
}

推荐答案

如果不同类型具有显着差异(如何处理它们和如何对待它们),请使用继承.也就是说,如果您将要完全使用多态,则应该使用继承.

Use inheritance if the different types have significant differences (how you process them and treat them). That is, if you are going to use polymorphism at all, you should use inheritance.

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

If you only need a way to distinguish different types of books, go with the Enum.

这篇关于继承或枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆