interface 是什么类型?

博客指出interface是关键字,其类型由“变量”决定,若强理解为类型则是引用类型;class不是类型,但定义为class的都是引用类型,还给出了相关实例的转载链接。

interface不是类型,interface是关键字,定义为 interface 是什么类型,是由其“变量”决定的。如果一定要强去理解“接口到底是什么类型”?那么它必然是引用类型(这句话与前面那句矛盾)。

class也不是类型,只不过定义为 class 都是引用类型。

看看实例吧:

    public interface ITest
    {
         string Text { get; set; }
    }
    
    public struct TestStruct : ITest
    {
        public string Text { get; set; }
    }
    
    public class TestClass : ITest
    {
        public string Text { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Test1:");
            Test1();
            Console.WriteLine("Test2:");
            Test2();
            Console.ReadKey();
        }

        static void Test1()
        {
            TestStruct t1 = new TestStruct();
            t1.Text = "我爱辣妹";
            Func(t1);
            Console.WriteLine(t1.Text);
            TestClass t2 = new TestClass();
            t2.Text = "我爱辣妹";
            Func(t2);
            Console.WriteLine(t2.Text);
        }

        static void Test2()
        {
            ITest t1 = new TestStruct();
            t1.Text = "我爱辣妹";
            Func(t1);
            Console.WriteLine(t1.Text);
            ITest t2 = new TestClass();
            t2.Text = "我爱辣妹";
            Func(t2);
            Console.WriteLine(t2.Text);
        }

        static void Func(ITest test)
        {
            test.Text = "辣妹爱我";
        }
    }

 

转载于:https://www.cnblogs.com/sofire/archive/2010/12/07/1898658.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值