0

ここに、年齢、ID、名前、支払いの単純なプロパティを持つEmployeeオブジェクトの作成を可能にするプログラムスニペットがあります。それで遊んでいるだけで、私はそれに気づきました

 Console.WriteLine(joe.Age+1); is my Main() method returns one, 

しかし、Console.WriteLine(joe.Age++);0を返します。コンストラクターごとにAgeプロパティが0に初期化されることはわかっていますが、++演算子で1が追加されないのはなぜですか? 編集:私は奇妙な行動の原因を見つけました。Ageプロパティでは、次のempAge=Age値に等しいはずのときに持っていますvalue

ソース:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EmployeeApp
{
    class Employee
    {
        //field data
        //notice the the fields are declared as private
        //these fields are used in the constructors
        private string empName;
        private int empID;
        private float currPay;
        private int empAge;

        //properties! private field data should be accessed via public properties
        //note that properties don't use parentheses ()
        //within the set scope you see the 'value' contextual keyword
        //it represents the value being assigned by the caller and it will always be the same 
        //underlying data type as the property itself
        public int Age
        {
            get { return empAge; }
            set { empAge = Age; }
        }


        public string Name
        {
            get { return empName; }
            set
            {
                if (value.Length > 15)
                    Console.WriteLine("this name is too long.");
                else
                    empName = value;
            }
        }
        public int ID
        {
            get { return empID; }
            set { empID = value; }
        }
        public float pay
        {
            get { return currPay; }
            set { currPay = value; }
        }

        //constructors
        public Employee() { }

        public Employee(string name, int id, float pay, int age)
        {
            empName = name;
            empID = id;
            currPay = pay;
            empAge = age;
        }

        //methods
        //the int parameter that this method takes will come from somewhere in the Main method
        //currpay is a private field
        public void GiveBonus(float amount)
        {
            currPay += amount;
        }
        public void DisplayStats()
        {
            Console.WriteLine("name: {0}", empName);
            Console.WriteLine("ID: {0}", empID);
            Console.WriteLine("pay: {0}", currPay);
            Console.WriteLine("age: {0}", empAge);
        }
    }

}

ここでの主な方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//Encapsulation using traditional accessors/mutators or get/set methods
//the role of a get method is to return to the caller the current value of the underlying state data
//a set method allows the caller ot change the current value of the state data

//you need to have a getter and a setter for every field that the class has

namespace EmployeeApp
{
    class Program
    {
        static void Main(string[] args)
        {
            //Console.WriteLine("fun with encapsulation");
            //Employee emp = new Employee("marvin", 456, 4000, 56);
            //emp.GiveBonus(3);
           // emp.DisplayStats();
           // emp.Name = "wilson";
           // emp.DisplayStats();

            Employee joe = new Employee();

            Console.WriteLine(joe.Age++);
        }
    }
}
4

4 に答える 4

4

++インクリメンタル演算子には次の 2 つの用途があります。

joe.Age++

++joe.Age

最初のものは、使用しているように、現在の操作の後に実行されます。したがって、 を呼び出すとConsole.WriteLine(joe.Age++);、次のように表すこともできます。

Console.WriteLine(joe.Age);
joe.Age = joe.Age + 1;

したがって、現在の値を に渡し、WriteLineそれインクリメントしています。

先頭に を付けると++逆になり、インクリメントしてから値使用します。したがって、次のConsole.WriteLine(++joe.Age);ように読むこともできます。

joe.Age = joe.Age + 1;
Console.WriteLine(joe.Age);
于 2012-10-05T13:04:21.717 に答える
3

変数の後に単項 ++ 演算子を使用すると、外側の式が評価されるまで加算は行われません。変数の前に使用すると、外側の式が評価される前に加算が行われます。

例えば、

// this will increment joe.Age, and then write it to console.
Console.WriteLine(++joe.Age);

// this will write joe.Age to the console, and then increment it.
Console.WriteLine(joe.Age++);

msdnのドキュメントから:

最初の形式は、プレフィックスのインクリメント操作です。演算の結果は、インクリメントされた後のオペランドの値です。

2 番目の形式は、後置インクリメント操作です。演算の結果は、インクリメントされる前のオペランドの値です。

于 2012-10-05T13:04:11.493 に答える
1

Age プロパティで、メンバーを渡された値に変更していません。これがおそらく、複数回empAge試しても変更が見られない理由です。++

public int Age 
{ 
    get { return empAge; } 
    set { empAge = Age;  } // this does not set the value!
} 

value代わりに次を使用します。

public int Age 
{ 
    get { return empAge;   } 
    set { empAge = value;  } // use the value passed in
} 

また、他の人が指摘したように、++演算子の後置バージョンを使用しています。プレフィックス バージョンは、プロパティをコンソールに書き込む前に、最初に量を増やします。

于 2012-10-05T13:06:54.237 に答える
1

C++ と C# には、2 つの ++ 演算子があります。1 つ目は前置演算子 (++age) で、これは期待どおりに機能します。値をインクリメントしてから結果を返します。後置演算子 (age++) は値を増やしますが、の値を返します。

于 2012-10-05T13:04:39.210 に答える