简体   繁体   English

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

[英]is inaccessible due to its protection level

I can't figure this out.我想不通。 The problem is that the distance, club, cleanclub, hole, scores and par all say inaccessible due to protection level and I don't know why because I thought I did everything right.问题是距离、俱乐部、清洁俱乐部、洞、分数和标准杆都因为保护级别而无法访问,我不知道为什么,因为我认为我做的一切都是正确的。

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;}
        }
    }   
}

this is the derived class:这是派生的 class:

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);
        }            
    }
}

This is the interface:这是界面:

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

here is the main code:这是主要代码:

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();    
        }
    }
}

In your base class Clubs the following are declared protected在您的基础 class Clubs中,以下内容被声明为protected

  • club;俱乐部;
  • distance;距离;
  • cleanclub;清洁俱乐部;
  • scores;分数;
  • par;平价;
  • hole;洞;

which means these can only be accessed by the class itself or any class which derives from Clubs .这意味着这些只能由 class 本身或从Clubs派生的任何 class 访问。

In your main code, you try to access these outside of the class itself.在您的main代码中,您尝试在 class 本身之外访问这些。 eg:例如:

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

You have (somewhat correctly) provided public accessors to these variables.您已经(有些正确)为这些变量提供了公共访问器。 eg:例如:

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

which means your main code could be changed to这意味着您的主要代码可以更改为

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

Dan, it's just you're accessing the protected field instead of properties.丹,只是您正在访问受保护的字段而不是属性。

See for example this line in your Main(...) :例如,请参阅Main(...)中的这一行:

myClub.distance = Console.ReadLine();

myClub.distance is the protected field, while you wanted to set the property mydistance . myClub.distance是受保护的字段,而您想设置属性mydistance

I'm just giving you some hint, I'm not going to correct your code, since this is homework;我只是给你一些提示,我不会更正你的代码,因为这是家庭作业; ;) ;)

myClub.distance = Console.ReadLine();

should be应该

myClub.mydistance = Console.ReadLine(); 

use your public properties that you have defined for others as well instead of the protected field members.使用您为其他人定义的公共属性,而不是受保护的字段成员。

In your Main method, you're trying to access, for instance, club (which is protected), when you should be accessing myclub which is the public property that you created.在您的 Main 方法中,您正在尝试访问例如club (受保护),而您应该访问myclub ,这是您创建的公共属性。

You organized class interface such that public members begin with "my".您组织了 class 界面,使公共成员以“my”开头。 Therefore you must use only those members.因此,您必须仅使用这些成员。 Instead of代替

myScoreonHole.hole = Console.ReadLine();

you should write你应该写

myScoreonHole.myhole = Console.ReadLine();

It's because you cannot access protected member data through its class instance.这是因为您无法通过其 class 实例访问受保护的成员数据。 You should correct your code as follows:您应该按如下方式更正您的代码:

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();    
        }
    }
}

You need to use the public properties from Main , and not try to directly change the internal variables.您需要使用Main中的公共属性,而不是尝试直接更改内部变量。

The reason being you cannot access protected member data through the instance of the class.原因是您无法通过 class 的实例访问受保护的成员数据。

Reason why it is not allowed is explained in this blog .这个博客解释了为什么不允许这样做的原因。

Though it is irrelevant to the case at hand, for the benefit of the next person who arrives at this article through a search engine, if the default constructor of your base class is marked as private , derived classes will incur a CS0122 diagnostic.尽管它与手头的情况无关,但为了下一个通过搜索引擎到达本文的人的利益,如果您的基础 class 的默认构造函数被标记为private ,派生类将引发CS0122诊断。

Instead, you must promote the private method to protected .相反,您必须将私有方法提升protected

The protected method remains inaccessible to consumers of the derived class unless said class overrides it with a new constructor.派生的 class 的使用者仍然无法访问受保护的方法,除非所述 class 用的构造函数覆盖它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM