簡體   English   中英

C# 中的 Inheritance 問題:CS7036

[英]Inheritance Issue in C# : CS7036

出於某種原因,我在構造函數方法“ModifiedExercise”CS7036 中遇到錯誤:沒有給出與所需的形式參數name對應的參數。Exercise Exercise.Exercise(string,int,int,string,string[],Dictionary<string,bool>)我添加了 Parent 和 child 類ExerciseModifiedExercise

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

namespace Trainer_App
{
    internal class ModifiedExercise : Exercise 
    {
        private Exercise regular_exercise;
        public ModifiedExercise(Exercise regular_exercise,
                                string name,
                                int min_num_balls,
                                int participants_num,
                                string court_type,
                                string[] accessories,
                                Dictionary<string, bool> exerciseTypes)
        {
            this.regular_exercise = regular_exercise;
            this.id = Exercise.id_counter;
            Exercise.id_counter++;
            this.name = name;
            this.min_num_balls = min_num_balls;
            this.participants_num = participants_num;
            this.court_type = court_type;
            this.accessories = accessories;
            this.exerciseTypes = exerciseTypes;

        }
    }
}

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

namespace Trainer_App
{
    internal class Exercise
    {
        protected static int id_counter = 0;
        protected int id;
        protected string name;
        protected int min_num_balls;
        protected int participants_num;
        protected string court_type;
        protected string[] accessories;
        protected static int exerciseCounter = 0;
        protected Dictionary<string, bool> exerciseTypes = new Dictionary<string, bool>();

        public Exercise(string name,int min_num_balls, int participants_num,string court_type, string[]accessories,Dictionary<string,bool> exerciseTypes)
        {
            this.id = id_counter;
            id_counter++;
            this.name = name;
            this.min_num_balls = min_num_balls;
            this.participants_num = participants_num;
            this.court_type = court_type;
            this.accessories = accessories;
            this.exerciseTypes = exerciseTypes;
        }



    }
}

默認情況下,構造函數在其基本類型上有效地調用: base()

您在這里有兩個選擇:

  1. protected Exercise(){}無參數構造函數添加到基類型
  2. 添加顯式: base(...)顯式指定將哪個值傳遞到哪個 position 到基本構造函數

注:2是通常的做法,減少重復。 這也允許字段保持private

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM