<bdo id='IM8Vw'></bdo><ul id='IM8Vw'></ul>
<legend id='IM8Vw'><style id='IM8Vw'><dir id='IM8Vw'><q id='IM8Vw'></q></dir></style></legend>

    <tfoot id='IM8Vw'></tfoot>

    <small id='IM8Vw'></small><noframes id='IM8Vw'>

    1. <i id='IM8Vw'><tr id='IM8Vw'><dt id='IM8Vw'><q id='IM8Vw'><span id='IM8Vw'><b id='IM8Vw'><form id='IM8Vw'><ins id='IM8Vw'></ins><ul id='IM8Vw'></ul><sub id='IM8Vw'></sub></form><legend id='IM8Vw'></legend><bdo id='IM8Vw'><pre id='IM8Vw'><center id='IM8Vw'></center></pre></bdo></b><th id='IM8Vw'></th></span></q></dt></tr></i><div id='IM8Vw'><tfoot id='IM8Vw'></tfoot><dl id='IM8Vw'><fieldset id='IM8Vw'></fieldset></dl></div>
    2. 速记访问器和突变器

      Shorthand Accessors and Mutators(速记访问器和突变器)

      <small id='p8a3E'></small><noframes id='p8a3E'>

      <legend id='p8a3E'><style id='p8a3E'><dir id='p8a3E'><q id='p8a3E'></q></dir></style></legend>
      • <i id='p8a3E'><tr id='p8a3E'><dt id='p8a3E'><q id='p8a3E'><span id='p8a3E'><b id='p8a3E'><form id='p8a3E'><ins id='p8a3E'></ins><ul id='p8a3E'></ul><sub id='p8a3E'></sub></form><legend id='p8a3E'></legend><bdo id='p8a3E'><pre id='p8a3E'><center id='p8a3E'></center></pre></bdo></b><th id='p8a3E'></th></span></q></dt></tr></i><div id='p8a3E'><tfoot id='p8a3E'></tfoot><dl id='p8a3E'><fieldset id='p8a3E'></fieldset></dl></div>
          <tbody id='p8a3E'></tbody>

          1. <tfoot id='p8a3E'></tfoot>

              <bdo id='p8a3E'></bdo><ul id='p8a3E'></ul>
                本文介绍了速记访问器和突变器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在学习 C#,并且正在学习如何将字段设为类私有,并使用 Getter 和 Setter 来公开方法而不是字段值.

                I am learning C#, and am learning about making fields private to the class, and using Getters and Setters to expose Methods instead of field values.

                get;set;Method 1Method 2 等效?例如一个是另一个的简写吗?

                Are the get; set; in Method 1 and Method 2 equivalent? e.g. is one a shorthand of the other?

                class Student
                {
                    // Instance fields
                    private string name;
                    private int mark;
                
                    // Method 1
                    public string Name { get; set; }
                
                    // Method 2
                    public int Mark
                    {
                        get { return mark; }
                        set { mark = value; }
                    }
                }
                

                最后,当您想在获取或设置值之前执行计算时,是否会使用 方法 2?例如将值转换为百分比或执行验证?例如

                Finally, would Method 2 be used when you want to for example perform a calculation before getting or setting a value? e.g. converting value to a percentage or perform validation? e.g.

                class Student
                {
                    // Instance fields
                    private string name;
                    private double mark;
                    private int maxMark = 50;
                
                    // Method 1
                    public string Name { get; set; }
                
                    // Method 2
                    public double Mark
                    {
                        get { return mark; }
                        set { if ( mark <= maxMark ) mark = value / maxMark * 100; }
                    }
                }
                

                推荐答案

                是的,方法一是方法二的快捷方式,我建议默认使用方法一.当您需要更多功能时,请使用方法 2.您还可以为 get 和 set 指定不同的访问修饰符.

                Yes, Method 1 is a shortcut to Method 2. I suggest using Method 1 by default. When you need more functionality, use Method 2. You can also specify different access modifiers for get and set.

                这篇关于速记访问器和突变器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)
                Parameter count mismatch with Invoke?(参数计数与调用不匹配?)
                How to store delegates in a List(如何将代表存储在列表中)
                How delegates work (in the background)?(代表如何工作(在后台)?)
                C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)
                Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)

                  <legend id='2ob9F'><style id='2ob9F'><dir id='2ob9F'><q id='2ob9F'></q></dir></style></legend>
                      <bdo id='2ob9F'></bdo><ul id='2ob9F'></ul>
                      <tfoot id='2ob9F'></tfoot>
                      1. <small id='2ob9F'></small><noframes id='2ob9F'>

                            <tbody id='2ob9F'></tbody>

                          <i id='2ob9F'><tr id='2ob9F'><dt id='2ob9F'><q id='2ob9F'><span id='2ob9F'><b id='2ob9F'><form id='2ob9F'><ins id='2ob9F'></ins><ul id='2ob9F'></ul><sub id='2ob9F'></sub></form><legend id='2ob9F'></legend><bdo id='2ob9F'><pre id='2ob9F'><center id='2ob9F'></center></pre></bdo></b><th id='2ob9F'></th></span></q></dt></tr></i><div id='2ob9F'><tfoot id='2ob9F'></tfoot><dl id='2ob9F'><fieldset id='2ob9F'></fieldset></dl></div>