1

我是来自 java 的 C# 新手,并且收到关于歧义的错误。请让我知道需要更正的地方。

public class JessiahP3 
{
    boolean  isPlaying =  false;
    int strings  = 1;
    boolean isTuned = false;
    public String instrumentName;

    //is tuned
    public void isTuned() 
    {
        isTuned = true;
        System.out.println("Currently tuning " + getInstrumentName());
    }

    //not tuned
    public void isNotTuned() 
    {
        isTuned = false;
        System.out.println(getInstrumentName() + " is not tuned");
    }
}
4

4 回答 4

6

您有一个名为 isTuned 的变量和函数。

于 2011-11-28T01:32:48.410 回答
4

我可以建议以下作为更惯用的 C#。

  1. 使用属性而不是公共字段。
  2. 在适当的情况下,首选属性的自动 getter/setter。
  3. 属性名称应以大写字母开头
  4. 明确指定可见性

--

public class JessiahP3
{
    private int strings  = 1;
    public string InstrumentName { get; set; }
    public boolean IsPlaying { get; set; }
    public boolean IsTuned { get; set; }
}
于 2011-11-28T01:37:41.383 回答
1

您有一个具有相同签名的字段和方法。见isTuned

于 2011-11-28T01:34:49.513 回答
1

我在这里看到三个明显的错误。

  1. isTuned在同一类型中同时用作变量和方法名称。
  2. System.out.println将需要Console.WriteLine
  3. boolean应该是bool(或Boolean

话虽如此,在 C# 中,这通常作为单个属性完成(以及更改getInstrumentName()InstrumentName属性):

bool isTuned = false;

bool IsTuned
{
    get { return isTuned; }
    set 
    { 
         this.isTuned = value; 
         Console.WriteLine( isTuned ? "Currently tuning " + this.InstrumentName : this.InstrumentName + " is not tuned" );
    } 
}
于 2011-11-28T01:38:24.207 回答