由于其保护级别而无法访问

dan*_*dan 27 c#

我无法弄清楚这一点.问题是距离,俱乐部,清洁俱乐部,洞,分数和标准都说由于保护水平而无法进入,我不知道为什么因为我认为我做的一切都是正确的.

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;}
        }
    }   
}
Run Code Online (Sandbox Code Playgroud)

这是派生类:

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);
        }            
    }
}
Run Code Online (Sandbox Code Playgroud)

这是界面:

namespace homeworkchap8
{
    public interface ISwingClub
    {
        void SwingClub();
        void clean();
        void score();
    }  
}
Run Code Online (Sandbox Code Playgroud)

这是主要代码:

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();    
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Jam*_*iec 23

在您的基类Clubs中声明以下内容protected

  • 俱乐部;
  • 距离;
  • cleanclub;
  • 分数;
  • 比肩;
  • 洞;

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

在您的main代码中,您尝试在类本身之外访问这些.例如:

Console.WriteLine("How far to the hole?");
myClub.distance = Console.ReadLine();
Run Code Online (Sandbox Code Playgroud)

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

public string mydistance
{
    get
    {
        return distance;
    }
    set
    {
        distance = value;
    }
}        
Run Code Online (Sandbox Code Playgroud)

这意味着您的主要代码可以更改为

Console.WriteLine("How far to the hole?");
myClub.mydistance = Console.ReadLine();
Run Code Online (Sandbox Code Playgroud)


Mat*_*zer 5

Dan,只是你正在访问受保护的字段而不是属性.

例如,请参阅以下内容中的这一行Main(...):

myClub.distance = Console.ReadLine();

myClub.distance是受保护的字段,而您想要设置属性mydistance.

我只是给你一些提示,我不打算更改你的代码,因为这是功课!;)