C#学习—类的使用

一、定义类

为了使程序更加清晰,方便使用,使用面向对象编程
类实际上就是对象的模板,每个对象都含有数据,并提供了处理和访问数据的方法
类定义了类的每个对象(称为实例)可以包含什么数据和功能,主要包含数据成员和函数成员
数据成员:字段,
函数成员:方法
代码示例:首先在项目下创建一个类,命名为Customer,包含三个字段及一个函数,在main函数下调用

Customer.cs

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

namespace _类
{
    class Customer
    {
        //定义四个字段,数据成员
        public string name;
        public string address;
        public int age;
        public string buytime;

        //定义一个方法,函数成员
        public void Show()
        {
            Console.WriteLine("客户姓名:"+ name);
            Console.WriteLine("客户地址:"+ address);
            Console.WriteLine("客户年龄:{0}", age);
            Console.WriteLine("购买时间:{0}", buytime);
        }
        
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _类
{
    class Program
    {
        static void Main(string[] args)
        {
            //因为在同一个项目之下,可以直接使用,如果不在需要引入命名空间
            //1.创建声明一个对象,
            Customer customer1;
            //2.对象初始化,不初始化无法使用,使用new关键字进行初始化
            customer1 = new Customer();
            customer1.name = "siki";
            Console.WriteLine(customer1.name);
            //使用对象中的方法
            customer1.Show();
            //控制台暂停
            Console.ReadKey();
        }
    }
}

运行结果
在这里插入图片描述

二、类的声明与使用

练习一:
定义一个车辆(Vehicle)类,具有run、stop俩个方法,具有speed、maxspeed。weight字段
答:
在项目中新建类文件,命名为Vehicle.cs
Vehicle.cs

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

namespace _类的定义与声明
{
    //新建一个车辆类
    class Vehicle
    {
        public float speed;
        public float maxSpace;
        public float weight;

        //新建方法run

        public void Run()
        {
            Console.WriteLine("车辆正在以{0}m/s速度前行", speed);
        }
		//新建方法stop
        public void Stop()
        {
            speed = 0;
            Console.WriteLine("车辆停止");
              
        }
    }
}

在Program.cs的main中进行赋值调用

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

namespace _005_类的定义与声明
{
    class Program
    {
        static void Main(string[] args)
        {
            //类的实例及初始化,car1为实例
            Vehicle car1 = new Vehicle();
            //将车辆速度赋值100
            car1.speed = 100;
            //调用 Vehicle类的run方法
            car1.Run();
            //调用 Vehicle类的stop方法
            car1.Stop();
            Console.WriteLine("当前车辆速度为:"+car1.speed);
            Console.ReadKey();
        }
    }
}

运行结果
在这里插入图片描述
练习二:
定义一个向量,ThreeVector,包含想,x,y,z三个字段,并且有计算长度的方法,有设置属性(set)的方法
使用这个变量声明一个变量(对象)
答:
新建一个ThreeVector的类

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

namespace _类的定义与声明
{
    class ThreeVector
    {
        //新建x,y,z字段
        public float x;
        public float y;
        public float z;
        //新建长短计算方法Length
        //注意函数前,类型格式为float,不为void,有返回值
        public float Length()
        {
            //根据xyz计算长度,并返回
            return (float)Math.Sqrt(x * x + y * y + z * z);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _类的定义与声明
{
    class Program
    {
        static void Main(string[] args)
        {
            //类的实例及初始化,vector1为实例
            ThreeVector vector1 = new ThreeVector();
            //赋值
            vector1.x = 1;
            vector1.y = 1;
            vector1.z = 1;
            Console.WriteLine("当前向量长度为:{0}", vector1.Length()) ;
            Console.ReadKey();
        }
    }
}

运行结果
在这里插入图片描述
编程规范上,将类中的字段设置为private,只可以在类的内部进行方法,不可以进行访问,上述在main函数中就无法进行赋值

在这里插入图片描述

为字段提供修改犯法,进行修改,代码如下
ThreeVector.cs

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

namespace _类的定义与声明
{
    class ThreeVector
    {
        //新建x,y,z字段
        private float x;
        private float y;
        private float z;

        //为字段设置set方法,来设置字段的值
        //如果我么直接在方法内部访问同名变量,优先访问最近形参
        //使用this关键字,可以将变量确定为当前类中,而不会造成歧义
        public void SetX(float x) 
        { 
            this.x = x;
        }
        public void SetY(float y)
        {
            this.y = y;
        }
        public void SetZ(float z)
        {
            this.z = z;
        }
        public float Length()
        {
            //根据xyz计算长度,并返回
            return (float)Math.Sqrt(x * x + y * y + z * z);
        }
    }
}

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

namespace _类的定义与声明
{
    class Program
    {
        static void Main(string[] args)
        {
            //类的实例及初始化,vector1为实例
            ThreeVector vector1 = new ThreeVector();
            //赋值
            //vector1.x = 1;
            //vector1.y = 1;
            //vector1.z = 1;
            //通过set方法进行赋值
            vector1.SetX(2);
            vector1.SetY(2);
            vector1.SetZ(2);
            Console.WriteLine("当前向量长度为:{0}", vector1.Length()) ;
            Console.ReadKey();
        }
    }
}

运行结果
在这里插入图片描述

三、构造函数

当我们构造对象的时候,对象的初始化,是自动完成的,但是扎起初始化对象的过程中有点时候,并不希望回到默认的系统值
构造函数就是用于初始化数据的函数
声明基本的构造函数的语法就是声明一个和所在类同名的方法,但是没有返回类型
例如上面中ThreeVector类,可以加入一个public ThreeVector()函数
ThreeVector.cs

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

namespace _类的定义与声明
{
    class ThreeVector
    {
        //建造一个构造函数,用来进行初始化
        //构造函数可以带有参数,也可以不带参数
        public ThreeVector(float x, float y, float z) 
        {
            Console.WriteLine("构造函数被调用了,原始构造函数被停用");
            //初始化x,y,z
            this.x = x;
            this.y = y;
            this.z = z;

        }

        public float Length()
        {
            //根据xyz计算长度,并返回
            return (float)Math.Sqrt(x * x + y * y + z * z);
        }
    }
}

Main.cs

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

namespace _005_类的定义与声明
{
    class Program
    {
        static void Main(string[] args)
        {
            ThreeVector vector1 = new ThreeVector(3,3,3);
            Console.WriteLine("当前向量长度为:{0}", vector1.Length()) ;
            Console.ReadKey();
        }
    }
}

在这里插入图片描述

四、属性

属性和字段不太一样,包含两个模块set,get
访问属性和访问字段一样,当取得会调用get,设置数据的时候调用set
ThreeVector.cs

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

namespace _005_类的定义与声明
{
    class ThreeVector
    {
        //新建一个属性
        public int MyintProperty 
        {
            //需要一个get
            set 
            {
                //用来赋值
                //
                Console.WriteLine("属性中的set块被调用");
                Console.WriteLine("set的value的数值"+value);
            }
            //需要一个set
            get
            {
                //用来设置返回值
                Console.WriteLine("属性中的get块被调用");
                return 100;
            }
          //需要一个set
        }
    }
}

Main.cs

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

namespace _类的定义与声明
{
    class Program
    {
        static void Main(string[] args)
        {
            //使用属性和使用字段一样
            //实例初始化
            ThreeVector vector1 = new ThreeVector();
            //赋值操作
            vector1.MyintProperty = 0;
            //取值操作
            int temp = vector1.MyintProperty;
            Console.WriteLine("temp:{0}", temp);
            Console.ReadKey();
        }
    }
}

在这里插入图片描述

五、总结

属性的set和get可以任意存在一个,不是必须同时存在
如果没有get就无法取值,set没有就没法赋值
1.通过属性来访问字段
ThreeVector.cs

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

namespace _005_类的定义与声明
{
    class ThreeVector
    {
        //新建私有x,y,z字段
        private float x;
        private float y;
        private float z;
        //设置属性,通过属性来访问字段,也可以叫做get,set方法
        public float X 
        {
            get { return x; }
            set { x = value; }
        }
    }
}

Main.cs

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

namespace _005_类的定义与声明
{
    class Program
    {
        static void Main(string[] args)
        {
            ThreeVector vector1 = new ThreeVector();
            vector1.X = 100;
            Console.WriteLine("X:{0}", vector1.X);
            Console.ReadKey();

        }
    }
}

在这里插入图片描述
2.属性的简写形式

//编译器会自动给我提供一个字段来存储,效果和上面是一样的
public float X { get; set; }
  • 5
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值