C#_1_面相对象(不是规范)

类的创建

//定义了一个新的类Customer
class Customer
{
	//这里是数据成员
	public string name;
	public string address;
	public int age;
	public string buyTime;

	//这里是函数成员,函数名首字母大写
	public void Show()
	{
	Console.WriteLine("姓名" + name);
	Console.WriteLine("address" + address);
	Console.WriteLine("age" + age);
	Console.WriteLine("buyTime" + buyTime);
	}
}

创建对象

//如果要使用一个类,要先引入它所在的命名空间
//若定义在当前命名空间下则不需要
Customer customer_1; // 声明变量(对象)
customer_1 = new Customer(); // 初始化对象
customer_1.name = "langlang";
Console.WriteLine(customer_1.name);
customer_1.Show();
Console.ReadKey();

再写一个

namespace Learn_class
{
    class Vehicle
    {
        public float speed;
        public float maxSpeed;
        public float weight;
        public float x, y, z;

        public void Run()
        {
            Console.WriteLine("vehicle is running!");
        }

        public void Stop()
        {
            Console.WriteLine("vehicle is stop");
            speed = 0;
        }

        public float Length()
        {
        	//Math.Sqrt()求平方根,所得为double,转换一下
            return (float)Math.Sqrt(x*x + y*y + z*z);
        }
    }
}
Vehicle vehicle_1;
vehicle_1 = new Vehicle();
vehicle_1.speed = 10;
vehicle_1.Run();
vehicle_1.Stop();
vehicle_1.x = 20;
vehicle_1.y = 30;
vehicle_1.z = 40;
Console.Write(vehicle_1.Length());
Console.ReadKey();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值