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

    继承是面向对象程序设计中重要的概念之一,C#作为面向对象的语言也支持继承,

    其中被继承的类叫做基类,新的类叫做派生类

    与java使用“extends”关键字不同的是,C#使用“ : ”来实现继承

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

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

    例:

    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;
       }
    
       // 派生类
       class Rectangle: Shape
       {
          public int getArea()
          {
             return (width * height);
          }
       }
       
       class RectangleTester
       {
          static void Main(string[] args)
          {
             Rectangle Rect = new Rectangle();
    
             Rect.setWidth(5);
             Rect.setHeight(7);
    
             // 打印对象的面积
             Console.WriteLine("总面积: {0}",  Rect.getArea());
             Console.ReadKey();
          }
       }
    }

    在C#中也有与java中super关键字作用差不多的一个关键字------>base

    代码举例:

    using System;
    namespace RectangleApplication
    {
       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());
          }
       }//end class 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;
          }
          public void Display()
          {
             base.Display();
             Console.WriteLine("成本: {0}", GetCost());
          }
       }
       class ExecuteRectangle
       {
          static void Main(string[] args)
          {
             Tabletop t = new Tabletop(4.5, 7.5);
             t.Display();
             Console.ReadLine();
          }
       }
    }

    多重继承指的是一个类别可以同时从多个父类继承行为与特征的功能。与单一继承相对,单一继承指一个类别只可以继承自一个父类,但是C#与java一样同样不支持多继承,但是可以使用接口来实现多继承

    代码如下:

    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();
          }
       }
    }
  • 相关阅读:
    富人和穷人的区别(转)
    asp.net主题的几种应用
    asp.net主题的几种应用
    富人和穷人的区别(转)
    SQL SERVER中一些常见性能问题的总结
    SQL SERVER中一些常见性能问题的总结
    Bind和Eval的区别详解
    优酷去广告代码
    使用jquery框架导致js功能失效解决办法
    absolute定位问题
  • 原文地址:https://www.cnblogs.com/xing-29391/p/14581361.html
Copyright © 2011-2022 走看看