我是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();
}
}
答案 0 :(得分:0)
MyCalc2没有初始化MyCalc(基类)的方法,因为在您的BaseClass中,没有参数较少的构造函数。
解决方案:
下面的代码应该可以工作:
class MyCalc2 : MyCalc
{
public MyCalc2 () : base(0, 0, "", "")
{
}
}
答案 1 :(得分:0)
MyCalc2
没有显式的构造函数。这意味着它只有一个 implicit 构造函数,该构造函数不带参数且不设置值。如果进行了明确显示,它将如下所示:
public MyCalc2()
{
}
但是,MyCalc
确实具有显式构造函数。这意味着它具有 no 隐式构造函数。并且其构造函数确实接受参数:
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
提供任何值。您基本上有三个选择:
MyCalc
添加一个构造函数(只要参数不同,您可以具有任意数量的构造函数),该构造函数不带参数。但是,在那种情况下,MyCalc
的类级别值将全部为默认值。构造对象后,必须显式设置它们。 1 MyCalc2
中添加一个构造函数,以接受这些值并将其传递给父构造函数,或者至少将默认值传递给父构造函数。老实说,在这种情况下,我会选择第三个选项。继承在这里意味着什么? MyCalc2
并不是MyCalc
的实例。它所做的所有事情都保留了应用程序的初始入口点(Main
方法),而这实际上就是它应该做的所有事情。
您的Main
方法中的逻辑应创建并使用MyCalc
的实例,但是具有该Main
方法的类不应尝试成为 MyCalc
的实例。比解决任何有意义的问题只会引起更多的混乱。
1 注意:从历史上看,公共类字段是进入面向对象编程的一种坏习惯。关于该主题的讨论多种多样,随着您继续体验,您会经常看到这一点。通常,您希望对象公开行为,而不是值。按照惯例,对象上的方法看上去有点像Java。对于C#约定,请考虑使用属性(可编译为方法本身,语法在语义上有所不同)。您可以为值本身提供{ get; set; }
自动属性,并为计算值提供显式只读{ get { /*...*/ } }
属性。
答案 2 :(得分:0)
这是一个可能的解决方案。 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 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();
}
}
}