Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ 一般的なアクセス修飾子 | データ構造とファイル操作
C#オブジェクト指向構造

一般的なアクセス修飾子

メニューを表示するにはスワイプしてください

すでにキーワード public に馴染みがあるかもしれません。これはアクセス修飾子であり、特定のクラスメソッド、またはフィールドがどこからアクセスできるかを定義します。

同様に、privateprotected という用語もあります。これらはどちらもメソッドフィールドのアクセス範囲を指定するために使用されます。

publicクラスの前に記述すると、そのクラスはプログラム内の異なる部分(ファイル)からアクセス可能になります。この側面について詳しく調べる必要はありません。本章の範囲外です。

メソッドやフィールドを public と指定すると、そのクラスのプロパティはオブジェクトを通じてコード内のどこからでもアクセス可能になります。

index.cs

index.cs

1234567891011121314151617181920212223
using System; public class Point { public int x; public int y; public Point(int x, int y) { this.x = x; this.y = y; } } class ConsoleApp { static void Main() { Point p = new Point(7, 9); // We can directly access 'public' fields of an object using the dot '.' symbol Console.WriteLine(p.x); Console.WriteLine(p.y); } }

オブジェクトのフィールドやメソッドをクラス外からアクセスできないようにしたい場合は、単純にそれを private に指定します。

index.cs

index.cs

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
using System; class Point { public int x; public int y; private double magnitude; public Point(int x, int y) { this.x = x; this.y = y; this.magnitude = calculateMagnitude(); } // A private method can only be used inside the class. // Trying to use a private method outside the class might result in a compilation error. private double calculateMagnitude() { // Math.Sqrt() is the square root function. return Math.Sqrt(x * x + y * y); } // Sometimes, to be able to access data from a private field, we make public methods // which specifically retrieve the value of that field. Such methods are called "getters", // and such fields are called "read-only" fields as we can access their data but cannot modify it from outside the class. public double getMagnitude() { return magnitude; } } class ConsoleApp { static void Main() { Point p = new Point(7, 9); // Correct Console.WriteLine(p.getMagnitude()); // Error Console.WriteLine(p.calculateMagnitude()); // Error Console.WriteLine(p.magnitude); } }

プライベートフィールドおよびメソッドは、派生クラスに継承されない点に注意が必要です。

index.cs

index.cs

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
using System; class Point { public int x; public int y; private double magnitude; public Point(int x, int y) { this.x = x; this.y = y; this.magnitude = calculateMagnitude(); } private double calculateMagnitude() { // Math.Sqrt() is the square root function. return Math.Sqrt(x * x + y * y); } public double getMagnitude() { return magnitude; } } class Point3D : Point { public int z; // The 'base(x, y)' part simply calls the constructor of the 'Point' class with 'x' and 'y' values. // This will be more thoroughly explained in the later section. public Point3D(int x, int y, int z) : base(x, y) { this.z = z; // Error calculateMagnitude(); } } class ConsoleApp { static void Main() { Point3D p = new Point3D(5, 7, 9); // Correct according to rules of language, however note that it will give the magnitude of x and y excluding z (for a 2D point) so logically the result will be wrong Console.WriteLine(p.getMagnitude()); } }

フィールドやメソッドをクラス外からアクセスできないようにしつつ、子クラスからはアクセス可能にするには、protected キーワードを使用。

index.cs

index.cs

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
using System; class Point { public int x; public int y; protected double magnitude; public Point(int x, int y) { this.x = x; this.y = y; this.magnitude = calculateMagnitude(); } protected double calculateMagnitude() { // Math.Sqrt() is the squareroot function. return Math.Sqrt(x * x + y * y); } public double getMagnitude() { return magnitude; } } class Point3D : Point { public int z; public Point3D(int x, int y, int z) : base(x, y) { this.z = z; // No Error Now calculateMagnitude(); } } class ConsoleApp { static void Main() { Point3D p = new Point3D(5, 7, 9); Console.WriteLine(p.getMagnitude()); } }

プライベート属性へアクセスするために、パブリックメソッド(ゲッター)を作成する場合がある。getMagnitude メソッドはゲッターの例。この方法により、フィールドは読み取り専用となり、書き込みはできないため、その属性は読み取り専用となる。

Note
注意

クラスメンバーにアクセス修飾子が指定されていない場合、デフォルトで private と見なされます。

1. どのアクセス修飾子を使用すると、メンバーは定義されたクラス内でのみアクセス可能になりますか?

2. クラス属性を外部からアクセスできないようにするにはどうしますか?

3. クラス外部からプライベートフィールドの値にアクセスする適切な方法はどれですか?

question mark

どのアクセス修飾子を使用すると、メンバーは定義されたクラス内でのみアクセス可能になりますか?

正しい答えを選んでください

question mark

クラス属性を外部からアクセスできないようにするにはどうしますか?

すべての正しい答えを選択

question mark

クラス外部からプライベートフィールドの値にアクセスする適切な方法はどれですか?

正しい答えを選んでください

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 1.  31

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 1.  31
some-alt