Inheritance or listing

I have to create a new model for the application, and I am not doing the best:

using inheritance or using enumeration as an object type:

For instance:

Books

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
{

}

or better to use:

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,
...
}
+5
source share
8 answers

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

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

+21
source

- . , , , , , .

, , . , .

-, , , . , - Encyclopaedia Novel , - -, , , .

, - , - , {'children', 'ornithological', 'encyclopaedia',} - - " ", , , .

+3

, , , , .

+2

"" /. ? ?

, , - , Encyclopedie , , Encyclopedie , .

+2

. , . , .

0

, . ,

, . .

0

, , , "" . - ( ....), . - , , , ( "" ).

0

, "" , " C".

, , ( #), , , . "", , / : , " " , BookType ? ( / )

, bounch "if-then-else", .

Bye!

-1

All Articles