C#-Set與Get基本範例

本篇為class中Set與Get基本範例。

using UnityEngine;
using System.Collections;

public class Car
{
    private string name = "Lexus";
    //name的get方法
    public string GetName()
    {
        return name;
    }
    //name的set方法
    public void SetName(string value)
    {
        name = value;
        Debug.Log("SetName is" + name);
    }

}

public class method : MonoBehaviour {

	// Use this for initialization
	void Start () {
        Car Car001 = new Car();
        Debug.Log(Car001.GetName());
        Car001.SetName("Farrelli");
        Debug.Log(Car001.GetName());
	}

	// Update is called once per frame
	void Update () {

	}
}

Debug.Log 結果:
0426

發表留言