gpt4 book ai didi

c# - 继承或枚举

转载 作者:太空狗 更新时间:2023-10-29 19:50:10 24 4
gpt4 key购买 nike

我必须为应用程序构建新模型,但我不知道什么更好:

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

例如:

书籍

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

最佳答案

如果不同的类型有显着差异(处理和对待它们的方式),则使用继承。也就是说,如果您要使用多态性,就应该使用继承。

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

关于c# - 继承或枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3108081/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com