c# 继承 new,base的使用

在C#中,继承是指一个类(称为派生类或子类)从另一个类(称为基类或父类)继承属性和方法的机制。继承可以帮助我们重用代码并构建层次化的类结构。要使用继承,在定义类时使用冒号(:)指定其基类。下面是一个简单的C#继承示例:

// 定义基类
public class Animal
{
   
    public void Eat()
    {
   
        Console.WriteLine("The animal is eating.");
    }

    public void Sleep()
    {
   
        Console.WriteLine("The animal is sleeping.");
    }
}

// 定义派生类
public class Dog : Animal
{
   
    public void Bark()
    {
   
        Console.WriteLine("The dog is barking.");
    }
}

// 在程序中使用
class Program
{
   
    static void Main()
    {
   
        Dog dog = new Dog();
        dog.Eat(); // 可以调用基类的方法
        dog.Sleep(); // 可以调用基类的方法
        dog.Bark(); // 可以调用派生类自己的方法
    }
}

在上面的示例中,类Animal是基类,类Dog是派生类。派生类Dog继承了基类Animal的Eat()和Sleep()方法,并且还拥有自己的Bark()方法。在程序中可以创建Dog的实例,并调用继承的方法以及自己的方法。


在C#中,在子类中使用`new`关键字可以隐藏基类中的成员。这通常发生在子类中重新定义了一个与基类中同名的成员时。使用`new`关键字可以显式地指定要隐藏基类成员的方法或属性。在这种情况下,基类成员仍然存在,但在子类中使用`new`关键字定义的成员将会隐藏基类成员。

下面是一个简单的示例,演示了如何在C#中使用new关键字:

// 定义基类
public class Animal
{
   
    public void Eat()
    {
   
        Console.WriteLine("The animal is eating.");
    }
}

// 定义派生类
public class Dog : Animal
{
   
    public new void Eat()
    {
   
        Console.WriteLine("The dog is eating.");
    }
}

// 在程序中使用
class Program
{
   
    static void Main()
    {
   
        Dog dog = new Dog();
        dog.Eat(); // 输出:The dog is eating.
        
        // 通过基类引用调用基类的方法
        Animal animal = new Dog();
        animal.Eat(); // 输出:The animal is eating.
    }
}

在上面的示例中,派生类Dog重新定义了基类Animal中的Eat()方法,并且使用了new关键字。当创建Dog类的实例并调用Eat()方法时,输出的结果是"The dog is eating.“,而不是"The animal is eating.”。这表明派生类中使用了new关键字,隐藏了基类中的Eat()方法。

需要注意的是,基类成员仍然存在,但当通过派生类实例调用时,将会调用派生类中重新定义的成员。而当通过基类引用调用时,将会调用基类中的成员。


在 C# 中,使用 `base` 关键字可以在派生类中访问基类的成员或调用基类的构造函数。
  1. 访问基类的成员:
    使用 base 关键字可以在派生类中访问基类中被隐藏的成员。例如,如果在派生类中重新定义了与基类中同名的成员,可以通过 base 关键字访问基类中的成员。以下是一个简单的示例:

    // 定义基类
    public class Animal
    {
         
        public void Eat()
        {
         
            Console.WriteLine("The animal is eating.");
        }
    }
    
    // 定义派生类
    public class Dog : Animal
    {
         
        public void Eat()
        {
         
            base.Eat(); // 访问基类的 Eat() 方法
            Console.WriteLine("The dog is also eating.");
        }
    }
    
    // 在程序中使用
    class Program
    {
         
        static void Main()
        {
         
            Dog dog = new Dog();
            dog.Eat();
            // 输出:
            // The animal is eating.
            // The dog is also eating.
        }
    }
    

    在上面的示例中,派生类Dog中的Eat()方法通过 base.Eat() 访问基类Animal中的Eat()方法。

  2. 调用基类的构造函数:
    使用 base 关键字还可以在派生类的构造函数中调用基类的构造函数。以下是一个简单的示例:

    // 定义基类
    public class Animal
    {
         
        public Animal(string name)
        {
         
            Console.WriteLine($"Creating an animal named {
           name}");
        }
    }
    
    // 定义派生类
    public class Dog : Animal
    {
         
        public Dog(string name) : base(name)
        {
         
            Console.WriteLine("Creating a dog");
        }
    }
    
    // 在程序中使用
    class Program
    {
         
        static void Main()
        {
         
            Dog dog = new Dog("Buddy");
            // 输出:
            // Creating an animal named Buddy
            // Creating a dog
        }
    }
    

    在上面的示例中,派生类Dog的构造函数中使用了 base(name) 调用基类Animal的构造函数,以便在创建Dog对象时也能对基类进行初始化。

总之,base 关键字在 C# 中用于访问基类的成员或调用基类的构造函数,以便在派生类中能够与基类进行交互。

相关推荐

  1. c# 继承 new,base使用

    2024-01-24 22:34:01       53 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-24 22:34:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-24 22:34:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-24 22:34:01       87 阅读
  4. Python语言-面向对象

    2024-01-24 22:34:01       96 阅读

热门阅读

  1. LightDB - oracle_fdw join下推增强【24.1】

    2024-01-24 22:34:01       50 阅读
  2. oracle 字符集

    2024-01-24 22:34:01       58 阅读
  3. 为什么写进MySQL里的数据顺序乱了?

    2024-01-24 22:34:01       63 阅读
  4. 2765. 最长交替子数组 ( leetcode 01 - 23 每日 )

    2024-01-24 22:34:01       55 阅读
  5. C++从零开始的打怪升级之路(day19)

    2024-01-24 22:34:01       57 阅读
  6. mybatis多字段模糊查询

    2024-01-24 22:34:01       61 阅读
  7. RHEL8安装Oracle 19c软件runInstaller报错

    2024-01-24 22:34:01       63 阅读
  8. kafka安装

    2024-01-24 22:34:01       62 阅读
  9. python scapy抓包获取udp并转发

    2024-01-24 22:34:01       37 阅读