定义类
在 C# 中,类是用于创建对象的蓝图。它定义了对象的数据和行为。通过定义类,你可以创建具有特定属性和方法的对象。
// www.javascriptcn.com code example
public class Person
{
// 属性
public string Name { get; set; }
public int Age { get; set; }
// 构造函数
public Person(string name, int age)
{
Name = name;
Age = age;
}
// 方法
public void Introduce()
{
Console.WriteLine($"我的名字叫 {Name},我今年 {Age} 岁。");
}
}类的属性
属性是类的成员之一,用于存储类的状态信息。属性可以分为只读、只写和可读写的。使用属性可以控制对类数据成员的访问。
// www.javascriptcn.com code example
public class Car
{
private string _brand;
// 只读属性
public string Brand
{
get { return _brand; }
}
// 可读写属性
public string Model { get; set; }
// 只写属性
public int Year { set { _year = value; } }
}类的构造函数
构造函数是用于初始化新创建的对象的特殊方法。每个类至少有一个构造函数。如果未显式定义构造函数,则编译器会自动添加一个默认构造函数。
// www.javascriptcn.com code example
public class Animal
{
public string Name { get; set; }
// 默认构造函数
public Animal() { }
// 带参数的构造函数
public Animal(string name)
{
Name = name;
}
}类的继承
在面向对象编程中,继承是一种机制,允许一个类(子类)继承另一个类(基类)的属性和方法。继承使得代码复用更加容易,并且有助于构建复杂的系统。
// www.javascriptcn.com code example
// 基类
public class Animal
{
public string Name { get; set; }
public void Eat()
{
Console.WriteLine($"{Name} 在吃东西。");
}
}
// 子类
public class Dog : Animal
{
public void Bark()
{
Console.WriteLine("汪!汪!");
}
}
// 使用
Dog myDog = new Dog();
myDog.Name = "旺财";
myDog.Eat(); // 输出:旺财 在吃东西。
myDog.Bark(); // 输出:汪!汪!类的多态性
多态性是指不同类的对象对同一消息做出响应的能力。这种能力使我们可以通过一个接口来表示不同的类型。多态性通常与继承一起使用。
// www.javascriptcn.com code example
public abstract class Shape
{
public abstract double GetArea();
}
public class Circle : Shape
{
private double _radius;
public Circle(double radius)
{
_radius = radius;
}
public override double GetArea()
{
return Math.PI * _radius * _radius;
}
}
public class Rectangle : Shape
{
private double _width;
private double _height;
public Rectangle(double width, double height)
{
_width = width;
_height = height;
}
public override double GetArea()
{
return _width * _height;
}
}
// 使用
Shape circle = new Circle(5);
Shape rectangle = new Rectangle(4, 6);
Console.WriteLine(circle.GetArea()); // 输出:圆的面积
Console.WriteLine(rectangle.GetArea()); // 输出:矩形的面积类的抽象类
抽象类是一种不能实例化的类,它只能作为其他类的基类。抽象类可以包含抽象方法,这些方法没有实现,并且必须由派生类提供实现。
// www.javascriptcn.com code example
public abstract class Vehicle
{
public abstract void Drive();
public void StartEngine()
{
Console.WriteLine("引擎已启动。");
}
}
public class Car : Vehicle
{
public override void Drive()
{
Console.WriteLine("汽车正在行驶。");
}
}
// 使用
Car myCar = new Car();
myCar.StartEngine();
myCar.Drive();类的接口
接口定义了一组方法和属性,但不提供它们的具体实现。实现接口的类必须提供接口中所有成员的具体实现。接口允许你为一组相关功能定义一个通用契约。
// www.javascriptcn.com code example
public interface IPlayable
{
void Play();
void Pause();
void Stop();
}
public class AudioPlayer : IPlayable
{
public void Play()
{
Console.WriteLine("音频正在播放。");
}
public void Pause()
{
Console.WriteLine("音频已暂停。");
}
public void Stop()
{
Console.WriteLine("音频已停止。");
}
}
// 使用
IPlayable player = new AudioPlayer();
player.Play();
player.Pause();
player.Stop();类的静态成员
静态成员属于类本身而不是类的实例。静态成员可以用类名直接访问,而不需要创建类的实例。静态成员通常用于表示与类相关的全局状态或常量。
// www.javascriptcn.com code example
public class Constants
{
public static readonly double Pi = 3.14159;
}
public class Circle
{
public double Radius { get; set; }
public double GetArea()
{
return Constants.Pi * Radius * Radius;
}
}
// 使用
double area = Circle.GetArea(5); // 使用静态成员 Constants.Pi
Console.WriteLine(area); // 输出:78.53975类的密封类
密封类(sealed class)是一个不能被继承的类。使用 sealed 关键字标记的类不能被其他类继承。密封类通常用于那些不应该被扩展的类。
// www.javascriptcn.com code example
public sealed class Singleton
{
private static Singleton _instance;
private Singleton() { }
public static Singleton Instance
{
get
{
if (_instance == null)
{
_instance = new Singleton();
}
return _instance;
}
}
}
// 尝试继承密封类会导致编译错误
// public class AnotherSingleton : Singleton
// {
// // 编译错误:无法从 Singleton 继承
// }类的嵌套类
嵌套类是在另一个类内部定义的类。嵌套类可以访问外部类的所有成员,包括私有成员。这使得嵌套类可以方便地封装一些与外部类相关的功能。
// www.javascriptcn.com code example
public class OuterClass
{
private int _data = 42;
public class NestedClass
{
public void PrintData(OuterClass outer)
{
Console.WriteLine($"外部类的数据:{outer._data}");
}
}
}
// 使用
OuterClass.NestedClass nested = new OuterClass.NestedClass();
nested.PrintData(new OuterClass());类的内部类
内部类是另一种类型的嵌套类,它与外部类的关系比普通嵌套类更紧密。内部类可以访问外部类的所有成员,甚至包括私有成员,并且其行为更像是外部类的一部分。
// www.javascriptcn.com code example
public class OuterClass
{
private int _data = 42;
public class InnerClass
{
public void PrintData()
{
Console.WriteLine($"外部类的数据:{_data}");
}
}
}
// 使用
OuterClass.InnerClass inner = new OuterClass.InnerClass();
inner.PrintData(); // 输出:外部类的数据:42类的泛型类
泛型类允许你在定义类时指定类型参数,从而使得类可以处理多种数据类型。使用泛型类可以提高代码的重用性和类型安全性。
// www.javascriptcn.com code example
public class GenericList<T>
{
private T[] items = new T[100];
private int count;
public void Add(T item)
{
items[count] = item;
count++;
}
public T GetItem(int index)
{
return items[index];
}
}
// 使用
GenericList<int> intList = new GenericList<int>();
intList.Add(10);
intList.Add(20);
GenericList<string> stringList = new GenericList<string>();
stringList.Add("Hello");
stringList.Add("World");
Console.WriteLine(intList.GetItem(1)); // 输出:20
Console.WriteLine(stringList.GetItem(1)); // 输出:World以上内容涵盖了 C# 中类的基本概念和使用方法。希望这些示例和解释能帮助你更好地理解和应用 C# 类。