• 18-C#笔记-继承


    1. 子类可以使用父类的成员和函数。

    和C++不同,使用的是一个冒号

    2. 不支持多重继承

    但是可以通过接口(interface)这种结构实现。后续讲解。

    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();
          }
       }
    }
    

      

    参考:

    http://www.runoob.com/csharp/csharp-inheritance.html

  • 相关阅读:
    Change MYSQL data directory
    Docker distrubution in django
    mongo&node
    理解Docker单机容器网络
    auto dock
    django_restframework_angularjs
    Javascript异步编程的4种方法
    DockerProblem
    Javascript面向对象编程
    scrapy post请求 multipart/form-data
  • 原文地址:https://www.cnblogs.com/alexYuin/p/9069260.html
Copyright © 2020-2023  润新知