C# 进阶之构造方法,虚方法,抽象方法,接口,析构方法,扩展方法(一)

在C#中,构造方法、虚方法、抽象方法、接口、析构方法和扩展方法都是面向对象编程中的重要概念。下面我会逐一解释这些概念,并给出相应的例子。

1. 构造方法(Constructor)

构造方法用于初始化对象的状态。它在创建对象时自动被调用,且必须与类名相同。

例子:

csharp
public class Person  
{  
    public string Name { get; set; }  
    public int Age { get; set; }  
  
    // 构造方法  
    public Person(string name, int age)  
    {  
        Name = name;  
        Age = age;  
    }  
}  

// 使用构造方法创建对象  
Person person = new Person("Alice", 25);

2. 虚方法(Virtual Method)

虚方法是可以在派生类中被重写的方法。使用virtual关键字标记。

例子:

public class Animal  
{  
    // 虚方法  
    public virtual void MakeSound()  
    {  
        Console.WriteLine("The animal makes a sound");  
    }  
}  
  
public class Dog : Animal  
{  
    // 重写虚方法  
    public override void MakeSound()  
    {  
        Console.WriteLine("The dog barks");  
    }  
}  
  
// 使用  
Animal animal = new Animal();  
animal.MakeSound(); // 输出 "The animal makes a sound"   
Dog dog = new Dog();  
dog.MakeSound(); // 输出 "The dog barks"

3. 抽象方法(Abstract Method)

抽象方法是在抽象类中声明但没有实现的方法。派生类必须实现所有抽象方法。

例子:

public abstract class Shape  
{  
    // 抽象方法  
    public abstract double GetArea();  
}  
  
public class Rectangle : Shape  
{  
    public double Width { get; set; }  
    public double Height { get; set; }  
  
    // 实现抽象方法  
    public override double GetArea()  
    {  
        return Width * Height;  
    }  
}  
  
// 使用  
Rectangle rectangle = new Rectangle { Width = 5, Height = 10 };  
double area = rectangle.GetArea(); // 计算面积

4. 接口(Interface)

接口定义了一组方法的契约,但不提供实现。实现接口的类必须提供接口中所有方法的实现。

例子:

public interface IComparable  
{  
    int CompareTo(object obj);  
}  
  
public class Person : IComparable  
{  
    public string Name { get; set; }  
  
    // 实现接口方法  
    public int CompareTo(object obj)  
    {  
        if (obj is Person otherPerson)  
        {  
            return Name.CompareTo(otherPerson.Name);  
        }  
        throw new ArgumentException("Object is not a Person");  
    }  
}  
  
// 使用  
Person person1 = new Person { Name = "Alice" };  
Person person2 = new Person { Name = "Bob" };  
int result = person1.CompareTo(person2); // 比较两个Person对象的Name属性

5. 析构方法(Destructor)

析构方法用于在对象被垃圾回收前释放非托管资源。在C#中,析构方法使用~符号标记,并且不能带任何参数。

例子:

public class ResourceHolder  
{  
    // 非托管资源,例如文件句柄、数据库连接等  
    private UnmanagedResource resource;  
  
    public ResourceHolder()  
    {  
        // 初始化资源  
        resource = new UnmanagedResource();  
    }  
  
    // 析构方法  
    ~ResourceHolder()  
    {  
        // 释放资源  
        resource.Dispose();  
    }  
}

6. 扩展方法(Extension Method)

扩展方法允许你为现有的类型添加新的方法,而无需创建新的派生类或者修改原始类型。

例子1:

public static class StringExtensions  
{  
    // 为string类型添加扩展方法  
    public static string ToUpperFirstLetter(this string input)  
    {  
        if (string.IsNullOrEmpty(input))  
        {  
            return input;  
        }  
        return char.ToUpper(input[0]) + input.Substring(1);  
    }  
}  
  
// 使用  
string name = "alice";  
string newName = name.ToUpperFirstLetter(); // 输出 "Alice"

例子2:
在C#中,扩展方法不能直接用于扩展接口。扩展方法允许你为现有的类型(包括类、结构体和枚举)添加新的方法,但是它们不能用于接口,因为接口本身没有实现任何方法或属性,只是定义了一组契约。接口的实现是由具体的类来完成的。

然而,你可以通过扩展实现该接口的类来间接地“扩展”接口。这意味着你可以为实现了某个接口的类添加一个或多个扩展方法,但这些扩展方法并不会成为接口的一部分,它们只是针对实现该接口的类的扩展。

以下是一个例子,展示了如何通过扩展方法间接扩展一个接口:

public interface IMyInterface  
{  
    void DoSomething();  
}  
  
public class MyClass : IMyInterface  
{  
    public void DoSomething()  
    {  
        Console.WriteLine("Doing something...");  
    }  
}  
  
// 为实现了IMyInterface的类添加扩展方法  
public static class MyInterfaceExtensions  
{  
    public static void DoSomethingElse(this IMyInterface myInterface)  
    {  
        Console.WriteLine("Doing something else...");  
    }  
}  
  
// 使用扩展方法  
class Program  
{  
    static void Main(string[] args)  
    {  
        MyClass myClass = new MyClass();  
        myClass.DoSomething(); // 输出 "Doing something..."  
        myClass.DoSomethingElse(); // 输出 "Doing something else..."  
    }  
}

例子3:
在C#中,扩展方法不能直接用于调用密封类(sealed class)或私有类(private class)的成员,因为扩展方法是对现有类型的公开成员的扩展。密封类表示该类不能被继承,而私有类表示该类只能在其声明的作用域内被访问。

对于密封类,由于其不能被继承,扩展方法仍然可以为其添加新的方法,只要这些方法是基于该类的公开成员。然而,扩展方法不能访问密封类的私有或受保护成员。

对于私有类,由于它们只能在声明它们的作用域内被访问,因此你无法在其外部定义扩展方法,因为扩展方法需要定义在可以访问到被扩展类型的作用域之外。

下面是一个关于为密封类添加扩展方法的例子:

// 密封类  
public sealed class SealedClass  
{  
    public void PublicMethod()  
    {  
        Console.WriteLine("PublicMethod called on SealedClass");  
    }  
  
    // 私有方法,扩展方法无法访问  
    private void PrivateMethod()  
    {  
        Console.WriteLine("PrivateMethod called on SealedClass");  
    }  
}  
  
// 为密封类添加扩展方法  
public static class SealedClassExtensions  
{  
    public static void ExtensionMethod(this SealedClass sealedClass)  
    {  
        Console.WriteLine("ExtensionMethod called on SealedClass");  
        sealedClass.PublicMethod(); // 可以调用公开方法  
        // sealedClass.PrivateMethod(); // 错误:无法访问私有方法  
    }  
}  
  
class Program  
{  
    static void Main(string[] args)  
    {  
        SealedClass sealedClassInstance = new SealedClass();  
        sealedClassInstance.ExtensionMethod(); // 调用扩展方法  
    }  
}
  • 9
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

落雪寒窗-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值