我收到一个错误:没有给出与ReadingMaterial.By(string)所需的形式参数'x'相对应的参数。我正在寻找有关为什么出错的一些指导。我的语法是否错误,或者我还缺少其他内容?我一直在尝试通过使用一些在线教程来学习C#,并尝试对其进行一些改动。
namespace ReadingMaterials
{
class Presentation
{
static void Main(string[] args)
{
aForm Form = new aForm(); // available in hardcopy form aForm.availForm
Online O = new Online(); // amtBlogs, amtBooks
Book B = new Book(); // hardCover, softCover
Magazine M = new Magazine(); // numArticles
O.blog(5); //contains 5 blogs
O.oBook(3); //contains 3 online books
O.By("Beck"); //Written by Beck
O.words(678); //with 678 combined words
O.pics(1); // with one pic
Console.WriteLine("The auther {0} ", O.By());
Form.availForm();
B.hardCover(10); //10 hardcover books
B.softCover(2); //2 softcover books
B.By("Bruce"); //Writen by Bruce
B.words(188264); //words combined
B.pics(15); //15 pictures
Form.availForm();
M.articles(5); //5 articles
M.By("Manddi"); //Writen by Manddi
M.words(18064);//combined words
M.pics(81); //81 pictures
Form.availForm();
}
}
class ReadingMaterial
{
protected int amtWords;
string author;
protected int pic;
public void words(int w)
{
amtWords = w;
}
public void By(string x)
{
author = x;
}
public void pics(int pi)
{
pic = pi;
}
}
class Online : ReadingMaterial
{
int amtBlogs;
int amtBooks;
public void blog(int s)
{
amtBlogs = s;
}
public void oBook(int b)
{
amtBooks = b;
}
}
class Book : ReadingMaterial
{
int hard;
int soft;
public void hardCover(int h)
{
hard = h;
}
public void softCover(int s)
{
soft = s;
}
}
class Magazine:ReadingMaterial
{
int numArticles;
public void articles(int a)
{
numArticles = a;
}
}
interface IPrintable
{
void availForm();
}
class aForm : IPrintable
{
public void availForm ()
{
Console.WriteLine("Available in hard copy form!");
}
}
}
You have defined the
By
method like this:但是,您尝试不使用参数调用它:
If you want to access the
author
value ofReadingMaterial
here then you need to make it a public property, rather than a private field, or add a method to access it.Console.WriteLine("The auther {0} ", O.By());
is the culprit.O.By()
expects a string, and theBy()
function sets the author, but doesn't return any string. Since you've set the author using theBy()
function, and theauthor
is a public field, you can doConsole.WriteLine("The auther {0} ", O.author);
问题是您在此行中不带任何参数的情况下调用By:
您可以添加一种方法来获取值:
}
您应该使用属性而不是方法: