这是我的任务:
使用以下准则设计和实现弦乐器类:
乐器的数据字段应包括字符串数,代表字符串名称的字符串名称数组(例如E,A,D,G),以及布尔字段,以确定乐器是否已调音以及乐器当前是否在演奏。如果愿意,欢迎您添加其他数据字段。
构造函数方法,将调整后的当前播放字段设置为false。其他方法
- 调音仪器
- 开始演奏乐器,以及
- 停止演奏乐器。
您认为合适的其他方法(添加至少一个唯一方法)。
使用您选择的图表工具(例如PPT,Visio)创建UML类图表。准备图表并将它们与您的班级简短描述一起放在Word文档中。
为您的仪器创建一个C#类。确保您的代码符合您的设计规范,并且包含一些最小功能。例如,如果调用了violin.play()方法,则至少应打印出小提琴正在演奏。当您停止播放,调谐或调用任何方法时,应提供类似的功能。
到目前为止,我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace StringedMusInsApp
{
public class Guitarra
{
public static void Main (string[] args);
//variable that stores the guitarra's name
private String nameValue;
//variable to store strings
private int numberOfStringsValue;
private char[] stringsValue = { 'E', 'A', 'D', 'G', 'B', 'E' };
//field for tune of the guitar,
private bool tunedValue;
//field for playing of guitar
private bool playingValue;
//method to set tune and playing false.
public Guitarra()
{
this.tunedValue = false;
this.playingValue = false;
}
// gets and sets
public String Name
{
get
{
return nameValue;
}
set
{
nameValue = value;
}
}
public int NumberOfStrings
{
get
{
return numberOfStringsValue;
}
set
{
numberOfStringsValue = value;
}
}
public void DisplayStringValues()
{
Console.WriteLine("String Values: ");
for (int i = 0; i < stringsValue.Length; i++)
{
Console.Write(stringsValue[i] + " ");
}
Console.WriteLine();
}
public bool Tuned
{
get
{
return tunedValue;
}
set
{
tunedValue = value;
}
}
public bool Playing
{
get
{
return playingValue;
}
set
{
playingValue = value;
}
}
//Method to play the violin
public void playGuitar()
{
Console.WriteLine("The guitar is now playing.");
Playing = true;
}
//Method to sto playing the violin
public void stopGuitar()
{
Console.WriteLine("The guitar has stopped playing.");
Playing = false;
}
//Method to tune the Guitar
public void tuneGuitar()
{
Console.WriteLine("The guitar is tuned.");
Tuned = true;
}
//Method to stop tuning the Guitar
public void stopTuneGuitar()
{
Console.WriteLine("The guitar has stopped tuning.");
Tuned = false;
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是我得到这个错误:
错误1'StringedMusInsApp.Guitarra.Main(string [])'必须声明一个主体,因为它没有被标记为抽象,外部或部分。
错误消息告诉您确切的问题所在:
'StringedMusInsApp.Guitarra.Main(string [])'必须声明一个主体,因为它没有被标记为抽象,外部或局部。
查看您的方法声明Main(string[]):
public static void Main (string[] args);
Run Code Online (Sandbox Code Playgroud)
没有方法主体。但是,由于它不是抽象方法,外部方法或部分方法,因此需要方法主体。定义一个:
public static void Main (string[] args)
{
// do something here
}
Run Code Online (Sandbox Code Playgroud)
另请注意,如果您在该方法中不执行任何操作,Main(string[])则您的应用程序将不执行任何操作。它会立即打开和关闭,而无需执行任何代码。该Main(string[])方法是应用程序的入口点。
如果您将应用程序宿主代码(入口点,Main(string[])基本上是执行程序的东西)与逻辑代码(Guitarra类,与您正在做的事情有关的任何业务逻辑)分开,那么这可能会使您更容易构造依此类推)。对于这么小的东西,没有必要将它们分解成自己的程序集或使用DI或类似的东西,但它们至少应该是自己的类。