【C#】基础——继承(Inherit)

C#继承

继承允许我们根据一个类去定义另一个类。当创建一个类(基类)时,不需要完全重新编写新的数据成员和成员函数,只需要设计一个新类(派生类),继承了已有类的成员即可。

ps:有没有一种感觉……看一个字久了、就越来越觉得这个字不像字了……比如——类!!!、

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

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

看一个简单的实列

using System;

namespace Inherit
{
    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 : {0}", Rect.GetArea());

            Console.ReadKey();
            //Console.WriteLine("Hello World!");
        }
    }
}

基类的初始化:

using System;

namespace Inherit
{
  
    class Rectangle
    {
        protected double length;
        protected double width;
        //构造函数
        public Rectangle(double l,double w)
        {
            length = l;
            width = w;
        }
        public double GetArea()
        {
            return length * width;
        }
        public void DisPlay()
        {
            Console.WriteLine("长度 : {0}", length);
            Console.WriteLine("宽度 : {0}", width);
            Console.WriteLine("面积 : {0}", GetArea());
        }
    }

    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;
        }
        public void DisPlay()
        {
            base.DisPlay();
            Console.WriteLine("成本 : {0}", GetCost());
        }
    }

    class Program
    {
        static void Main(string[] args)
        {

            Tabletop t = new Tabletop(4.5, 7.5);
            t.DisPlay();

            Console.ReadKey();
            //Console.WriteLine("Hello World!");
        }
    }
}

C# 多重继承

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

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

using System;
namespace InheritanceApplication
{
   class Shape
   {
      public void setWidth(int w)
      {
         width = w;
      }
      public void setHeight(int h)
      {
         height = h;
      }
      protected int width;
      protected int height;
   }

   // 基类 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 RectangleTester
   {
      static void Main(string[] args)
      {
         Rectangle Rect = new Rectangle();
         int area;
         Rect.setWidth(5);
         Rect.setHeight(7);
         area = Rect.getArea();
         // 打印对象的面积
         Console.WriteLine("总面积: {0}",  Rect.getArea());
         Console.WriteLine("油漆总成本: ${0}" , Rect.getCost(area));
         Console.ReadKey();
      }
   }
}

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值