2011-05-25 231 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