C#中的继承与多态

继承

继承是为了实现程序的复用,将父类的功能传递给子类,子类就可以省去重新定义的工作。

这一概念其实也跟面向对象紧密相关。面向对象就是将对象抽象出来,而不同的抽象层级就会得到不同的类。比如,大学生——学生——人——动物——生物,就是不断抽象的过程。越往上抽象,独有属性就越笼统,越少;越往下抽象就越具体,属性也就越丰富;而且下层的属性中必然包涵着上层的属性。大学生肯定有学生的属性,所以c#中的继承就是把上层的属性拿到下层来使用。程序可以这样使用:

  public class Person
    {
        public string Name;
        public int Age;
        public void Eat()
        {
            //吃
        }
        public void Move()
        {
            //移动
        }
    }
    public class Student : Person
    {
        public string StudentNumber;
        public string Grade;
        public void Study()
        {
            //学习
        }
    }

    //测试
    public class test
    {
        private void main()
        {
            Student stu = new Student();
            stu.StudentNumber = "202201220301";
            stu.Study();
            /
  • 3
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
封装、继承多态是面向对象编程的三个重要概念。下面是一个使用C#语言演示封装、继承多态的代码示例: 封装示例: ```csharp using System; namespace EncapsulationApplication { class Employee { private string name; private int age; public string Name { get { return name; } set { name = value; } } public int Age { get { return age; } set { age = value; } } public void Display() { Console.WriteLine("Name: " + name); Console.WriteLine("Age: " + age); } } class Program { static void Main(string[] args) { Employee emp = new Employee(); emp.Name = "John"; emp.Age = 30; emp.Display(); Console.ReadKey(); } } } ``` 在这个示例,Employee类封装了私有字段name和age,并通过公共属性Name和Age提供对它们的访问。Main方法创建了一个Employee对象emp,并通过属性设置和Display方法展示了封装的效果。 继承示例: ```csharp using System; namespace InheritanceApplication { class Shape { protected int width; protected int height; public void setWidth(int w) { width = w; } public void setHeight(int h) { height = h; } } class Rectangle : Shape { public int getArea() { return width * height; } } class Program { static void Main(string[] args) { Rectangle rect = new Rectangle(); rect.setWidth(5); rect.setHeight(7); Console.WriteLine("Area: " + rect.getArea()); Console.ReadKey(); } } } ``` 在这个示例,Shape类作为基类,Rectangle类继承了Shape类。Rectangle类可以访问Shape类的protected字段和方法。Main方法创建了一个Rectangle对象rect,并通过调用setWidth、setHeight和getArea方法展示了继承的效果。 多态示例: ```csharp using System; namespace PolymorphismApplication { class Shape { public virtual void Draw() { Console.WriteLine("Drawing a shape"); } } class Circle : Shape { public override void Draw() { Console.WriteLine("Drawing a circle"); } } class Rectangle : Shape { public override void Draw() { Console.WriteLine("Drawing a rectangle"); } } class Program { static void Main(string[] args) { Shape[] shapes = new Shape[3]; shapes[0] = new Shape(); shapes[1] = new Circle(); shapes[2] = new Rectangle(); foreach (Shape shape in shapes) { shape.Draw(); } Console.ReadKey(); } } } ``` 在这个示例,Shape类定义了一个虚拟的Draw方法,Circle类和Rectangle类分别重写了这个方法。Main方法创建了一个Shape类型的数组,并分别用Shape、Circle和Rectangle的实例填充数组。通过遍历数组并调用Draw方法,展示了多态的效果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

不知今夕何夕

重赏之下必有勇夫

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

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

打赏作者

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

抵扣说明:

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

余额充值