zoukankan      html  css  js  c++  java
  • C# 继承

    C# 继承

    继承是面向对象程序设计中最重要的概念之一。继承允许我们根据一个类来定义另一个类,这使得创建和维护应用程序变得更容易。同时也有利于重用代码和节省开发时间。

    当创建一个类时,程序员不需要完全重新编写新的数据成员和成员函数,只需要设计一个新的类,继承了已有的类的成员即可。这个已有的类被称为的基类,这个新的类被称为派生类

    继承的思想实现了 属于(IS-A) 关系。例如,哺乳动物 属于(IS-A) 动物,狗 属于(IS-A) 哺乳动物,因此狗 属于(IS-A) 动物。

    基类和派生类

    一个类可以派生自多个类或接口,这意味着它可以从多个基类或接口继承数据和函数。

    C# 中创建派生类的语法如下:

    <访问修饰符符> class <基类>
    {
     ...
    }
    class <派生类> : <基类>
    {
     ...
    }

     假如一个基类是Shape,一个派生类是Rectangle。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace week2._1
    {    
        // 基类
        class Shape
        {     
            public void setLength(int len)
            {
                length = len;
            }
            public void setWidth(int wid)
            {
                width = wid;
            }
            protected int length;
            protected int width;
        }
        //派生类
        class Rectangle : Shape
        {
            private int height;
            public int getArea()
            {
                return length * width;
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                //创建对象
                Rectangle rect = new Rectangle();
                rect.setLength(5);
                rect.setWidth(6);
                
                Console.WriteLine(rect.getArea());
                Console.ReadLine();
            }
        }
    }

     结果:

    30

    基类的初始化

    派生类继承了基类的成员变量和成员方法。因此父类对象应在子类对象创建之前被创建。您可以在成员初始化列表中进行父类的初始化。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace week2._1
    {
        class Rectangle
        {
            //成员变量
            protected double width;
            protected double lenght;
            //构造方法
            public Rectangle(double l, double w)
            {
                width = w;
                lenght = l;
            }
            //面积
            public  double GetArea()
            {
                return width *lenght;
            }
            //显示
            public void Display()
            {
                Console.WriteLine("长度:{0}", lenght);
                Console.WriteLine("宽度:{0}", width );
                Console.WriteLine("面积:{0}", GetArea());
            }
            //继承Rectangle
            class Tabletop : Rectangle
            {
                private double cost;
                public Tabletop(double l,double w) : base(l, w)
                {
                }
                public double Getcost()
                {
                    double cost;
                    cost = GetArea() * 70;
                    return cost;
                }
                //重写Display方法
                public void Display()
                {
    //base.Display(); Console.WriteLine(
    "成本:{0}", Getcost()); } } class Test { static void Main(string[] args) { Tabletop t = new Tabletop(4.5, 5.5); t.Display(); Console.ReadKey(); } } } }

    结果:

    成本:1732.5

    C# 多重继承

    多重继承指的是一个类别可以同时从多于一个父类继承行为与特征的功能。与单一继承相对,单一继承指一个类别只可以继承自一个父类。

    C# 不支持多重继承。但是,您可以使用接口来实现多重继承。下面的程序演示了这点:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace week2._1
    {
        class Shape
        {
            protected int  width;
            protected int  height;
            public void setWidth(int w)
            {
                width = w;
            }
            public void setHeight(int h)
            {
                height = h;
            }
        }
        //基类PaintCost
        public interface PaintCost
        {
            int getCost(int area);
        }
        //派生类
        class Rectangle: Shape, PaintCost
        {
            public int getArea()
            {
                return width * height;
            }
            public int getCost(int area)
            {
                return area * 70;
            }
     
        }
    
        class test
        {
            static void Main(string[] args)
            {
                Rectangle r = new Rectangle();
                int area;
                r.setHeight(7);
                r.setWidth(8);
                area = r.getArea();
                Console.WriteLine(area);
                Console.WriteLine(r.getCost(area));
                Console.ReadKey();
            }
        }
        
    }

    结果:

    56
    3920

    关于继承的几点:

    •  1、继承的语法:class 子类类名 : class 父类类名{ //子类类体 }
    •  2、继承的特点:子类拥有所有父类中所有的字段、属性和方法
    •  3、一个类可以有多个子类,但是父类只能有一个
    •  4、一个类在继承另一个类的同时,还可以被其他类继承
    •  5、在 C# 中,所有的类都直接或者间接的继承自 Object 类
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace week2._1
    {
        class Program
        {
            class father
            {
                public father()
                {
                    Console.WriteLine("father");
                }
            }
            class son : father
            {
                public son()
                {
                    Console.WriteLine("son");
                }
            }
            static void Main(string[] args)
            {
                son s = new son();
                Console.ReadKey();
            }
        }
    }

    结果:

    father
    son
  • 相关阅读:
    感到工作效率低?是因为你缺少这套“外贸企业邮箱”办公软件
    用企业邮箱的好处,企业邮箱手机微信使用
    公司企业邮箱注册,公司为什么用企业邮箱?
    中小企业公司邮箱一般用哪个?申请企业邮箱的流程
    公司企业邮箱注册,企业邮箱怎么办理
    array_replace — 使用传递的数组替换第一个数组的元素
    array_replace_recursive — 使用传递的数组递归替换第一个数组的元素
    array_reduce — 用回调函数迭代地将数组简化为单一的值
    array_rand — 从数组中随机取出一个或多个单元
    array_push — 将一个或多个单元压入数组的末尾(入栈)
  • 原文地址:https://www.cnblogs.com/jiangzijiang/p/13352737.html
Copyright © 2011-2022 走看看