zoukankan      html  css  js  c++  java
  • c# 02.1 继承 子类给父类传参

    补充一点子类调用父类构造器

    子类

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Text;
    
    namespace ConsoleApp1 
    {
        /// <summary>
        /// 胡萝卜类
        /// </summary>
         class Carrot : Vegetables
        {
            public static void Main(string[] args)
            {
                Carrot c = new Carrot("aaa");
            }
    
            //public Carrot() { }
    
            public Carrot(string var) : base(var) { }//将传给子类构造器的参数传给父类构造器
           
        }
    }

    父类

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace ConsoleApp1
    {
        /// <summary>
        /// 蔬菜类
        /// </summary>
        class Vegetables
        {
    
            /// <summary>
            /// 维生素
            /// </summary>
            private string _vitamin;
    
            public string Vitamin { get => _vitamin; set => _vitamin = value; }
    
    
            public Vegetables(string var) {
                this.Vitamin = var;
                Console.WriteLine("调到我这儿来了并且传的参数是:{0}",var);
            }
    
            //public Vegetables() { }
        }
    }

    结果:

  • 相关阅读:
    1
    iulg
    实验10
    作业5 指针应用
    作业4 函数应用
    实验9 指针
    实验 8 数组2
    实验7
    实验6 数组1
    实验5
  • 原文地址:https://www.cnblogs.com/li-yan-long/p/14002128.html
Copyright © 2011-2022 走看看