zoukankan      html  css  js  c++  java
  • c#使用属性封装

    1、使用属性封装方法

    AccessModifer Type PropertyName
    {
    get{...}
    set{...}
    }
    public int {
    get{ return this.X}
    set{this.x=rangeCheckX(value)}
    }

    2、属性对某个字段封装时,命名规则,首字母大写

    private int employeeID;
    public int EmployeeID
    {
    get{return this.EmployeeID;};
    set{this.EmployeeID=value}
    }

    3、接口中定义属性

    interface IScreenPosition
    
    {
    
    int X{get;set;}
    
    int Y{get;set;}
    
    }

    4、实例化同时赋值

    static void DoWork()
            {
                Polygon square = new Polygon();
                Polygon triangle = new Polygon { NumSides = 3 };
                Polygon pentagon = new Polygon { SideLength = 15.5, NumSides = 5 };
    
                Console.WriteLine("Square: number of sides is {0}, length of each side is {1}", 
                    square.NumSides, square.SideLength);
                Console.WriteLine("Triangle: number of sides is {0}, length of each side is {1}", 
                    triangle.NumSides, triangle.SideLength);
                Console.WriteLine("Pentagon: number of sides is {0}, length of each side is {1}", 
                    pentagon.NumSides, pentagon.SideLength);
            }
  • 相关阅读:
    中海洋朗讯杯比赛总结[2014年12月]
    青理工ACM比赛总结和反思[2014年11月]
    程序员技术练级攻略
    一天能学会的计算机技术
    UVa 1597
    回滚机制
    超时和重试机制
    降级特技
    限流详解
    隔离术
  • 原文地址:https://www.cnblogs.com/lujianwei/p/2559458.html
Copyright © 2011-2022 走看看