zoukankan      html  css  js  c++  java
  • C# this和base

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Audi audi = new Audi("linQ",60);
                audi[1] = "A6";
                audi[2] = "A8";
                Console.WriteLine(audi[1]);
                audi.Run();
                audi.ShowResult();
                Console.ReadLine();
            }
        }
        public class Action
        {
            public static void ToRun(Vehicle vehicle)
            {
                Console.WriteLine("{0} is running.", vehicle.ToString());
            }
        }
        public class Vehicle
        {
            private string name;
            private int speed;
            private string[] array = new string[10];

            public Vehicle()
            {
            }
            //限定被相似的名称隐藏的成员
            public Vehicle(string name, int speed)
            {
                this.name = name;
                this.speed = speed;
            }
            public virtual void ShowResult()
            {
                Console.WriteLine("The top speed of {0} is {1}.", name, speed);
            }
            public void Run()
            {
                //传递当前实例参数
                Action.ToRun(this);
            }
            //声明索引器,必须为this,这样就可以像数组一样来索引对象
            public string this[int param]
            {
                get { return array[param]; }
                set { array[param] = value; }
            }
        }
        public class Car : Vehicle
        {
            //派生类和基类通信,以base实现,基类首先被调用
            public Car()
                : base("Car", 200)
            { }

            //指定创建派生类实例时应调用的基类构造函数
            public Car(string name, int speed)
                : this()
            { }

            public override void ShowResult()
            {
                //调用基类上已被其他方法重写的方法
                base.ShowResult();
                Console.WriteLine("It's a car's result.");
            }
        }
        public class Audi : Car
        {
            public Audi()
                : base("Audi", 300)
            { }

            public Audi(string name, int speed)
                : this()
            {
            }
            public override void ShowResult()
            {
                //由三层继承可以看出,base只能继承其直接基类成员
                base.ShowResult();
                base.Run();
                Console.WriteLine("It's audi's result.");
            }

        }


    }

  • 相关阅读:
    树状数组与线段树基础
    阿里云服务器低价购买及域名解析
    ENVI5.3安装教程(含软件下载)
    05 ArcGIS JS API 4.12版本监听view-change事件
    jQuery的AJAX请求成功,但是跳转到error的解决方法
    04 ArcGIS JS API 4.12加载天地图
    02 Portal for ArcGIS 10.7安装部署教程(linux环境)
    10 webpack4.0学习笔记——热替换_devServer
    09 webpack4.0学习笔记——配置文件_HtmlWebpackPlugin使用
    08 webpack4.0学习笔记——配置文件_DefinePlugin使用
  • 原文地址:https://www.cnblogs.com/woshilulin/p/3720365.html
Copyright © 2011-2022 走看看