zoukankan      html  css  js  c++  java
  • c#中语句的先后顺序对结果的影响

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 
     6 namespace Test05
     7 {
     8     /// <summary>
     9     /// 自定义类,封装加数和被加数属性
    10     /// </summary>
    11     class MyClass
    12     {
    13         private int x = 0;                        //定义int型变量,作为加数
    14         private int y = 0;                        //定义int型变量,作为被加数
    15         /// <summary>
    16         /// 加数
    17         /// </summary>
    18         public int X
    19         {
    20             get
    21             {
    22                 return x;
    23             }
    24             set
    25             {
    26                 x = value;
    27             }
    28         }
    29         /// <summary>
    30         /// 被加数
    31         /// </summary>
    32         public int Y
    33         {
    34             get
    35             {
    36                 return y;
    37             }
    38             set
    39             {
    40                 y = value;
    41             }
    42         }
    43         /// <summary>
    44         /// 求和
    45         /// </summary>
    46         /// <returns>加法运算和</returns>
    47         public int Add()
    48         {
    49             return X + Y;
    50         }
    51     }
    52     class Program
    53     {
    54         static void Main(string[] args)
    55         {
    56             
    57             MyClass myclass = new MyClass();    //实例化MyClass的对象myclass,new出操作空间
    58             myclass.X = 3;                    //为MyClass类中的对象myclass的属性赋值
    59             myclass.Y = 5;                    //为MyClass类中的对象myclass的属性赋值
    60             int kg = myclass.Add();
    61             Console.WriteLine(kg);    //调用MyClass类中的Add方法求和
    62             Console.ReadLine();
    63         }
    64     }
    65 
    66 }

    第60行的语句若是被放到第56行,则结果输出是0不是8,所以,在设计程序时,要注意语句次序,有着清晰的思维逻辑 。

    当然,作为刚入门的我,还有很多需要学习的地方,希望大家多多指教,共同学习,一起进步!

  • 相关阅读:
    COM组件
    【游戏引擎架构】入门(一)
    UNICODE字符串
    Python随笔10
    Python随笔9-函数
    Python随笔7
    Python随笔6
    Python随笔5
    Python随笔4
    Python随笔3
  • 原文地址:https://www.cnblogs.com/liuyaozhi/p/4919885.html
Copyright © 2011-2022 走看看