類和類的引用3/private的定義和使用
namespace ConsoleApp52
{
? ? class Vector3
? ? {? ?//先用私有進(jìn)行定義
? ? ? ? private float x;
? ? ? ? private float y;
? ? ? ? private float z;
? ? ? ?//再對之前定義的變量,進(jìn)行賦值。賦值是用一個函數(shù)來進(jìn)行。所以就有大括號
? ? ? ? public void SetX(float temp)
? ? ? ? { x = temp; }
? ? ? ? public void SetY(float temp)
? ? ? ? { y = temp; }
? ? ? ? public void SetZ(float temp)
? ? ? ? { z = temp; }
? ? ? ? public double Length()
? ? ? ? {
? ? ? ? ? ? return Math.Sqrt(x*x+y*y+z*z);
? ? ? ? }
? ? }
}
=====================================================
namespace ConsoleApp52
{
? ? class Program
? ? {
? ? ? ? static void Main(string[] args)
? ? ? ? {
? ? ? ? ? ? Vector3 v1 = new Vector3();
? ? ? ? ? ? v1.SetX(2.3f);
? ? ? ? ? ? v1.SetY(6.3f);
? ? ? ? ? ? v1.SetZ(12.1f);
? ? ? ? ? ? Console.WriteLine(v1.Length());
? ? ? ? }
? ? }
}