方法必须声明一个主体吗? [英] Method must declare a body?

查看:28
本文介绍了方法必须声明一个主体吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的任务:

使用以下准则:

乐器的数据字段应包括字符串的数量、表示字符串名称的字符串名称数组(例如 E、A、D、G),以及布尔字段,以确定仪器是否已调谐,以及乐器正在演奏.欢迎您补充如果您愿意,可以使用数据字段.

Data fields for your instrument should include number of strings, an array of string names representing string names (e.g. E, A, D, G), and boolean fields to determine if the instrument is tuned, and if the instrument is currently playing. You are welcome to add additional data fields if you like.

一个构造器方法,用于设置已调整的和当前正在播放的字段为假.其他方法

A constructor method that set the tuned and currently playing fields to false. Other methods

  1. 调整乐器,
  2. 开始演奏乐器,以及
  3. 停止乐器演奏.

您认为合适的其他方法(添加至少一种独特的方法).

Other methods as you see fit (add at least one unique method).

使用图表工具(例如 PPT、Visio)创建 UML 类图你的选择.准备图表并将它们放在 Word 文档中以及对您的课程的简要说明.

Create a UML class diagram using a diagram tool (e.g. PPT, Visio) of your choice. Prepare the diagrams and place them in a Word document along with a brief description of your class.

为您的乐器创建一个 C# 类.确保您的代码匹配包括您的设计规范和一些最小功能.例如,如果您调用 violin.play() 方法,您应该在至少打印小提琴正在演奏.类似的功能应该当您停止播放、调整或调用您的任何方法时提供.

Create a C# class for your instrument. Be sure that your code matches your design specifications and some minimal functionality is included. For example, if you called the violin.play() method, you should at least print that the violin is playing. Similar functionality should be supplied when you stop playing, tune or call any of your methods.

到目前为止我的代码:

    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;
      }
     }
    }

但我收到此错误:

错误 1 ​​'StringedMusInsApp.Guitarra.Main(string[])' 必须声明一个主体,因为它没有标记为抽象、外部或部分.

Error 1 'StringedMusInsApp.Guitarra.Main(string[])' must declare a body because it is not marked abstract, extern, or partial.

推荐答案

错误信息告诉你确切出了什么问题:

The error message tells you exactly what's wrong:

'StringedMusInsApp.Guitarra.Main(string[])' 必须声明一个主体,因为它没有标记为抽象、外部或部分.

'StringedMusInsApp.Guitarra.Main(string[])' must declare a body because it is not marked abstract, extern, or partial.

查看Main(string[])的方法声明:

public static void Main (string[] args);

没有方法体.但由于它不是抽象、外部或部分方法,它需要一个方法体.定义一个:

There's no method body. But since it's not an abstract, extern, or partial method, it requires a method body. Define one:

public static void Main (string[] args)
{
    // do something here
}

另请注意,如果您Main(string[]) 方法中执行任何操作,那么您的应用程序将不会执行任何操作.它会立即打开和关闭,无需执行任何代码.Main(string[]) 方法是应用程序的入口点.

Also note that if you don't do anything in the Main(string[]) method then your application won't do anything. It'll just open and close immediately without ever executing any code. The Main(string[]) method is the entry point for the application.

如果您将应用程序主机代码(入口点,Main(string[]),基本上是执行程序的内容)与逻辑代码分开,这对您来说可能会更容易构建(Guitarra 类,与您正在做的事情相关的任何业务逻辑,等等).对于这么小的东西,没有必要将它们分解成它们自己的程序集或使用 DI 或类似的东西,但它们至少应该是它们自己的类.

This would probably become easier for you to structure if you separate your application host code (the entry point, Main(string[]), basically the stuff that executes the program) from your logic code (the Guitarra class, any business logic related to what you're doing, and so on). For something this small it's not necessary to break them apart into their own assemblies or use DI or anything like that, but they should at least be their own classes.

这篇关于方法必须声明一个主体吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆