低落和昂扬
我是C#(和OOP)的新手。当我有如下代码时:
class Employee
{
// some code
}
class Manager : Employee
{
//some code
}
问题 1:如果我有其他代码执行此操作:
Manager mgr = new Manager();
Employee emp = (Employee)mgr;
这里 Employee
是 Manager
,但是当我将它像那样转换为 Employee
这意味着我正在向上转换它?
问题2:
当我有多个Employee
类对象并且其中一些但不是全部是Manager
的时,我如何在可能的情况下向下转换它们?
I am new to C# (and OOP). When I have some code like the following:
class Employee
{
// some code
}
class Manager : Employee
{
//some code
}
Question 1: If I have other code that does this:
Manager mgr = new Manager();
Employee emp = (Employee)mgr;
Here Employee
is a Manager
, but when I cast it like that to an Employee
it means I am upcasting it?
Question 2:
When I have several Employee
class objects and some but not all of them are Manager
's, how can I downcast them where possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
向上转换(使用
(Employee)someInstance
)通常很容易,因为编译器可以在编译时告诉您某种类型是否派生自另一种类型。然而,向下转型通常必须在运行时完成,因为编译器可能并不总是知道所讨论的实例是否属于给定的类型。 C# 为此提供了两个运算符 - is,它告诉您向下转型是否有效,并返回 true/false。 as 尝试进行转换并返回正确的类型(如果可能),否则返回 null。
要测试员工是否是经理:
您也可以使用此
Upcasting (using
(Employee)someInstance
) is generally easy as the compiler can tell you at compile time if a type is derived from another.Downcasting however has to be done at run time generally as the compiler may not always know whether the instance in question is of the type given. C# provides two operators for this - is which tells you if the downcast works, and return true/false. And as which attempts to do the cast and returns the correct type if possible, or null if not.
To test if an employee is a manager:
You can also use this
在您的情况下,
您正在进行向上转换。
向上转换总是成功,与需要显式转换的向下转换不同,因为它可能在运行时失败。(InvalidCastException)。
C# 提供了两个运算符来避免抛出此异常:
起始位置:
First:
Second:
警告:当您进行向上转换时,您只能访问超类的方法、属性等...
In your case
you are doing an upcasting.
An upcast always succeeds unlike a downcast that requires an explicit cast because it can potentially fail at runtime.(InvalidCastException).
C# offers two operators to avoid this exception to be thrown:
Starting from:
First:
Second:
Warning: When you do an upcast you can only access to the superclass' methods, properties etc...
如果您需要检查每个 Employee 对象是否是 Manager 对象,请使用 OfType 方法:
In case you need to check each of the Employee object whether it is a Manager object, use the OfType method:
答案1:
是的,这称为向上转换,但你这样做的方式不是现代方式。向上转换可以隐式执行,不需要任何转换。所以只需写 Employee emp = mgr;
足以进行升级。
答案2:
如果您创建 Manager 类的对象,我们可以说经理是一名员工。因为类Manager : Employee描述了Employee类和Manager类之间的Is-A关系。所以我们可以说每一位管理者都是员工。
但是,如果我们创建 Employee 类的对象,我们不能说该员工是经理,因为 class Employee 是一个不继承任何其他类的类。因此,您不能直接将该 Employee 类对象向下转换为 Manager 类对象。
所以答案是,如果你想从 Employee 类对象向下转换为 Manager 类对象,首先你必须先有 Manager 类的对象,然后你可以向上转换它,然后你可以向下转换它。
Answer 1 :
Yes it called upcasting but the way you do it is not modern way. Upcasting can be performed implicitly you don't need any conversion. So just writing Employee emp = mgr;
is enough for upcasting.
Answer 2 :
If you create object of Manager class we can say that manager is an employee. Because class Manager : Employee depicts Is-A relationship between Employee Class and Manager Class. So we can say that every manager is an employee.
But if we create object of Employee class we can not say that this employee is manager because class Employee is a class which is not inheriting any other class. So you can not directly downcast that Employee Class object to Manager Class object.
So answer is, if you want to downcast from Employee Class object to Manager Class object, first you must have object of Manager Class first then you can upcast it and then you can downcast it.
向上转型和向下转型:
向上转型:从派生类到基类的转换
向下转型:从基类到派生类的转换
让我们通过一个例子来理解:
考虑两个类 Shape 作为我的父类,Circle 作为派生类,定义如下:
向上转型:
Shape s = new Shape ();
圆c=s;
c 和 s 都引用相同的内存位置,但它们都有不同的视图,即使用“c”引用您也可以访问基类和派生类的所有属性,但使用“s”引用您可以访问属性唯一的父类。
向上转换的一个实际例子是 Stream 类,它是 .net 框架所有类型的流读取器的基类:
StreamReader reader = new StreamReader(new FileStreamReader());
在这里,FileStreamReader() 被向上转换为流渲染器。
向下转型:
Shape s = new Circle();
如上所述,s 的视图是唯一的父级,为了使其同时适用于父级和子级,我们需要将其向下转换
var c = (Circle) s;
Downcasting的实际例子是WPF的按钮类。
Upcasting and Downcasting:
Upcasting: Casting from Derived-Class to Base Class
Downcasting: Casting from Base Class to Derived Class
Let's understand the same as an example:
Consider two classes Shape as My parent class and Circle as a Derived class, defined as follows:
Upcasting:
Shape s = new Shape();
Circle c= s;
Both c and s are referencing to the same memory location, but both of them have different views i.e using "c" reference you can access all the properties of the base class and derived class as well but using "s" reference you can access properties of the only parent class.
A practical example of upcasting is Stream class which is baseclass of all types of stream reader of .net framework:
StreamReader reader = new StreamReader(new FileStreamReader());
here, FileStreamReader() is upcasted to streadm reder.
Downcasting:
Shape s = new Circle();
here as explained above, view of s is the only parent, in order to make it for both parent and a child we need to downcast it
var c = (Circle) s;
The practical example of Downcasting is button class of WPF.
这是正确的。当您这样做时,您将其转换为
employee
对象,这意味着您无法访问任何特定于经理的内容。向下转型是指您采用基类,然后尝试将其转换为更具体的类。这可以通过使用 is 和显式强制转换来完成,如下所示:
或者使用像这样的
as
运算符:如果有任何不清楚的地方,我很乐意纠正它!
That is correct. When you do that you are casting it it into an
employee
object, so that means you cannot access anything manager specific.Downcasting is where you take a base class and then try and turn it into a more specific class. This can be accomplished with using is and an explicit cast like this:
or with the
as
operator like this:If anything is unclear I'll be happy to correct it!