【2.C#基础】9.类
9.类
9.1概述
C# 是一门面向对象编程语言,面向对象编程语言有三大特性,分别是封装、继承和多态。所谓封装就是将一个或多个项目(变量和方法)集合在一个单元中,这个单元称之为类。类的写法:
class 类名 {
成员变量声明;
成员方法实现;
}
在 C# 中,类是引用类型,其中包括状态(成员变量)和操作(成员方法)。
9.2类的创建
下面我们创建一个学生类来说明,代码如下:
class Student{
private int id;
private string name;
private string sex;
private int age;
public void insert(int i, string n, string s, int a){
id = i;
name = n;
sex = s;
age = a;
}
public void display(){
Debug.Log("编号:" + id + ",姓名:" + name + ",性别:" + sex + ",年龄:" + age);
}
}
在上面定义的类中,id、name、sex、age 是学生类的 4 个成员变量,分别代码编号、姓名、性别和年龄。insert是该类的一个成员方法,用于初始化成员变量,display 也是该类的一个成员方法,其作用就是把学生的信息输出。
9.3类的使用
在C#中,我们可以动态创建类的实例(instance),这个实例也被称为对象(object),我们可以通过类和对象来设计程序。 也就是说类可以被当作一种类型使用,使用类定义的变量就是对象。创建对象的写法:
类名 对象名 = new 类名();
要使用类中的定义的变量和方法,可以通过对象名来访问,格式如下:
对象名.成员变量名或成员方法名
下面我们使用学生类来创建对象并设置各个成员变量,同时输出信息。代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Cls : MonoBehaviour
{
class Student{
private int id;
private string name;
private string sex;
private int age;
public void insert(int i, string n, string s, int a){
id = i;
name = n;
sex = s;
age = a;
}
public void display(){
Debug.Log("编号:" + id + ",姓名:" + name + ",性别:" + sex + ",年龄:" + age);
}
}
// Start is called before the first frame update
void Start()
{
Student stu1 = new Student();
Student stu2 = new Student();
stu1.insert(101, "张三", "男", 18);
stu1.display();
stu2.insert(102, "李四", "女", 16);
stu2.display();
}
// Update is called once per frame
void Update()
{
}
}
运行后控制台输出:
编号:101,姓名:张三,性别:男,年龄:18
UnityEngine.Debug:Log(Object)
Student:display() (at Assets/Cls.cs:19)
Cls:Start() (at Assets/Cls.cs:29)
编号:102,姓名:李四,性别:女,年龄:16
UnityEngine.Debug:Log(Object)
Student:display() (at Assets/Cls.cs:19)
Cls:Start() (at Assets/Cls.cs:31)
9.4访问修饰符
我们可以根据需要通过访问权限修饰符来设定类中成员的范围和可见性。C# 中的访问权限修饰符有以下几种:
- public:公共的,所有对象都可以访问,但是需要引用命名空间;
- private:私有的,类的内部才可以访问;
- internal:内部的,同一个程序集的对象可以访问,程序集就是命名空间;
- protected:受保护的,类的内部或类的父类和子类中可以访问;
- protected internal:protected 和 internal 的并集,符合任意一条都可以访问。
1.public
类中使用 public 访问权限修饰符修饰的成员变量或成员方法可以在其他函数和对象,我们可以从类的外部访问类中的公共成员(使用 public 修饰的类成员)。下面通过一个示例来演示一下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Cls : MonoBehaviour
{
class Rectangle
{
// 成员变量
public double width, length;
public double GetArea(){
return width * length;
}
public void Display(){
Debug.Log("长方形的长:" + length);
Debug.Log("长方形的宽:" + width);
Debug.Log("长方形的面积:" + GetArea());
}
}
// Start is called before the first frame update
void Start()
{
Rectangle r = new Rectangle();
r.width = 5.5;
r.length = 3;
r.Display();
}
// Update is called once per frame
void Update()
{
}
}
上面的示例中,成员变量 length 和 width 都是使用 public 修饰的,所以可以在 Start 方法中使用 Rectangle 类的实例(对象)来进行访问。
2.private
类中使用 private 访问权限修饰符修饰的成员变量或成员方法不能在其它方法或对象访问,即使是类的实例也不能访问这个类中的私有成员,只有同属一个类的方法才可以访问这个类的私有成员。 下面通过一个示例来演示一下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Cls : MonoBehaviour
{
class Rectangle
{
// 成员变量
private double width, length;
public void Setup(double w, double l){
length = l;
width = w;
}
public double GetArea(){
return width * length;
}
public void Display(){
Debug.Log("长方形的长:" + length);
Debug.Log("长方形的宽:" + width);
Debug.Log("长方形的面积:" + GetArea());
}
}
// Start is called before the first frame update
void Start()
{
Rectangle r = new Rectangle();
r.Setup(5.5, 3);
r.Display();
}
// Update is called once per frame
void Update()
{
}
}
上面的示例中,成员变量 length 和 width 使用 private 修饰,因此无法通过 Start 方法访问,只能通过 Rectangle 类中的成员方法 Setup() 和 Display() 来访问这些变量。又因为 Rectangle 中的成员方法 Setup() 和 Display() 是使用 public 修饰的,因此可以使用 Rectangle 类的实例 r 来调用它们。
3.protected
类中使用 protected 访问权限修饰符修饰的成员变量和成员方法可以在其子类中访问,也就是说基类(父类)中使用 protected 访问权限修饰符修饰的成员变量和成员方法可以在其子类中访问,这样有助于实现继承。
4.internal
类中使用 internal 访问权限修饰符修饰的成员变量和成员方法可以在当前程序集中的其他函数或对象中使用。换句话说就是,任何使用 internal 修饰的成员都可以被同一命名空间下的任何类或方法访问。 下面通过示例来演示一下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Cls : MonoBehaviour
{
class Rectangle
{
// 成员变量
internal double width, length;
public double GetArea(){
return width * length;
}
public void Display(){
Debug.Log("长方形的长:" + length);
Debug.Log("长方形的宽:" + width);
Debug.Log("长方形的面积:" + GetArea());
}
}
// Start is called before the first frame update
void Start()
{
Rectangle r = new Rectangle();
r.width = 5.5;
r.length = 3;
r.Display();
}
// Update is called once per frame
void Update()
{
}
}
5.protected internal
类中使用 protected internal 访问权限修饰符修饰的成员可以在本类、派生类(子类)或者包含该类(使用 using 引入)的程序集中访问,在实现继承时也使用此方法。
9.5 this关键字
this 关键字指向实例自身。如在上面的代码中,当我们在类中使用类的成员变量或方法时,加上 this 关键字进一步强调是成员变量或方法,特别是当我们使用与类的成员同名的变量时,必须使用 this 关键字。示例如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Cls : MonoBehaviour
{
class Rectangle
{
// 成员变量
private double width, length;
public void Setup(double width, double length){
this.length = length;
this.width = width;
}
public double GetArea(){
return width * length;
}
public void Display(){
Debug.Log("长方形的长:" + length);
Debug.Log("长方形的宽:" + width);
Debug.Log("长方形的面积:" + this.GetArea());
}
}
// Start is called before the first frame update
void Start()
{
Rectangle r = new Rectangle();
r.Setup(5.5, 3);
r.Display();
}
// Update is called once per frame
void Update()
{
}
}
在上例的 Setup 方法中,传递的参数名称与类 Rectangle 的成员变量名称是相同的,这时,就必须使用 this 关键字来区分成员变量与参数。