SubClass和SuperClass [英] SubClass And SuperClass
问题描述
我有以下课程
班级汽车
{
//一些代码
}
班奔驰:汽车
{
//一段代码
}
级保时捷:汽车
{
//一些代码
}
公共类汽车:列表<汽车>
{
//一段代码
}
现在在班级的对象汽车我可以添加班级的对象奔驰
和保时捷......但是当访问中的对象时列表怎么可以确定他们是属于奔驰还是保时捷......
请帮帮我
谢谢你提前
Rajkiran
RajkiranPro写道:
我有以下课程
班级汽车
{
//某段代码>
}
班奔驰:汽车
{
//一些代码
}
级保时捷:汽车
{
//一些代码
}
公共类汽车:列表<汽车>
{
//一些代码
}
现在在班级的对象汽车我可以添加班级的对象奔驰
和保时捷......但是当访问列表中的对象时怎么可以
i确定他们是属于奔驰还是保时捷..
多态性的一点是你不应该!
通过声明一个List< Caryou表达意图在列表中存储所有种类
的汽车'但仅使用Car的方法 - 而不是任何
子类的具体方法。坚持下去。
你*可以*测试:
如果(o是奔驰)
但不要!
Arne
7月6日,6:55 * am,Arne Vajh?j< a ... @ vajhoej.dkwrote:
RajkiranPro写道:
我有以下课程
class car
{
* //某段代码
}
class Benz:Car
{
//某件代码
}
class保时捷:汽车
{
//一些代码
}
公共类汽车:列表< Car>
{
//一些代码
}
现在在类的对象中我可以添加类的对象奔驰
和保时捷......但是当访问列表中的对象时怎么可以
i确定他们是属于奔驰还是保时捷..
多态性的一点是你不应该!
通过声明一个List< Caryou表达了在列表中存储所有种类
汽车的意图但仅使用Car的方法 - 而不是任何具体的
子类的方法。坚持下去。
你*可以*测试:
* *如果(o是奔驰)
但不要' 't!
Arne
感谢您的回复..
这段代码是正确的
Car obj = carlist [i];
// carlist是一个汽车对象,它已经初始化
奔驰b;
保时捷p;
如果(obj是奔驰)
{
b = obj;
}
else
{
p = obj;
}
7月6日,6:55 * am,Arne Vajh?j< a ... @ vajhoej.dkwrote:
< blockquote class =post_quotes>
RajkiranPro写道:
我有以下类
类汽车
{
* //一些代码
}
< blockquote class =post_quotes>
class Benz:Car
{
//一些代码
}
class保时捷:Car
{
//一些代码
}
公共类汽车:列表< Car>
{
//一些代码
}
< blockquote class =post_quotes>
现在在类的对象中我可以添加类的对象奔驰
和保时捷......但是当访问列表中的对象时怎么能
i确定他们是属于奔驰还是保时捷..
多态性的一点是你不应该这样做!
>
通过声明一个List< Caryou表达了一种意图来存储各种类型的汽车'但是只能使用汽车的方法 - 而不是任何
子类的具体方法。坚持下去。
你*可以*测试:
* *如果(o是奔驰)
但不要' 't!
Arne
有没有其他方法可以使用
$找到对象的类型b $ b System.Reflection ....若是,如何...
well i have the following classes
class Car
{
//some piece of code
}
class Benz : Car
{
//some piece of code
}
class Porsche : Car
{
//some piece of code
}
public class Cars : List<Car>
{
//some piece of code
}
now in the object of the class Cars i can add objects of classes Benz
And Porsche... However when accessing the objects in the list how can
i identify whether they Belong to Benz or Porsche..
Please Help Me
Thanks In Advance
Rajkiran
RajkiranPro wrote:well i have the following classes
class Car
{
//some piece of code
}
class Benz : Car
{
//some piece of code
}
class Porsche : Car
{
//some piece of code
}
public class Cars : List<Car>
{
//some piece of code
}
now in the object of the class Cars i can add objects of classes Benz
And Porsche... However when accessing the objects in the list how can
i identify whether they Belong to Benz or Porsche..The point of polymorphism is that you should not !
By declaring a List<Caryou express an intent to store all kinds
of Car''s in the List but only to use the methods of Car - not any
specific methods of sub classes. Stick to that.
You *can* test with:
if(o is Benz)
but don''t !
Arne
On Jul 6, 6:55*am, Arne Vajh?j <a...@vajhoej.dkwrote:RajkiranPro wrote:well i have the following classes
class Car
{
*//some piece of code
}
class Benz : Car
{
//some piece of code
}
class Porsche : Car
{
//some piece of code
}
public class Cars : List<Car>
{
//some piece of code
}
now in the object of the class Cars i can add objects of classes Benz
And Porsche... However when accessing the objects in the list how can
i identify whether they Belong to Benz or Porsche..
The point of polymorphism is that you should not !
By declaring a List<Caryou express an intent to store all kinds
of Car''s in the List but only to use the methods of Car - not any
specific methods of sub classes. Stick to that.
You *can* test with:
* * if(o is Benz)
but don''t !
ArneThank you for the reply..
is this piece of code correct
Car obj = carlist[i];
//carlist is an Object Of Cars and it is already initialized
Benz b;
Porsche p;
if(obj is Benz)
{
b = obj;
}
else
{
p = obj;
}
On Jul 6, 6:55*am, Arne Vajh?j <a...@vajhoej.dkwrote:RajkiranPro wrote:well i have the following classes
class Car
{
*//some piece of code
}
class Benz : Car
{
//some piece of code
}
class Porsche : Car
{
//some piece of code
}
public class Cars : List<Car>
{
//some piece of code
}
now in the object of the class Cars i can add objects of classes Benz
And Porsche... However when accessing the objects in the list how can
i identify whether they Belong to Benz or Porsche..
The point of polymorphism is that you should not !
By declaring a List<Caryou express an intent to store all kinds
of Car''s in the List but only to use the methods of Car - not any
specific methods of sub classes. Stick to that.
You *can* test with:
* * if(o is Benz)
but don''t !
ArneIs there any other way to find the type of object using the
System.Reflection.... If yes how...
这篇关于SubClass和SuperClass的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!