对接口实现的疑问
首先,这只是一个面向对象编程问题,并不适用于任何特定语言。
这对我来说是相当尴尬的事情。这件事发生在工作中,我太害羞了,无法与同事澄清这一点,因为这表明我对面向对象编程的理解很差。事件如下:
有一个类 A 实现了接口 I。该接口有一个方法 M。类 A 定义了该方法的主体,或者更确切地说是实现。 现在我有了一个工具,可以用它找出其他类或程序使用哪个类或程序,或者换句话说,调用 A 类 ->方法M。当我使用这个工具时,它没有返回任何结果;我认为没有类的一个迹象是调用方法 M。
当我与一位资深同事分享我的观察结果时,他只是转到了界面 I -> 。方法M,并使用了该工具。它返回了相当多的结果。他告诉我这些是调用方法 M 的类或方法,并要求我继续。
这个结果显然是实现接口 I 的类的列表。 但我不明白的是,这是如何调用类 A -> 的类或方法的集合?因为我已经自信地告诉同事没有类调用方法M,所以当他给我看结果列表后,我不好意思问他怎么就是我要找的结果。
知道我的同事在暗示什么吗?
问候, 米敦
Firstly, this is just an Object Oriented Programming question and does not apply to any Language in particular.
This is quite embarassing for me. This incident happened @ work and I was too shy to clarify this with my colleagues as it would indicate a poor understanding of Object Oriented Programming on my part. So here is the incident :
There is a class A which implements interface I. This interface has a method M. Class A has defined the body or rather implementation of this method.
Now I have a tool with which I can find out which other class or program uses, or in other words calls Class A -> method M.When I used this tool, it did not return any results; An indication which I took as no class is calling method M.
When I shared my observations with a senior colleague, he simply went to the interface I -> method M, and used the tool on this. It returned quite a few results. He told me that these are the classes or methods which call method M and asked me to proceed.
This result is obviously a list of those classes which implement interface I.
But what I didn't understand is that how is this the set of classes or methods which call Class A -> method M. Since I had confidently told my colleague that no class calls method M, after he showed me the result list, I was too embarassed to ask him how is that the result that I am looking for.
Any idea what my colleague is hinting at?
Regards,
Mithun
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为类提供接口的整个想法是使用接口来传递实例。这是为了提供松耦合。
现在,由于您实际上并没有传递类,而是传递特定于该类的接口,
:
例如
当您尝试在
ISomeClass / SomeClass
中调用someMethod
中名为myMethod
的方法时,它实际上是因此,所有用法
myMethod
的引用回ISomeClass
而不是真正的SomeClass
编辑:也许只是我,但我始终相信,当你不理解某些东西时,张开嘴问问题却被误认为傻瓜,比闭嘴证明自己是傻瓜要好!
The whole idea behind having interfaces for the classes is so that the interfaces will be used to pass the instances. This is to provide loose coupling.
Now, since you do not actually pass the class around but the interface specific to that class,
eg :
instead of
so when you are trying to call a method within
ISomeClass / SomeClass
calledmyMethod
insomeMethod
, it is actuallyHence, all usages of
myMethod
refer back toISomeClass
and not really toSomeClass
Edit: Maybe its just me, but I always believe that when you dont understand something, it is better to open your mouth to ask a question and be mistaken for a fool than to keep it shut and prove yourself to be one!
我认为你应该问问你的人发生了什么事。你不应该害怕承认你不知道;如果你从中学习,不知道并没有什么错。我一直在犯错,如果我能迈出下一步并学到一些东西,我一点也不为此感到尴尬。
我认为您所看到的是没有任何东西使用 A 类 -> M,但 I 的其他实现也使用 M。该工具发现其他实现正在使用 M。
I think you should ask your guy what is going on. You shouldn't be afraid to admit you don't know; there is nothing wrong with not knowing, if you learn from it. I am wrong all the time and am not in the least embarrassed by it, if I can get to the next step and learn something.
I think what you are seeing is that nothing is using Class A -> M, but that there are other implementations of I which use M. The tool found where M is being used by other implementations.
没有结果意味着吗?
结果真的是这个意思吗?
这个工具如何工作?结果意味着什么?不确定是你不懂OOP,还是只是这个神奇的工具?
Does no results mean that?
Is that really what the results mean?
How does this tool work? What do the results mean? Not sure if it's OOP you don't understand, or just this magic tool?