zoukankan      html  css  js  c++  java
  • 38属性的种种,只读只写属性、自动属性、静态属性、抽象属性、接口属性

    □ 只读属性

    public class Example
    {
        string name;
        public string Name
        {
            get {return name;}
        }
    }

    □ 只写属性

    public class Example
    {
        string name;
        public string Name
        {
            set {name = value;}
        }
    }

    □ 可读可写属性

    public class Example
    {
        string name;
        public string Name
        {
            get {return name;}
            set {name = value;}
        }
    }

    □ 自动属性

    public class Example
    {
        public string Name {get;set;}
    }

    自动只读属性:

    public class Example
    {
        public string Name{get; private set;}
    }

    自动只写属性:

    public class Example
    {
        public string Name{private get; set;}
    }

    □ 静态属性

    静态属性对应一个静态字段,通常用在"单例模式"中。"单例模式"构造函数必须是私有的。

    public class Example
    {
        private static Example instance = new Example();
        private Example(){}

        public static Example GetInstance
        {
            get {return instance;}
        }
    }

    □ 抽象属性

    抽象类和抽象属性。

    public abstrace class Person
    {
        public abstract string Name{get;set;}
    }

    抽象属性在子类中实现。

    public class Student : Person
    {
        private string name;

        public override string Name
        {
            get {return name;}
            set {name = value;}
        }
    }

    □ 接口属性

    public interface IPerson
    {
        string Name {get;set;}
    }

    public class Student : IPerson
    {
        private string name;

        public string Name
        {
            get {return name;}
            set {name = value;}
        }
    }

    参考资料:
    A Deep Dive into C# Property

  • 相关阅读:
    Netty ChannelHandler组件作用
    Netty Channel组件作用
    Netty NioEventLoop自定义任务处理
    NIO与BIO
    JDK ByteBuffer与Netty ByteBuf
    linux-源码软件管理-yum配置
    typora使用快捷键
    远程连接mysql库问题
    MVC 后台处理 json格式的日期
    使用 SqlClient 创建数据库连接并获取数据
  • 原文地址:https://www.cnblogs.com/darrenji/p/3695188.html
Copyright © 2011-2022 走看看