我嘗試使用 C# 創建簡單的控制臺應用程式。我想在我的應用程式中涵蓋這些簡單的事情。
在我的 Team Class 里面有這些方法。
- 成員變數 - teamName(string)
- 成員變數 - noOfPlayers(int)
- 一個建構式 1.it 接受 2 個引數并將它們分別分配給 teanName 和 noOfPlayers。
- 成員函式AddPlayer(count) 1.它以整數count為引數,按count增加noOfPlayers。
- 成員函式 RemovePlayer 1. 它以整數計數作為引數,并嘗試按計數減少 onOfPlayers。2.如果 decreasign 使 noOfPlayers 為負,則此函式僅回傳 false 3.Else 按計數減少 noOfPlayers 并回傳 true。
在我的 Subteam 類(繼承自 Team 類)中有這些方法。
- 一個建構式。1.它需要兩個引數 teamName 和 noOfPlayers 并使用這些引數呼叫基類建構式。
- 成員功能 ChangeTeamName
- 它將字串名稱作為引數并將 teamName 更改為 name。
在我的 ChangeTeamName 方法中,我需要使用名稱引數并將名稱更改為團隊名稱。但我不知道該怎么做。請幫助我使用上述任務創建這個簡單的應用程式。并請檢查其他代碼是否正確。
當我嘗試運行程式時出現這些錯誤。
1. 嚴重性代碼描述專案檔案行抑制狀態錯誤 CS1061“Program.Subteam”不包含“onOfPlayers”的定義,并且無法找到接受“Program.Subteam”型別的第一個引數的可訪問擴展方法“onOfPlayers”(是您是否缺少 using 指令或程式集參考?) ConsoleApp1 C:\Users\Sandanuwan\Desktop\ConsoleApp1\ConsoleApp1\Program.cs 74 Active
2.Severity Code Description Project File Line Suppression State Error CS0122 'Program.Team.noOfPlayers' is inaccessible due to its protection level ConsoleApp1 C:\Users\Sandanuwan\Desktop\ConsoleApp1\ConsoleApp1\Program.cs 68 Active
namespace ConsoleApp1
{
public partial class Program
{
public class Team
{
string teamName;
int noOfPlayers;
public Team(string teamName, int noOfPlayers)
{
this.teamName = teamName;
this.noOfPlayers = noOfPlayers;
}
public void AddPlayer(int count)
{
noOfPlayers = count ;
}
public bool RemovePlayer(int count)
{
noOfPlayers = count--;
if (noOfPlayers < 0)
{
return false;
}
return true;
}
}
class Subteam : Team
{
public Subteam(string teamName, int noOfPlayers) : base(teamName, noOfPlayers)
{
}
public void ChangeTeamName(string name)
{
}
}
static void Main(string[] args)
{
if(!typeof(Subteam).IsSubclassOf(typeof(Team)))
{
throw new Exception("Subteam class chould inherit from Team class");
}
String str = Console.ReadLine();
String[] strArr = str.Split();
string initialName = strArr[0];
int count = Convert.ToInt32(strArr[1]);
Subteam teamObj = new Subteam(initialName, count);
Console.WriteLine("Team " teamObj.teamName " created");
str = Console.ReadLine();
count = Convert.ToInt32(str);
Console.WriteLine("Current number of players in team " teamObj, teamName " is " teamObj.noOfPlayers);
teamObj.AddPlayer(count);
Console.WriteLine("New number of players in team " teamObj.teamName " is " teamObj.onOfPlayers);
str = Console.ReadLine();
count = Convert.ToInt32(str);
Console.WriteLine("Current number of players in team " teamObj.teamName " is " teamObj.onOfPlayers);
var res = teamObj.RemovePlayer(count);
if(res)
{
Console.WriteLine("New number of players in team " teamObj.teamName " is " teamObj.onOfPlayers);
} else
{
Console.WriteLine("Number of players in team " teamObj.teamName " remains same");
}
str = Console.ReadLine();
teamObj.ChangeTeamName(str);
Console.WriteLine("Team name of team " initialName " changed to " teamObj.teamName);
}
}
}
uj5u.com熱心網友回復:
如果我理解正確,并且您不想要類ChangeTeamName上的方法,Team那么您需要創建該teamName欄位,protected以便派生SubTeam類可以訪問和修改它。
您尚未向該teamName欄位添加訪問修飾符,因此它具有默認訪問權限,private這意味著它只能從定義在該類中的類內部讀取或更改Team。
protected 意味著它可以從這個類本身和派生(子)類訪問。
internal 意味著它可以被同一程式集(dll 或專案)中的任何類訪問。
public 意味著它可以從任何地方訪問。
我建議使用屬性而不是類進行protected訪問。
public class Team
{
protected string TeamName { get; set; }
int noOfPlayers;
public Team(string teamName, int noOfPlayers)
{
this.TeamName = teamName;
this.noOfPlayers = noOfPlayers;
}
etc.
}
class Subteam : Team
{
public Subteam(string teamName, int noOfPlayers) : base(teamName, noOfPlayers)
{
}
public void ChangeTeamName(string name)
{
this.TeamName = name;
}
}
uj5u.com熱心網友回復:
您可能會遇到此問題,因為未指定任何內容時的默認保護級別是私有的,因此派生類無法訪問基類的變數。您可以將其更改為 protected 以便派生類可以使用該變數,或者(盡管可能不推薦)將其完全公開。
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
(new C() {Index = 1}).Print();
}
}
public class B
{
public int Index {get; set;}
}
public class C : B {
public void Print(){
Console.WriteLine(this.Index);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/342839.html
上一篇:如何使用方法更新文本塊
下一篇:如何在WPF中制作側邊欄
