c#歧义错误

时间:2011-11-28 01:29:39

标签: c# ambiguity

我是Java新手,并且在模糊性方面遇到错误。请让我知道需要纠正的内容。

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 个答案:

答案 0 :(得分:6)

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

答案 1 :(得分:4)

我可能会将以下内容称为更为惯用的C#。

  1. 使用属性而不是公共字段。
  2. 在适当的时候,首选自动吸气/固定器。
  3. 属性名称应以大写字母
  4. 开头
  5. 明确指定可见性
  6. -

    public class JessiahP3
    {
        private int strings  = 1;
        public string InstrumentName { get; set; }
        public boolean IsPlaying { get; set; }
        public boolean IsTuned { get; set; }
    }
    

答案 2 :(得分:1)

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

答案 3 :(得分:1)

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

  1. 您将isTuned用作同一类型的变量和方法名称。
  2. System.out.println需要Console.WriteLine
  3. boolean应为bool(或Boolean
  4. 话虽如此,在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" );
        } 
    }
    
相关问题