+ 2
Nested classes
What is the difference between nested classes and inherited classes? when use one and when the other?
5 Réponses
+ 5
A type defined within a class or struct is called a nested type.
For example:
class Container
{
class Nested
{ Nested()
{
}
}
}
Regardless of whether the outer type is a class or a struct, nested class types default is private when left undeclared, but can be set to be made public, protected or private.
(Basically all the public-private types, etc. But if you don't state that it is public/protected, it automatically becomes private.)
For inheritance, the class whose members are inherited is called the base class, and the class that inherits those members is called the derived class.(Well not quite sure how to explain it simplier, go re-check back on the lessons)
Basically the difference :
- Nested class need not be be 'similar' to the outer class unlike inherited class
- inherited class may not need to be inside a class to be consider a inherited class.
+ 5
Maybe during a real world context you are creating a weapon for a game. You will have to set the weapon base damage and its random number generator for that, etc. That is where I will use nested class
+ 2
class Person {
class Hand {
public void Shake() {
Console.WriteLine("Hi"); }
} }
0
thanks, can you help me, for example when you choose to use nested class in a program, and not inheritence?
0
oke, now i get it i think, thank you.