Kursinhalt
C# Über die Grundlagen Hinaus
C# Über die Grundlagen Hinaus
Allgemeine Zugriffsmodifikatoren
Sie sind möglicherweise bereits mit dem Schlüsselwort public
vertraut. Es ist ein Zugriffsmodifikator, der definiert, von wo aus auf eine bestimmte Klasse, Methode oder ein Feld zugegriffen werden kann.
Ähnlich gibt es Begriffe wie private
und protected
. Beide Begriffe können verwendet werden, um die Zugänglichkeit einer Methode oder eines Feldes zu spezifizieren.
Wenn wir public
vor eine Klasse schreiben, macht es die Klasse einfach von verschiedenen Teilen (Dateien) des Programms aus zugänglich. Wir müssen diesen Aspekt nicht im Detail erkunden, da er nicht im Umfang dieses Kapitels liegt.
Wenn wir eine Methode oder ein Feld als public
spezifizieren, machen wir diese Eigenschaft der Klasse im Grunde von überall im Code durch ein Objekt zugänglich:
index
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); } }
Wenn wir nicht möchten, dass ein Feld oder eine Methode eines Objekts außerhalb der Klasse zugänglich ist, können wir es einfach als private
deklarieren:
index
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); } }
Es ist wichtig zu beachten, dass private Felder und Methoden nicht von abgeleiteten Klassen geerbt werden:
index
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()); } }
Um ein Feld oder eine Methode von außerhalb der Klasse unzugänglich, aber von abgeleiteten Klassen zugänglich zu machen, verwenden wir das Schlüsselwort protected
:
index
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()); } }
Manchmal erstellen wir öffentliche Methoden, die Getter genannt werden, um auf private Attribute einer Klasse zugreifen zu können. Die Methode getMagnitude
ist ein Beispiel für einen Getter. Dies stellt sicher, dass das Feld nur lesbar und nicht beschreibbar ist, wodurch dieses Attribut schreibgeschützt wird.
Hinweis: Wenn kein Zugriffsmodifikator für ein Klassenmitglied angegeben ist, wird standardmäßig private
angenommen.
1. Welcher Zugriffsmodifikator ermöglicht es einem Mitglied, nur innerhalb seiner definierenden Klasse zugänglich zu sein?
2. Wie kann man ein Klassenattribut von außen unzugänglich machen?
3. Was ist eine gute Möglichkeit, auf die Werte privater Felder von außerhalb der Klasse zuzugreifen?
Danke für Ihr Feedback!