实现接口? [英] implementing interface??

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

问题描述

class Program
    {
        interface math
        {
            void odd();
            void even();
        }
        class test : math
        {
            void math.odd()
            {
Console.WriteLine("number is odd");
            }
            void math.even()
            {
Console.WriteLine("number is odd");
            }
         public void sort(math ob)
            {
                Console.WriteLine("enter number");
                int n = Convert.ToInt32(Console.ReadLine());
                if( n%2==0)
                Console.WriteLine("number is even");
                else
                Console.WriteLine("number is odd");
            }
        }
        static void Main(string[] args)
        {
            math ob = new test();
            test ob1=new test();
            ob1.sort(ob);

            Console.ReadKey();
        }
    }


我只是用一个偶数函数的简单示例来检查接口的实现.这种实现接口的方法正确吗?因为在接口中声明的方法没有任何实现,而没有接口就可以完成?


i have just used a simple example of even odd function to check interface implementation.Is this way of implementing the interface correct?? as the methods declared in interface didnt have any implementation could this have been done without the interface??

推荐答案

<< this =" way =" of =" Implementing =" the =" interface ="正确吗?

是的,=" is ="正确.=" such =" Implementation ="意味着=" that =" method =" are =" private =" and =" you =" can''t =" get =" access =" out =" class,=" only =" throught =" interface.=" for ="示例:

< pre =">
测试t =新的test();
test.odd(); //-编译错误

(以数学方式测试).odd(); //确定
<<is this="" way="" of="" implementing="" the="" interface="" correct??

yes,="" is="" correct.="" such="" implementation="" means="" that="" method="" are="" private="" and="" you="" can''t="" get="" access="" out="" class,="" only="" throught="" interface.="" for="" example:

<pre="">
test t = new test();
test.odd(); //-compilation error

(test as math).odd(); //OK


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

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