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



我是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 operatorr");
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(基类(的方法,因为在 BaseClass 中,您没有参数较少的构造函数。

溶液:

  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的类级别值都将是默认值。 您必须在构造对象后显式设置它们。1
  2. 将构造函数
  3. 添加到接受这些值并将其传递给父构造函数或至少将默认值传递给父构造函数的构造函数MyCalc2
  4. 不要在此处使用继承。

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

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


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

这是一个可能的解决方案。有两个类MyClass,用于计算器(你可能想要重命名它(和Propram。程序只包含方法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 operatorr");
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();
}
}

}

最新更新