首頁  >  文章  >  後端開發  >  在 C# 中受保護

在 C# 中受保護

PHPz
PHPz原創
2024-09-03 15:28:471019瀏覽

在本文中,我們將詳細了解如何在 C# 中實作 protected。借助存取修飾符,我們可以限制參數和類別的可存取等級。 C#中有以下存取修飾符

  • 公開
  • 私人
  • 受保護
  • 內部

C# 中受保護的修飾符

在 C# 中,我們可以使用 protected 修飾符來指定存取僅限於包含類型。此外,我們也可以將它用於從包含類別派生的類型。 protected 這個字表示它本身以及衍生類別都可以存取或可見。

借助此成員或類型,只能由同一類別中使用的程式碼或衍生類別中使用的程式碼存取。 protected 關鍵字位於 private 和 public 修飾詞之間。它與 private 修飾符幾乎相同,但它允許成員存取衍生類別。當我們想要向父母授予對孩子財產的存取權時,我們主要使用 protected 關鍵字。因此我們可以藉助 protected 關鍵字重複使用邏輯。

範例:

using System;
class Test
{
protected int _x; private int _y;
}
class Test1 : Test
{
public Test1 ()
{
// In this we can  access the variable protected int but we cannot access private int variable
Console.WriteLine(this._x);
}
}
class Program
{
static void Main()
{ Test1 b = new Test1 (); }
}

考慮 2 個類,Test 和 Test1。類別 Test1 衍生自 Test。如果我們查看類別 Test 的內部,我們可以看到已經宣告了兩個 int 欄位。 1 個受保護,1 個私有。

在類別 B Test1 中,我們可以存取 protected int,但無法存取 private int。因此 protected 修飾符為我們提供了衍生類別中的額外存取權限。因此,借助 protected 關鍵字,我們可以存取包括所有衍生類別在內的受保護欄位。

一個類別也可以被保護。下面是如何聲明它的範例

文法:

public class Test
{
protected class Child
{
}
}

只有在巢狀類別中,我們才能將類別宣告為受保護的。我們無法在命名空間內定義它。

在 C# 中實作 Protected 的範例

以下範例展示如何在 C# 中實作 protected:

範例 #1 – 沒有實作子類別

代碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProtectedExample
{
class demo
{
// String Variable declared as protected
protected string name;
public void print()
{
Console.WriteLine("\name is " + name);
}
}
class Program
{
static void Main(string[] args)     // main method
{
demo d = new demo();
Console.Write("Enter your name:\t");
d.name = Console.ReadLine();
d.print();
Console.ReadLine();
}
}
}

在上面的範例中,字串被宣告為受保護。該程式將引發錯誤,因為 protected 將對其他類別隱藏其成員。所以它只能在兒童班級中使用。

範例 #2 – 透過繼承實作

代碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProtectedExample
{
class Demo
{
protected string name = "Protected Keyword";
protected void Display(string val)
{
Console.WriteLine("This is " + val);
}
}
class Program : Demo   // inheritance
{
static void Main(string[] args)
{
Program program = new Program();
// Accessing protected variable
Console.WriteLine("This is " + program.name);
// Accessing protected function
program.Display("protected example");
Console.ReadLine();
}
}
}

在上面的範例中,Parent 類別由受保護的成員組成。 protected 用於聲明字串。現在子類別是從父類別派生的,並且使用繼承的概念來存取受保護的成員。

輸出:

在 C# 中受保護

範例 #3

代碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProtectedExample
{
class Parent
{
private String Private = "My name is John";    // string declared as private
protected String Protected = "My name is Dan"; //  string declared as protected
}
class Child : Parent // inheritance
{
public void Show()
{
Console.WriteLine(Protected);
}
}
class Program
{
static int Main(string[] args)  // main method
{
Child child = new Child();    // child object
child.Show();
Console.ReadKey();
return 0;
}
}
}

在上面的範例中,父類別包含私有和受保護的字串。子類別是從父類別派生的。 Show() 雖然不能存取 private,但可以存取 protected。子類別物件用於呼叫該方法。 Protected 用於保護會員不被類外存取。

輸出:

在 C# 中受保護

我們也可以將建構函式宣告為受保護的。因此,透過將任何建構函數宣告為受保護,我們可以從子類別中呼叫它。

文法:

public class TEst : Test1
{
public Test() : base() // here we can Call the protected base constructor
{
}
}

我們無法呼叫受保護的方法。我們可以從衍生類別呼叫受保護的建構函式。

受保護的內部存取修飾符

在受保護的內部的幫助下,我們可以指定存取僅限於從包含類別派生的目前類型。因此,這確保了成員和類型可以由同一類別中的程式碼或另一個程式集中編寫的衍生類別存取。

範例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProtectedExample
{
class Demo
{
protected internal string name;    // variable is declared as protected internal
public void print()
{
Console.WriteLine("name is " + name);
}
}
class Program
{
static void Main(string[] args) // main method
{
Demo d = new Demo();
Console.Write("Enter your name:\t");
// Accepting value in protected internal variable
d.name = Console.ReadLine();
d.print();
Console.ReadLine();
}
}
}

輸出:

在 C# 中受保護

C# 中 protected 的重要性

受保護的關鍵字很有用,因為這種類型的變數可以由同一類別中使用的程式碼存取。當我們想要授予子類別權限以便它可以存取父類別成員時,它很有用。因此從這個意義上說,實作程式碼可重用性非常重要。

結論

因此我們可以將 protected 與變數一起使用,並使用繼承概念來存取它們。它可以用在類別本身或子類別可以存取成員的地方。

以上是在 C# 中受保護的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:C# 鍊錶下一篇:C# 鍊錶