面向对象(二)

1.自动属性

2.接口的实现(正常实现,显示实现)

3.静态的初始化与生命周期

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

namespace _04自动属性
{
    abstract class Person // 李村
    {
        private string name;

        // 属性可读可写,没有约束
        public string Name
        {
            get { return name; }
            set { name = value; }
        }


        // 懒
        public int Age
        {
            get;
            set;
        }
        //未实现(接口里面的写法)
        public abstract char Sex
        {
            get;
            set;
        }
    }
}


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

namespace _02接口
{
    //public interface I1
    //{
    //    void Func();
    //}
    //public interface I2
    //{
    //    void Func();
    //}
    //class MyClass : I1, I2
    //{ 
    
    //}

    public interface IMyInterface
    { 
        // 接口里面可以有的成员:
        // 方法、属性、索引、事件
        // 是对能力的抽象

        // 成员写法:1、不需要访问修饰符;2、没有方法体
        void Func();
        string Name
        {
            get;
            set;
        }
        string this[int index]
        {
            get;
            set;
        }
        event Action MyEvent;
    }

    // 实现接口:1、正常实现;2、显式实现
    // 1、就像没有接口一样使用
    class MyClass : IMyInterface
    {
        public void Func()
        {
            throw new NotImplementedException();
        }

        public string Name
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string this[int index]
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public event Action MyEvent;
    }
    // 2、显式实现接口:
    //  为什么要有:实现多个接口中有重名的方法
    //  不要修饰符,使用接口名.成员名实现
    class MyClass2 : IMyInterface
    {
        void IMyInterface.Func()
        {
            throw new NotImplementedException();
        }

        string IMyInterface.Name
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        string IMyInterface.this[int index]
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        event Action IMyInterface.MyEvent
        {
            add { throw new NotImplementedException(); }
            remove { throw new NotImplementedException(); }
        }
    }
    //  使用注意:显式实现的接口对象,只允许使用接口类型进行调用

    public interface I
    {
        void Func();
    }
    class Class : I
    {
        void I.Func()
        {
            throw new NotImplementedException();
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            Class c = new Class();
            I i = c;
            
            i.Func();

            // 接口就是为了多态而诞生的
        }
    }
}

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

namespace _06静态的初始化与生命周期以及使用建议
{
    // 静态在访问这个类型的第一次进行初始化,在程序结束后才会被释放资源
    class MyClass
    {
        // public int numPublic = 10; // 这里的操作是构造方法做的,是为了方便程序员给的简单的语法

        // public static int numStatic = 20;
        public int numPublic;
        public static int numStatic;

        public MyClass()
        {
            numPublic = 10;
        }
        // 静态构造方法
        static MyClass()
        {
            numStatic = 20;
            // 静态构造方法目的在初始化静态成员,不允许手动调用
            // 不允许有重载继承等内容
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            MyClass m1 = new MyClass();
            // 访问MyClass->初始化静态成员(调用静态构造方法)
            // ->初始化实例成员(调用实例构造方法)


            MyClass m2 = new MyClass();
            // 访问MyClass->实例构造方法
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值