如果有人指导我,我所做的一切都是正确的,或者帮助我理解上述问题的要求? [英] If someone direct me whatever I have done is correct or help me understand what above question is asking for?
问题描述
我被要求编写一个名为Magic的类。包括私有int成员字段spiders和toads及其属性,包括get和set函数。还包括一个名为do_Magic()的公共void成员函数,它打印出Poooofff。最后包括2个构造函数,一个不带参数的构造函数,并设置toads和spider都等于0;一个接受两个int参数,并设置等于第一个参数的toads和等于第二个参数的spiders。在Main()中,我试图创建一个实例myMagic,并将值3,5分别分配给toads和spiders。
我已经编写了输出Poooofff的代码,但我不知道它是写还是错。我很困惑这个程序的输出是什么?如果有人指导我,我所做的一切都是正确的,或者帮助我理解上述问题的要求?
{
class Magic
{
private int 蜘蛛;
public int 蜘蛛
{
set
{
spiders = value ;
}
获取
{
返回蜘蛛;
}
}
私人 int toads;
public int Toads
{
set
{
toads = value ;
}
get
{
return toads;
}
}
public void do_myMagic( )
{
spiders = 0 ;
toads = 0 ;
Console.WriteLine( Pooofff);
}
public void do_myMagic( int arg1, int arg2)
{
toads = arg1;
spiders = arg2;
}
class 计划
{
静态 < span class =code-keyword> void Main()
{
Magic myMagic = new Magic();
myMagic.spiders = 5 ;
myMagic.toads = 3 ;
myMagic.do_myMagic();
}
}
}
你越来越近但你对构造函数的理解不足。
开始阅读构造函数:
C#中的构造函数介绍 [ ^ ]
do_MyMagic()只需要这样:
public void do_myMagic()
{
Console.WriteLine( Pooofff);
}
你现在需要2个施工人员:
class Magic {
public Magic( )
{
// code
}
public Magic( int iToads, int iSpiders)
{
// code
}
}
我不会做其余的事 - 你似乎很有能力。
然后你就可以写:
Magic myMagic = new Magic( 3 , 5 );
现在加在顶部
使用系统;
命名空间家庭作业
及以下< pre lang =cs> myMagic.do_myMagic();
添加行
Console.ReadKey (); // 点击键 - 停止立即退出。
这应该编译并运行打印出pooff。然后你可以玩set get例如:
Console.WriteLine(myMagic.Spiders);
myMagic.Spiders = 9 ;
Console.WriteLine(myMagic.Spiders);
我同意Bill的评论。
构造函数名称应与Class的名称相同:因此,使用public Magic()
public Magic(int arg1,int arg2)do_myMagic应该是返回Type为void的方法/函数的名称,它只是将字符串值写入您希望输出的任何内容:
public void do_myMagic()
{
/ /你的代码要做什么
}在我的拙见中,无论谁给你这个任务都没有资格教授编程:这是一项愚蠢而无用的任务。
我建议您获得一本关于C#编程的好书,并开始编写能够真正教给你的代码示例。
I am asked to write a class called "Magic." Include private int member fields spiders and toads and their properties, which include get and set functions. Also include a public void member function called do_Magic(), that print out "Poooofff". And finally include 2 Constructors, one that takes no arguments, and sets toads and spider both equal 0; one that takes two int arguments, and sets toads equal to the first argument and spiders equal to the second argument. In Main(), I am trying to create an instance myMagic and assign the values 3,5 to toads and spiders, respectively.
I have wrote the codes that output Poooofff, but I don’t know it that is write or wrong. I am confused what this program’s output would be? If someone direct me whatever I have done is correct or help me understand what above question is asking for?
{
class Magic
{
private int spiders;
public int Spiders
{
set
{
spiders = value;
}
get
{
return spiders;
}
}
private int toads;
public int Toads
{
set
{
toads = value;
}
get
{
return toads;
}
}
public void do_myMagic()
{
spiders= 0;
toads = 0;
Console.WriteLine("Pooofff");
}
public void do_myMagic(int arg1, int arg2)
{
toads = arg1;
spiders = arg2;
}
class Program
{
static void Main()
{
Magic myMagic = new Magic();
myMagic.spiders = 5;
myMagic.toads = 3;
myMagic.do_myMagic();
}
}
}
You are getting close but your understanding of constructors is deficient.
To begin read up on constructors:
An Intro to Constructors in C#[^]
do_MyMagic() only needs this:
public void do_myMagic() { Console.WriteLine("Pooofff"); }
You now need 2 constructors:
class Magic { public Magic() { // code } public Magic(int iToads, int iSpiders) { // code } }
I won't do the rest - you seem quite capable of that.
Then you will be able to write:
Magic myMagic = new Magic(3, 5);
[EDIT] Now add at the top
using System; namespace Homework
And belowmyMagic.do_myMagic();
add the line
Console.ReadKey(); // Hit key - Stops from exiting immediately.
This should compile and run printing out "pooff". Then you can play around with set get eg:
Console.WriteLine(myMagic.Spiders); myMagic.Spiders = 9; Console.WriteLine(myMagic.Spiders);
I agree with Bill's comments.
Constructor names should be the same name as the Class: so, usepublic Magic() public Magic(int arg1, int arg2)do_myMagic should be the name of a method/function with return Type 'void that simply writes the string value to whatever you wish to output it to:
public void do_myMagic() { // your code to do whatever }In my humble opinion, whoever gave you this "task" is unqualified to teach programming: it is a silly, and useless, assignment.
I recommend you get a good book on C# programming, and start working on code examples that will really teach you something.
这篇关于如果有人指导我,我所做的一切都是正确的,或者帮助我理解上述问题的要求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!