zoukankan      html  css  js  c++  java
  • 关于Property赋值

    赋值1:

    View Code
     1 using System;
     2 
     3 class ComplexNumber
     4 {
     5      public double Real { get; set; }
     6      public double Imaginary { get; set; }
     7  
     8      public override string ToString()
     9      {
    10           return String.Format("{0}{1}{2}i", 
    11               Real, 
    12               Imaginary >= 0 ? "+" : "-", 
    13               Math.Abs(Imaginary));
    14       }
    15 }
    16 
    17 class CompareComplexNumbers
    18 {
    19      public static void Main()
    20      {
    21           ComplexNumber a = new ComplexNumber() //初始化赋值
    22               { Real = 4.5D, Imaginary = 8.4D };
    23           ComplexNumber b = new ComplexNumber() 
    24               { Real = 6.3D, Imaginary = -2.3D };
    25           ComplexNumber c = new ComplexNumber() 
    26               { Real = 4.5D, Imaginary = 8.4D };
    27   
    28           Console.WriteLine("{0} equals {1}: {2}", 
    29               a, b, CompareTwoComplexNumbers(a, b));
    30           Console.WriteLine("{0} equals {1}: {2}", 
    31               b, c, CompareTwoComplexNumbers(b, c));
    32           Console.WriteLine("{0} equals {1}: {2}", 
    33               a, c, CompareTwoComplexNumbers(a, c));
    34       }
    35  
    36      static bool CompareTwoComplexNumbers(ComplexNumber A, ComplexNumber B)
    37      {
    38           // This returns true only if the real parts and the imaginary parts are equal
    39           return ((A.Real == B.Real) && (A.Imaginary == B.Imaginary));
    40       }
    41 }

    赋值2:property的setter为private屏蔽外界,赋值可通过constructer,property的setter没有写代表只读,所以赋值需要从filed

    View Code
     1 /// <summary>
     2 /// A guy with a name, age and a wallet full of bucks
     3 /// </summary>
     4 class Guy
     5 {
     6      /// <summary>
     7      /// Read-only backing field for the Name property
     8      /// </summary>
     9      private readonly string name;
    10  
    11      /// <summary>
    12      /// The name of the guy
    13      /// </summary>
    14      public string Name { get { return name; } }
    15  
    16      /// <summary>
    17      /// Read-only backing field for the Name property
    18      /// </summary>
    19      private readonly int age;
    20  
    21      /// <summary>
    22      /// The guy's age
    23      /// </summary>
    24      public int Age { get { return age; } }
    25  
    26      /// <summary>
    27      /// The number of bucks the guy has
    28      /// </summary>
    29      public int Cash { get; private set; }
    30  
    31      /// <summary>
    32      /// The constructor sets the name, age and cash
    33      /// </summary>
    34      /// <param name="name">The name of the guy</param>
    35      /// <param name="age">The guy's age</param>
    36      /// <param name="cash">The amount of cash the guy starts with</param>
    37      public Guy(string name, int age, int cash) {
    38           this.name = name;
    39           this.age = age;
    40           Cash = cash;
    41       }
    42  
    43      public override string ToString() {
    44           return String.Format("{0} is {1} years old and has {2} bucks", Name, Age, Cash);
    45       }
    46  
    47      /// <summary>
    48      /// Give cash from my wallet
    49      /// </summary>
    50      /// <param name="amount">The amount of cash to give</param>
    51      /// <returns>The amount of cash I gave, or 0 if I don't have enough cash</returns>
    52      public int GiveCash(int amount) {
    53           if (amount <= Cash && amount > 0)
    54           {
    55                Cash -= amount;
    56                return amount;
    57            }
    58           else
    59           {
    60                return 0;
    61            }
    62       }
    63  
    64      /// <summary>
    65      /// Receive some cash into my wallet
    66      /// </summary>
    67      /// <param name="amount">Amount to receive</param>
    68      /// <returns>The amount of cash received, or 0 if no cash was received</returns>
    69      public int ReceiveCash(int amount) {
    70           if (amount > 0)
    71           {
    72                if (amount > 0)
    73                {
    74                     Cash += amount;
    75                     return amount;
    76                 }
    77                Console.WriteLine("{0} says: {1} isn't an amount I'll take", Name, amount);
    78            }
    79           return 0;
    80       }
    81 }
  • 相关阅读:
    chapter1 Qt入门
    base64加密解密
    vue+CryptoJS+cookie实现保存账号密码
    js计时器
    Elenent ui中的表单校验规则、自定义校验规则、预验证功能
    js获取文件后缀
    js中如何将有字符串转换为数组,或将数组转换为字符串
    vue-router
    Vue项目纯前端导出word文档
    async/await
  • 原文地址:https://www.cnblogs.com/shawnzxx/p/3068278.html
Copyright © 2011-2022 走看看