与接口有关的C sharp oop概念 [英] C sharp oop concepts related to Interface

查看:55
本文介绍了与接口有关的C sharp oop概念的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个班级



I have Three class

Public class class1:IClass
{
   //implementation of interface
}

Public class class2:IClass
{
   //implementation of interface
}
Public class class3:IClass
{
 
   //implementation of interface

    public void fun1()
    {

    }
     
}
interface IClass
{
 string fun();

}





从上面的场景我可以这样写。





From the above scenario can i write like this.

IClass obj =new Class3();
obj.fun1();

推荐答案

不,你不能,

理由:

当你创建任何类型的引用时(这里是IClass接口),这意味着你只能调用你的类型中存在的成员(IClass接口)。

这里,你没有在你的界面中声明fun1()方法,所以它不会被intellisense使用,它也会给你编译时错误。

如解决方案1中所述,你将不得不用fun1()取代乐趣,否则你必须在界面中添加fun1()才能调用它。



谢谢。 />


Kishor
No you can''t,
Reason:
When you create a reference of any type ( here it is IClass interface), this means you can only invoke members that are present in your type (IClass interface).
Here, you do not have fun1() method declared in your interface, so it will not be available by intellisense, also It''ll give you compile time error.
As mentioned in Solution 1, you will have to replace fun with fun1() or you''ll have to add fun1() in your interface in order to invoke it.

Thanks.

Kishor


是的,当然,如果你替换接口方法的名称 fun fun1 。我希望这只是一种类型。

顺便说一句,你命名是如此糟糕,以至于我的眼睛受伤了。看看微软的命名惯例,它们非常好。

我也想注意这个问题及其任何答案都不会帮助你学到很多东西,缺少事物的本质。你最好阅读大量关于OOP的文献并做练习。



-SA
Yes, of course, if you replace the name of interface method fun to fun1. I hope this is just a type.
By the way, you naming is so bad that my eyes hurt. Look at Microsoft naming conventions, they a pretty good.
I also want to note that this question and any answers to it will hardly help you to learn much, missing essence of things. You better read a good deal of literature on OOP and do exercises.

—SA


这篇关于与接口有关的C sharp oop概念的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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