对象创建中的混淆

时间:2014-03-15 12:59:24

标签: c# .net class oop interface

我正在搜索接口示例,我发现了一个。下面给出了这个例子......

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace InterFaceDemo
{
    interface IOne
    {
        void ONE();//Pure Abstract Method Signature
    }
    interface ITwo
    {
        void TWO();
    }
    interface IThree:IOne
    {
        void THREE();
    }
    interface IFour
    {
        void FOUR();
    }
    interface IFive:IThree
    {
        void FIVE();
    }
    interface IEVEN:ITwo,IFour
    {

    }
    class ODDEVEN:IEVEN,IFive//Must Implement all the abstract method, in Derived class.
    {
        public void ONE()//Implementation of Abstract Method.
        {
            Console.WriteLine("This is ONE");
        }
        public void TWO()
        {
            Console.WriteLine("This is TWO");
        }
        public void THREE()
        {
            Console.WriteLine("This is THERE");
        }
        public void FOUR()
        {
            Console.WriteLine("This is FOUR");
        }
        public void FIVE()
        {
            Console.WriteLine("This is FIVE");
        }

    }
}

Program.cs的

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace InterFaceDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("This is ODD");
            IFive obj1 = new ODDEVEN();
            obj1.ONE();
            obj1.THREE();
            obj1.FIVE();

            Console.WriteLine("\n\nThis is EVEN");
            IEVEN obj2 = new ODDEVEN();
            obj2.TWO();
            obj2.FOUR();

            Console.ReadLine();
        }
    }
}

从这个示例中,接口概念已经被清除,但有一件事让我感到困惑,那就是这条线......

IFive obj1 = new ODDEVEN();

他是如何制造一个物体......从我的想法来看,他应该以这种方式制造和反对

ODDEVEN obj1 = new ODDEVEN();

他正在制作" ODDEVEN" class..can任何人都可以用简单的语言解释我这个对象的创建,因为我是programmimg的新手......提前致谢

1 个答案:

答案 0 :(得分:1)

这是以下的捷径:

ODDEVEN temp = new ODDEVEN();
IFive obj1 = temp;

这是有效的,因为ODDEVEN实现了IFive,因此它可以分配给该接口引用。

相关问题