zoukankan      html  css  js  c++  java
  • 使用自动属性减少代码输入量

    .Net 3.0中的自动属性可以大幅度降低我们输入的代码量如:

    public class Product
        {
            private String name;
            public String Name
            {
                get
                {
                    return name;
                }
                private set
                {
                    name = value;
                }
            }

            private Decimal price;
            public Decimal Price
            {
                get
                {
                    return price;
                }
                set
                {
                    price = value;
                }
            }

            public Product(String name, Decimal price)
            {
                this.price = price;
                this.name = name;
            }
        }

    可以改写为:

    public class Product
        {
            public String Name
            {
                get;
                private set;
            }

            public Decimal Price
            {
                get;
                set;
            }

            public Product(String name, Decimal price)
            {
                Name = name;
                Price = price;
            }

            public override string ToString()
            {
                return String.Format("{0}:{1}", this.Name, this.Price);
            }
        }

    代码是不是简化了很多!

    注意:

    不能定义只读或者只写的属性,必须同时提供
    如果想在属性中增加判断、验证等逻辑,则只能用传统的属性定义方法实现

  • 相关阅读:
    几张图就把 Kubernetes Service 掰扯清楚了
    一文读懂 Kubernetes APIServer 原理
    K8S调度系统由浅入深系列:简介
    K8S蓝绿部署:Blue/Green Deployments on Kubernetes
    K8s Nginx Ingress 介绍
    探索 Kubernetes HPA
    [译]走进Kubernetes集群的大脑:Etcd
    AIX文件系统大小限制调整(转)
    修改SSH服务监听端口,引起服务启动失败原因分析
    Docker-compos部署nextcloud私有网盘
  • 原文地址:https://www.cnblogs.com/wangluojisuan/p/2799836.html
Copyright © 2011-2022 走看看