对象的继承和集合
我有一个基本问题。
假设我们有 3 个类:
Class S, class A, class B
A and B inherits S.
A 有属性 A1,B 有属性 B1。
我们还有一个包含 A 和 B 对象的对象集合。
ex.
Dim c as Collection = new Collection
c.add(new A)
c.add(new B)
现在我们想要创建一个将从集合中读取的通用对象。
ex .
Dim obj as S
我们如何根据类来转换 obj 以便查看属性 A1 或 B1 ;
i have a basic question.
lets say we have 3 classes:
Class S, class A, class B
A and B inherits S.
A has a property A1 and B has a property B1.
we also have a collection of objects that has A and B objects.
ex.
Dim c as Collection = new Collection
c.add(new A)
c.add(new B)
Now we want to make a general object that will read from the collection.
ex .
Dim obj as S
how can we cast obj in order to see properties A1 or B1 according to the class;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 TypeOf 来测试转换前的对象类型,尽管 TryCast 可能更好取决于你想做什么。另请查看 DirectCast。
You can use TypeOf to test the object type before casting although TryCast might be better depending on what you want to do. Also take a look at DirectCast.