继承问题 [英] Inheritance question

查看:97
本文介绍了继承问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我是C#新手。我想创建一个条形码阅读器引擎库,其基类支持基本功能,但有许多品牌的条形码,并且有一些品牌具有特定的功能。我不知道哪个品牌运行该程序。如果没有尴尬,我如何调用该品牌的特定功能?


我是否必须检查其类型然后进行投射,如下例所示?


提前致谢!

Hello,

I am a C# newbie. I want to create a barcode reader engine library, with base class that supports basic functions, but there are many brands of barcode and there are some brands that have specific functions. I don''t know ahead of time which brand runs the program. How do I call a specific function of that brand without it getting awkward?

Do I have to check its type and then cast it such as the example below?

Thanks in advance!

展开 | 选择 | Wrap | 行号

推荐答案

据我所知,如果你正在处理基类型的对象,您只能访问为该类型定义的内容。任何继承基础的对象都可以有额外的东西,但是如果你正在查看基类型,你就会坚持下去。


所以在你的情况下,只有奶牛可以mo,但你不必检查类型(本身)。这是一个迂回的例子,但希望它有意义......

As I understand it, if you''re dealing with an object of the base type, you can only access things defined for that type. Any object that inherits the base can have extra stuff on it, but if you''re looking at the base type, you''re stuck with it.

So in your case, only cows can moo, but you don''t have to check the type (per se). It''s a roundabout example, but hopefully it makes sense...

展开 | 选择 | Wrap < span class =codeDivider> | 行号


嗨GaryTexmo,


感谢您的回复。我知道当我们有两个具有相同功能的不同对象时,可以使用该接口。但是,我只是想知道如何解决一个动物与其他动物相比具有额外功能的问题?例如,所有条形码都可以执行Scan(),但品牌ABC和XYZ的条形码可以执行MoreThanScan()?如果我有一个名为''Run MoreThanScan'的按钮的表单,但该表只有一个BarCode类型的对象(它是父类),我如何调用MoreThanScan()?我是否必须首先检查_myBarCode'的类型是ABC还是XYZ?


对不起,如果这听起来有点混乱! :)
Hi GaryTexmo,

Thanks for your reply. I understand that interface can be used when we have two different objects with the same functionalities. However, I am just wondering how to tackle when one Animal has an extra functionality compared to other animal? For example, all barcodes can do Scan(), but barcode with brand ABC and XYZ can do MoreThanScan()? If I have a Form with a button called ''Run MoreThanScan'' but that Form only has an object of type BarCode (which is the parent class), how do I call MoreThanScan()? Do I have to check if _myBarCode''s type is either ABC or XYZ first?

Sorry if this sounds confusing! :)


如我帖子的第一部分所述,是的,你必须检查类型或演员表。抱歉...除非有另一种方式(可能存在,但在这种情况下似乎不太可能),您只能使用属于或继承自当前类型对象的方法。 />

我知道,这一切都让人感到困惑,希望我所说的有意义;)
As mentioned in the first section of my post, yes you''ll have to do a check on the type, or a cast. Sorry... unless there''s another way (which there could be, but it seems unlikely in this case), you can only methods that belong to, or are inherited from, the object of the type you currently have.

And I know, this does all get confusing, hopefully what I said there makes sense ;)


这篇关于继承问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆