问题
I have a question about casting between a base class and its child classes:
(1) Why is this allowed?
BaseClass b = new ChildClassA();
ChildClassA c = (ChildClassA)b
(2) Why is this not allowed?
ChildClassA c = (ChildClassA)new BaseClass();
BaseClass b = (BaseClass)c;
(3) Why is this allowed?
BaseClass b = new BaseClass();
ChildClassA c = (ChildClassA)c;
(4) Why is this allowed?
ChildClassA c = new ChildClassA();
BaseClass b = (BaseClass)c;
回答1:
The reasons the cast is either allowed or not allowed is the basics behind inheritance.
A child class (or a derived class) is always a base class, but the opposite is not true.
To explain, let's use some more real world names for your example classes:
class Animal
{
}
class Dog : Animal
{
}
class Cat : Animal
{
}
So for your example (1):
Animal b = new Dog();
Dog c = (Dog)b
This is true because all Dogs are Animals and your Animal b is actually a dog so the cast is successful.
For your example (2):
Dog c = (Dog)new Animal();
Animal b = (Animal)c;
This is impossible because you are assigning an Animal object to a Dog, but you know that not all animals are Dogs, some animals are cats.
And for examples (3) & (4):
Dog c = new Dog();
Animal b = (Animal)c;
This is the same as your example 1 above. All dogs are animals, so any dog can be classified as an animal and cast (in fact you don't need the cast, there would be an implicit cast and you can write it as Animal b = c;
回答2:
(1) Why is this allowed? BaseClass b = new ChildClassA(); ChildClassA c = (ChildClassA)b
Because ChildClassA IS a BaseClass.
(2) Why is this not allowed? ChildClassA c = (ChildClassA)new BaseClass(); BaseClass b = (BaseClass)c;
Because a BaseClass IS NOT a ChildClassA
(3) Why is this allowed? ChildClassA c = new ChildClassA(); BaseClass b = (BaseClass)c;
Because ChildClassA IS a BaseClass.
(4) Why is this allowed? ChildClassA c = new ChildClassA(); BaseClass b = (BaseClass)c;
Same as question 3.
When you create an inherited class, the class can be casted to an instance of the base class because it is a child of that class. However, when you try to create a child class with a base class object, the base class does not have all of the information of the child class.
The child knows what about its parent, but the parent does not know what the child has.
回答3:
Think in terms of "is a". We can say any instance of ChildClassA
"is a" BaseClass
where class ChildClassA : BaseClass
. But it is not true for vice versa. We can not say any instance of BaseClass
"is a" ChildClassA
in this case
Now try reading your lines of codes as follows. I believe it will make more sense this time.
new ChildClassA()
is an instance of ChildClassA
then it also "is a" BaseClass
. So we can assign new ChildClassA()
to b
which is of type BaseClass
We can cast b
to ChildClassA
because it actually (points) a instance of ChildClassA
.
We can not cast new BaseClass()
to ChildClassA
because instance of BaseClass
"is not a" ChildClassA
回答4:
Well...
(2) is not allowed, because you cannot cast a BaseClass
to a ChildClassA
- an instance of ChildClassA
is by inheritance also a BaseClass
but not the other way around!
回答5:
- is allowed because b is indeed a ChildClassA
- is not allowed because you can't cast a baseclass to a childClass since baseClass don't contain the definition needed by childClass to work
- is allowed because ChildClass contain all the definition for BaseClass and a ChildClass IS A BaseClass 4.same as 3?
回答6:
Generally speaking, it's because ChildClassA might have defined things that BaseClass doesn't know about, whereas, because ChildClassA derives from BaseClass, it knows everything about it.
来源:https://stackoverflow.com/questions/16858335/casting-between-base-class-and-child-classes