无法访问由于其保护级别 [英] is inaccessible due to its protection level
问题描述
帮助我,我不能想出解决办法。问题是,距离,俱乐部,cleanclub,洞,成绩,和参数都因保护级别人迹罕至的说,我不知道为什么,因为我认为我做的一切权利。
命名空间homeworkchap8
{
公共类俱乐部
{
保护串俱乐部;
保护串的距离;
保护串cleanclub;
保护串分数;
保护串相提并论;
保护的弦孔;
公共字符串Myclub上海
{
得到
{
返回俱乐部;
}
组
{
俱乐部=价值;
}
}
公共字符串mydistance
{
得到
{
返回的距离;
}
组
{
距离=价值;
}
}
公共字符串mycleanclub
{
得到
{
返回cleanclub;
}
组
{
cleanclub =价值;
}
}
公共字符串myScore的
{
得到
{
回报率的分数;
}
组
{
分数=价值;
}
}
公共字符串parhole
{
得到
{
返回面值;
}
组
{
=票面价值;
}
}
公共字符串myhole
{
得到
{
返回孔;
}
组
{
孔=价值;
}
}
}
}
这是派生类:
命名空间homeworkchap8
{
公共类SteelClubs:俱乐部,ISwingClub
{
公共无效SwingClub()
{
Console.WriteLine(你打一个+ Myclub上海++ mydistance);
}
公共无效干净()
{
如果(mycleanclub!=是)
{
Console.WriteLine(你的俱乐部脏);
}
,否则
{
Console.WriteLine(你的俱乐部是干净的);
}
}
公共无效得分()
{
Console.WriteLine(你是在孔+ myhole +,你打进了+
myScore的+相提并论+ parhole);
}
}
}
这是界面:
命名空间homeworkchap8
{
公共接口ISwingClub
{
无效SwingClub() ;
无效干净();
无效得分();
}
}
下面是主要代码:
命名空间homeworkchap8
{
类主要
{
静态无效的主要(字串[] args)
{
SteelClubs Myclub上海=新SteelClubs();
Console.WriteLine(有多远洞口?);
myClub.distance =到Console.ReadLine();
Console.WriteLine(你打算什么俱乐部打?);
myClub.club =到Console.ReadLine();
myClub.SwingClub();
SteelClubs mycleanclub =新SteelClubs();
Console.WriteLine(\\\
Did你清理后你的俱乐部?);
mycleanclub.cleanclub =到Console.ReadLine();
mycleanclub.clean();
SteelClubs myScoreonHole =新SteelClubs();
Console.WriteLine(\\\
What孔是你吗?);
myScoreonHole.hole =到Console.ReadLine();
Console.WriteLine(你是怎么在比分上洞?);
myScoreonHole.scores =到Console.ReadLine();
Console.WriteLine(什么是洞的标准杆?);
myScoreonHole.par =到Console.ReadLine();
myScoreonHole.score();
Console.ReadKey();
}
}
}
在你的基类俱乐部
以下是声明保护
- 俱乐部;
- 的距离;
- cleanclub;
- 分数;
- 参数;
- 孔;
这意味着,这些只能由类本身或从俱乐部
派生的任何类访问。
在你的主
代码,您试图访问这些类本身之外。例如:
Console.WriteLine(?多远洞);
myClub.distance =到Console.ReadLine();
您已经(有点正确)提供的公共访问到这些变量。例如:
公共字符串mydistance
{
得到
{
返回的距离;
}
组
{
距离=价值;
}
}
这意味着你的主代码可以改为
Console.WriteLine(有多远洞口?);
myClub.mydistance =到Console.ReadLine();
help me i cant 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:
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
- club;
- distance;
- cleanclub;
- scores;
- par;
- hole;
which means these can only be accessed by the class itself or any class which derives from Clubs
.
In your main
code, you try to access these outside of the class itself. 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();
这篇关于无法访问由于其保护级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!