2011-05-25 123 views
20

我无法弄清楚这一点。问题在于距离,俱乐部,干净俱乐部,洞,比分和标准都因保护水平而无法接近,我不知道为什么,因为我认为我做的一切都是正确的。无法访问

namespace homeworkchap8 
{ 
    public class Clubs 
    { 
     protected string club; 
     protected string distance; 
     protected string cleanclub; 
     protected string scores; 
     protected string par; 
     protected string hole;    

     public string myclub 
     { 
      get { return club; } 
      set {club = value; } 
     }   
     public string mydistance 
     { 
      get { return distance; } 
      set { distance = value; } 
     }   
     public string mycleanclub 
     { 
      get { return cleanclub; } 
      set { cleanclub = value; } 
     }  
     public string myscore 
     { 
      get { return scores; } 
      set { scores = value; } 
     }  
     public string parhole 
     { 
      get { return par; } 
      set { par = value; } 
     }  
     public string myhole 
     { 
      get { return hole; } 
      set { hole = value;} 
     } 
    } 
} 

这是派生类:

namespace homeworkchap8 
{ 
    public class SteelClubs : Clubs, ISwingClub 
    { 
     public void SwingClub() 
     { 
      Console.WriteLine("You hit a " + myclub + " " + mydistance); 
     } 

     public void clean() 
     { 
      if (mycleanclub != "yes") 
      { 
       Console.WriteLine("your club is dirty"); 
      } 
      else 
      { 
       Console.WriteLine("your club is clean"); 
      } 
     } 

     public void score() 
     { 
      Console.WriteLine("you are on hole " + myhole + " and you scored a " + 
       myscore + " on a par " + parhole); 
     }    
    } 
} 

这是接口:

namespace homeworkchap8 
{ 
    public interface ISwingClub 
    { 
     void SwingClub(); 
     void clean(); 
     void score(); 
    } 
} 

这里是主要代码:

namespace homeworkchap8 
{ 
    class main 
    {  
     static void Main(string[] args) 
     {  
      SteelClubs myClub = new SteelClubs(); 
      Console.WriteLine("How far to the hole?"); 
      myClub.distance = Console.ReadLine(); 
      Console.WriteLine("what club are you going to hit?"); 
      myClub.club = Console.ReadLine(); 
      myClub.SwingClub(); 

      SteelClubs mycleanclub = new SteelClubs(); 
      Console.WriteLine("\nDid you clean your club after?"); 
      mycleanclub.cleanclub = Console.ReadLine(); 
      mycleanclub.clean(); 

      SteelClubs myScoreonHole = new SteelClubs(); 
      Console.WriteLine("\nWhat hole are you on?"); 
      myScoreonHole.hole = Console.ReadLine(); 
      Console.WriteLine("What did you score on the hole?"); 
      myScoreonHole.scores = Console.ReadLine(); 
      Console.WriteLine("What is the par of the hole?"); 
      myScoreonHole.par = Console.ReadLine(); 

      myScoreonHole.score(); 

      Console.ReadKey();  
     } 
    } 
} 
+6

欢迎来到SO。请尝试使用代码小部件来格式化您的代码,并避免txtspk。这次我编辑了你的问题来整理它。 – Jamiec 2011-05-25 13:25:22

回答

20

在你的基类Clubs声明protected

  • 俱乐部以下;
  • 距离;
  • cleanclub;
  • 得分;
  • par;
  • 孔;

这意味着这些只能由类本身或任何派生自Clubs的类访问。

在您的main代码中,您尝试访问类以外的这些代码。例如:

Console.WriteLine("How far to the hole?"); 
myClub.distance = Console.ReadLine(); 

您(有点正确)为这些变量提供了公共访问器。例如:

public string mydistance 
{ 
    get 
    { 
     return distance; 
    } 
    set 
    { 
     distance = value; 
    } 
}   

这意味着你的主代码可改为

Console.WriteLine("How far to the hole?"); 
myClub.mydistance = Console.ReadLine(); 
5

丹,它只是你'重新访问受保护的字段而不是属性。

例如见这一行你Main(...)

myClub.distance = Console.ReadLine();

myClub.distance是受保护的领域,而你想设置的属性mydistance

我只是给你一些提示,我不会纠正你的代码,因为这是作业! ;)

2

在您的Main方法中,当您尝试访问(例如,club(受保护))时,您应该访问您创建的公共属性myclub

0

您需要使用Main的公共属性,而不是尝试直接更改内部变量。

4
myClub.distance = Console.ReadLine(); 

应该

myClub.mydistance = Console.ReadLine(); 

使用您的其他人也,而不是受保护的字段成员定义的公共属性。

+0

非常感谢我没有看到或至少尝试过 – dan 2011-05-25 13:31:08

0

这是因为您无法通过其类实例访问受保护的成员数据。 你应该纠正你的代码如下:

namespace homeworkchap8 
{ 
    class main 
    {  
     static void Main(string[] args) 
     {  
      SteelClubs myClub = new SteelClubs(); 
      Console.WriteLine("How far to the hole?"); 
      myClub.mydistance = Console.ReadLine(); 
      Console.WriteLine("what club are you going to hit?"); 
      myClub.myclub = Console.ReadLine(); 
      myClub.SwingClub(); 

      SteelClubs mycleanclub = new SteelClubs(); 
      Console.WriteLine("\nDid you clean your club after?"); 
      mycleanclub.mycleanclub = Console.ReadLine(); 
      mycleanclub.clean(); 

      SteelClubs myScoreonHole = new SteelClubs(); 
      Console.WriteLine("\nWhat hole are you on?"); 
      myScoreonHole.myhole = Console.ReadLine(); 
      Console.WriteLine("What did you score on the hole?"); 
      myScoreonHole.myscore = Console.ReadLine(); 
      Console.WriteLine("What is the par of the hole?"); 
      myScoreonHole.parhole = Console.ReadLine(); 

      myScoreonHole.score(); 

      Console.ReadKey();  
     } 
    } 
} 
2

你有组织类接口,使得公众成员“我”开头。因此您只能使用这些成员。取而代之的

myScoreonHole.hole = Console.ReadLine(); 

你应该写

myScoreonHole.myhole = Console.ReadLine(); 
0

的原因是你无法通过类的实例访问受保护的数据成员。

为什么不允许的原因在此解释blog