c#之面向对象编程

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

namespace 面向对象编程之类
{
    class Vector3
    {
        //编程规范上,习惯把所有的字段设置为private,只可以在类的内部访问,不可以通过对象访问 
        private float x, y, z;

        //为字段提供set方法,来设置字段的值
        public void SetX(float x)
        {
            //通过this. 表示访问的是类的字段或方法
            this.x = x;
        }
        public void SetY(float y)
        {
            this.y = y;
        }
        public void SetZ(float z)
        {
            this.z = z;
        }
        public float Length()
        {
            return (float)Math.Sqrt(x*x + y*y +z*z);
        }
    }
}
namespace 面向对象编程之类
{
    class Program
    {
        static void Main(string[] args)
        {
            //Customer customer1 = new Customer();
            //customer1.name = "aaa";
            //Console.WriteLine(customer1.name);
            Vector3 vect = new Vector3();
            vect.SetX(1);
            vect.SetY(1);
            vect.SetZ(1);
            Console.WriteLine(vect.Length());

        }
    }
}

你可能感兴趣的:(c#,开发语言)