Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            MyClass ob = new MyClass();
            bool    result;

            result = ob.IsEven(4);
            if (result)
            {
                Console.WriteLine("4 - четное число.");
            }

            // result = ob.IsOdd(4); // Ошибка, член IsOdd интерфейса IEven недоступен
            // Но следующий код написан верно, поскольку в нем сначала создается
            // интерфейсная ссылка типа IEven на объект класса MyClass, а затем по
            // этой ссылке вызывается метод IsOdd().

            IEven iRef = (IEven)ob;

            result = iRef.IsOdd(3);
            if (result)
            {
                Console.WriteLine("3 — нечетное число.");
            }

            Console.ReadKey();
        }
    static void Main()
    {
        MyClass ob = new MyClass();

        bool result;

        result = ob.IsEven(4);

        if (result)
        {
            Console.WriteLine("4 is even.");
        }

        result = ob.IsOdd(4); // Error, IsOdd not exposed.

        // But, this is OK. It creates an IEven reference to a MyClass object
        // and then calls IsOdd() through that reference.

        IEven iRef = (IEven)ob;

        result = iRef.IsOdd(3);
        if (result)
        {
            Console.WriteLine("3 is odd.");
        }
    }
Ejemplo n.º 3
0
        public void Page_389()
        {
            MyClass2 myClass2 = new MyClass2();

            bool result = myClass2.IsEven(4);

            Console.WriteLine("4 - is even number: " + result);

            IEven iRef = myClass2;

            result = iRef.IsOdd(3);
            Console.WriteLine("3 - is odd number: " + result);
        }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            checkEven myNum = new checkEven();
            bool      result;

            Console.WriteLine("result = myNum.IsOdd(4);");
            result = myNum.IsOdd(4);
            Console.WriteLine(result);
            Console.WriteLine("IEven ob = (IEven)myNum;");
            IEven ob = (IEven)myNum;

            Console.WriteLine("ob.IsEven(4) : {0}, ob.IsOdd(5) {1}", ob.IsEven(4), ob.IsOdd(5));
            Console.ReadKey();
        }
    // Normal implementation.
    public bool IsEven(int x)
    {
        IEven o = this; // Interface reference to the invoking object.

        return(!o.IsOdd(x));
    }
Ejemplo n.º 6
0
    public bool IsOdd(int x)
    {
        IEven ob = this;

        return(!ob.IsEven(x));
    }
    // Обычная реализация.
    public bool isEven(int x)
    {
        IEven o = this; // Ссылка на вызывающий объект.

        return(!o.isOdd(x));
    }
Ejemplo n.º 8
0
        // Обычная реализация,
        public bool IsEven(int x)
        {
            IEven о = this; // Интерфейсная ссылка на вызывающий объект.

            return(!о.IsOdd(x));
        }
Ejemplo n.º 9
0
        // Не явная реализация, создаем ссылку на интерфейс и указываем текущий класс
        // Через данную сслыку мы можем обратиться к любому методу определенному в
        // этом интерфейсе

        public bool IsEven(int x)
        {
            IEven o = this;

            return(!o.IsOdd(x));
        }