繁体   English   中英

简单计算器的 C# 类继承问题

[英]C# Class Inheritance Issue with Simple Calculator

我是 C# 的初学者,并试图让我的第二个类 MyCalc2 继承自 MyCalc。 但我遇到以下有关 MyCalc2 的错误消息:

没有给出对应于 'MyCalc.MyCalc(int, int, string, string)' 的所需形式参数 'x' 的参数

这里的目标是添加另一个继承自基类的类。

我知道我需要在我的基类中添加诸如“MyCalc: base(x)”之类的东西,但是我不知道在哪里放置参数(如果这甚至是正确的做法)。 任何指导将不胜感激。 这是我到目前为止所拥有的:

    using System;
class MyCalc
{
    // class variable
    public int x;
    public int z;
    public string y;
    public string n;

    // constructor
    public MyCalc(int x, int z, string y, string n)
    {
        this.x = x;  // assign the parameter passed to the class variable
        this.z = z;
        this.y = y;
        this.n = n;

    }
    // calculate the operations
    public int GetAdd()
    {
        return (this.x + this.z);

    }

    public int GetSubtract()
    { 
        return (this.x - this.z);
    }

    public int GetMultiply()
    {
        return (this.x * this.z);
    }

    public int GetDivide()
    {
        return (this.x / this.z);
    }

    public string GetYes()
    {
        return (this.y);
    }

    public string GetNo()
    {
        return (this.n);
    }

}

class MyCalc2:MyCalc //where the error is occurring 
{
    static void Main(string[] args)



    {
        bool repeat = false;
        do
        {

            repeat = false;


            int x = 0; int z = 0; string y; string n;
            Console.WriteLine("Enter the First Number");
            x = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Enter the Second Number");
            z = Convert.ToInt32(Console.ReadLine());



            //Using a switch statement to perform calculation:
            Console.WriteLine("Enter operator\r");
            switch (Console.ReadLine())


            {

                case "+":
                    Console.WriteLine($"The Answer is: {x} + {z} = " + (x + z));
                    break;

                case "-":
                    Console.WriteLine($"The Answer is: {x} - {z} = " + (x - z));
                    break;
                case "*":
                    Console.WriteLine($"The Answer is: {x} + {z} = " + (x + z));
                    break;

                case "/":
                    Console.WriteLine($"The Answer is: {x} - {z} = " + (x - z));
                    break;


            }



                //Repeat or Exit program using the do-while loop:

            string input = Console.ReadLine();
            Console.WriteLine("Do you want another operation(Y / N) ?");
            input = Console.ReadLine();
            repeat = (input.ToUpper() == "Y");

        }
            while (repeat);
            Console.WriteLine("Thanks for using our system.");
            Console.ReadKey();
        }

    }

MyCalc2 没有初始化 MyCalc(基类)的方法,因为在您的基类中,您没有无参数构造函数。

解决方案:

  1. 在基类中添加一个无参数构造函数
  2. 在派生类中添加一个构造函数,它可以调用基类构造函数

对于下面的代码应该可以工作:

class MyCalc2 : MyCalc
{
    public MyCalc2 () : base(0, 0, "", "")
    {
    }
}

MyCalc2没有显式构造函数。 这意味着它只有一个不带参数也不设置值的隐式构造函数。 如果明确表示,它将如下所示:

public MyCalc2()
{

}

但是, MyCalc确实有一个显式构造函数。 这意味着它没有隐式构造函数。 它的构造函数确实接受参数:

public MyCalc(int x, int z, string y, string n)
{
    this.x = x;  // assign the parameter passed to the class variable
    this.z = z;
    this.y = y;
    this.n = n;
}

因此,当您创建MyCalc2的实例时,它无法向MyCalc提供任何值。 您基本上有三个选择:

  1. MyCalc添加一个不带参数的构造MyCalc (只要参数不同,您可以拥有任意数量的构造函数)。 但是,在这种情况下, MyCalc的类级别值MyCalc将是默认值。 您必须在构造对象后显式设置它们。 1
  2. MyCalc2添加一个构造函数,它接受这些值并将它们传递给父构造函数,或者至少将默认值传递给父构造函数。
  3. 不要在这里使用继承。

老实说,在这种情况下,我会选择第三个选项。 继承在这里的意义是什么? MyCalc2是不是有意义的一个实例MyCalc 它所做的一切都保存了应用程序的初始入口点( Main方法),这就是它真正应该做的。

Main方法中的逻辑应创建并使用MyCalc的实例,但具有该Main方法的类不应尝试成为MyCalc的实例。 这只会造成比解决任何有意义的问题更多的混乱。


1旁注:从历史上看,公共类字段是面向对象编程中的一个坏习惯。 关于这个主题有各种各样的讨论,当您继续体验时,您会经常看到这种情况。 通常,您希望对象公开行为,而不是 对象上的方法在约定上看起来有点像 Java。 对于 C# 约定,请考虑使用属性(编译为方法本身,语法只是语义不同)。 你可以有{ get; set; } { get; set; } { get; set; }值本身的自动属性,以及计算值的显式只读{ get { /*...*/ } }属性。

这是一个可能的解决方案。 有两个类 MyClass,用于计算器(您可能想要重命名)和 Propram。 Program 只包含 Main 方法,它可以启动您的程序。 它是这样工作的,但还有一些错误。 我把它留给你来修复它们。 除了你对类和继承的概念没有清晰的理解之外,你的代码对于初学者来说还不错。 它几乎可以工作了。

using System;

namespace TestCalculator
  {
 class MyCalc
{
// class variable
public int x;
public int z;
public string y;
public string n;

// constructor
public MyCalc(int x, int z, string y, string n)
  {
  this.x = x;  // assign the parameter passed to the class variable
  this.z = z;
  this.y = y;
  this.n = n;

  }
// calculate the operations
public int GetAdd()
  {
  return (this.x + this.z);

  }

public int GetSubtract()
  {
  return (this.x - this.z);
  }

public int GetMultiply()
  {
  return (this.x * this.z);
  }

public int GetDivide()
  {
  return (this.x / this.z);
  }

public string GetYes()
  {
  return (this.y);
  }

public string GetNo()
  {
  return (this.n);
  }

}
 class Program
{
static void Main(string[] args)
  {
  bool repeat = false;
  do
    {

    repeat = false;


    int x = 0; int z = 0; string y; string n;
    Console.WriteLine("Enter the First Number");
    x = Convert.ToInt32(Console.ReadLine());

    Console.WriteLine("Enter the Second Number");
    z = Convert.ToInt32(Console.ReadLine());



    //Using a switch statement to perform calculation:
    Console.WriteLine("Enter operator\r");
    switch (Console.ReadLine())


      {

      case "+":
        Console.WriteLine($"The Answer is: {x} + {z} = " + (x + z));
        break;

      case "-":
        Console.WriteLine($"The Answer is: {x} - {z} = " + (x - z));
        break;
      case "*":
        Console.WriteLine($"The Answer is: {x} + {z} = " + (x + z));
        break;

      case "/":
        Console.WriteLine($"The Answer is: {x} - {z} = " + (x - z));
        break;


      }



    //Repeat or Exit program using the do-while loop:

    string input = Console.ReadLine();
    Console.WriteLine("Do you want another operation(Y / N) ?");
    input = Console.ReadLine();
    repeat = (input.ToUpper() == "Y");

    }
  while (repeat);
  Console.WriteLine("Thanks for using our system.");
  Console.ReadKey();
  }
}

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM