Accessibility Level
◎ 常見的存取修飾詞
- Private : 僅可以在該 CLASS 中使用,通常在 FIELD 設為 PRIVATE
- Public : 最開放的存取修飾詞
- Protected : 僅限於 父類別 以及其 子類別使用
- Internal : 僅能被使用於目前的 Assembly ( 該專案編譯產生的
.dll
或.exe
) - Protected Internal : 同時有 Protected 與 Internal 的性質
Type | Type Members |
---|---|
Class, Struct, Enums, Interface, Dlegate | Fields, Properties, Constructors, Methods |
僅可以用Public 或 Internal 擇一 預設為 INTERNAL |
Private, Public, Protected, Internal, Protected Internal 預設為 PRIVATE |
◎ Assembly ?
在範例之前,我們先來了解什麼是 Assembly 一般來說新增一個「類別庫(Class Library)」專案之後
「專案」上按下右鍵-->屬性
重新建置之後...
◎ 範例程式碼
一般的主控台應用程式(要加入類別庫的參考)
using System;
using OnlineGame;
using static System.Console;
namespace AccessModifiersDemo
{
class Program
{
static void Main(string[] args)
{
Gamer gamer = new Gamer();
//int gamer_gameScore = gamer._gameScore; // Error, Not available.
//int gamer_Level = gamer._level; // Error, Not available.
WriteLine("gamer.GameScore == {0}", gamer.GameScore);
GamerSub gamerSub = new GamerSub();
gamerSub.GetGameScore();
WriteLine("gamerSub.GameScore == {0} , gamerSub.Level = {1}.", gamerSub.GameScore, gamerSub.Level);
WriteLine("============ 範例二 ============");
//GamerA gamerA = new GamerA();
////int gamerA_gameScore = gamerA._gameScore; // Error, Not available.
////int gamerA_Level = gamerA._level; // Error, Not available.
//Console.WriteLine("gamerA.GameScore == {0}", gamerA.GameScore);
//GamerASub gamerASub = new GamerASub();
//Console.WriteLine("gamerASub.Level == {0} , gamerASub.GameScore = {1}", gamerASub.GameScore, gamerASub.Level);
//// gamerASub.GetGameScore(); // Error, Not available.
Console.ReadLine();
}
}
}
namespace OnlineGame
{
public class Gamer
{
#region Field
private int _gameScore = 0;
protected int _level = 1;
#endregion
#region Property
public int GameScore
{
get
{
return _gameScore;
}
set
{
_gameScore = value;
}
}
#endregion
}
//Internal 代表僅能被使用於目前的 Assembly ( 該專案編譯產生的 dll 或 exe )
internal class GamerSub : Gamer
{
#region Property
//用Property方式讓子類別可以存取父類別的 Protected Field
public int Level
{
get
{
return base._level;
}
set
{
base._level = value;
}
}
#endregion
#region Function
public int GetGameScore()
{
//使用父類別的Property,存取
return base.GameScore;
}
#endregion
}
}
類別庫程式碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OnLineGameA
{
public class GamerA
{
// private field means only available in current class.
private int _gameScore = 500;
// protected field means available in current class and its sub class.
protected internal int _level = 2;
// public property means available every where.
public int GameScore
{
get
{
return _gameScore;
}
set
{
_gameScore = value;
}
}
}
//Internal means only available in current assembly.
public class GamerASub : GamerA
{
#region Property
//用Property方式讓子類別可以存取父類別的 Protected Field
public int Level
{
get
{
return _level;
}
set
{
_level = value;
}
}
#endregion
// Protected internal method means only available in current assembly, and its sub class.
protected internal int GetGameScore()
{
////return base._gameScore;
// base._gameScore is private, thus, not available in its sub class.
return GameScore;
}
}
}