Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Gängige Zugriffsmodifizierer | OOP-Grundlagen
C# Jenseits der Grundlagen

bookGängige Zugriffsmodifizierer

Sie sind möglicherweise bereits mit dem Schlüsselwort public vertraut. Es handelt sich dabei um einen Zugriffsmodifizierer, 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 festzulegen.

Wenn Sie public vor eine Klasse schreiben, wird die Klasse einfach aus verschiedenen Teilen (Dateien) des Programms zugänglich gemacht. Dieser Aspekt muss nicht im Detail betrachtet werden, da er außerhalb des Umfangs dieses Kapitels liegt.

Wenn Sie eine Methode oder ein Feld als public deklarieren, machen Sie diese Eigenschaft der Klasse im Grunde von überall im Code über ein Objekt zugänglich:

index.cs

index.cs

copy
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); } }

Wenn Sie nicht möchten, dass auf ein Feld oder eine Methode eines Objekts außerhalb der Klasse zugegriffen werden kann, können Sie es einfach als private deklarieren:

index.cs

index.cs

copy
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); } }

Es ist wichtig zu beachten, dass private Felder und Methoden nicht von abgeleiteten Klassen geerbt werden:

index.cs

index.cs

copy
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()); } }

Um ein Feld oder eine Methode außerhalb der Klasse unzugänglich, aber für abgeleitete Klassen zugänglich zu machen, kann das Schlüsselwort protected verwendet werden:

index.cs

index.cs

copy
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()); } }

Um auf private Attribute einer Klasse zugreifen zu können, werden manchmal öffentliche Methoden erstellt, die als Getter bezeichnet werden. Die Methode getMagnitude ist ein Beispiel für einen Getter. Dadurch ist das Feld nur lesbar und nicht schreibbar, was dieses Attribut schreibgeschützt macht.

Note
Hinweis

Wenn für ein Klassenmitglied kein Zugriffsmodifizierer angegeben wird, gilt standardmäßig private.

1. Welcher Zugriffsmodifizierer erlaubt den Zugriff auf ein Mitglied nur innerhalb der definierenden Klasse?

2. Wie kann ein Klassenattribut von außen unzugänglich gemacht werden?

3. Was ist eine geeignete Methode, um auf die Werte privater Felder von außerhalb der Klasse zuzugreifen?

question mark

Welcher Zugriffsmodifizierer erlaubt den Zugriff auf ein Mitglied nur innerhalb der definierenden Klasse?

Select the correct answer

question mark

Wie kann ein Klassenattribut von außen unzugänglich gemacht werden?

Select the correct answer

question mark

Was ist eine geeignete Methode, um auf die Werte privater Felder von außerhalb der Klasse zuzugreifen?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 4. Kapitel 3

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Suggested prompts:

Can you explain the difference between public, private, and protected with examples?

Why would I use getters instead of making a field public?

Are there any best practices for choosing between these access modifiers?

Awesome!

Completion rate improved to 2.04

bookGängige Zugriffsmodifizierer

Swipe um das Menü anzuzeigen

Sie sind möglicherweise bereits mit dem Schlüsselwort public vertraut. Es handelt sich dabei um einen Zugriffsmodifizierer, 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 festzulegen.

Wenn Sie public vor eine Klasse schreiben, wird die Klasse einfach aus verschiedenen Teilen (Dateien) des Programms zugänglich gemacht. Dieser Aspekt muss nicht im Detail betrachtet werden, da er außerhalb des Umfangs dieses Kapitels liegt.

Wenn Sie eine Methode oder ein Feld als public deklarieren, machen Sie diese Eigenschaft der Klasse im Grunde von überall im Code über ein Objekt zugänglich:

index.cs

index.cs

copy
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); } }

Wenn Sie nicht möchten, dass auf ein Feld oder eine Methode eines Objekts außerhalb der Klasse zugegriffen werden kann, können Sie es einfach als private deklarieren:

index.cs

index.cs

copy
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); } }

Es ist wichtig zu beachten, dass private Felder und Methoden nicht von abgeleiteten Klassen geerbt werden:

index.cs

index.cs

copy
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()); } }

Um ein Feld oder eine Methode außerhalb der Klasse unzugänglich, aber für abgeleitete Klassen zugänglich zu machen, kann das Schlüsselwort protected verwendet werden:

index.cs

index.cs

copy
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()); } }

Um auf private Attribute einer Klasse zugreifen zu können, werden manchmal öffentliche Methoden erstellt, die als Getter bezeichnet werden. Die Methode getMagnitude ist ein Beispiel für einen Getter. Dadurch ist das Feld nur lesbar und nicht schreibbar, was dieses Attribut schreibgeschützt macht.

Note
Hinweis

Wenn für ein Klassenmitglied kein Zugriffsmodifizierer angegeben wird, gilt standardmäßig private.

1. Welcher Zugriffsmodifizierer erlaubt den Zugriff auf ein Mitglied nur innerhalb der definierenden Klasse?

2. Wie kann ein Klassenattribut von außen unzugänglich gemacht werden?

3. Was ist eine geeignete Methode, um auf die Werte privater Felder von außerhalb der Klasse zuzugreifen?

question mark

Welcher Zugriffsmodifizierer erlaubt den Zugriff auf ein Mitglied nur innerhalb der definierenden Klasse?

Select the correct answer

question mark

Wie kann ein Klassenattribut von außen unzugänglich gemacht werden?

Select the correct answer

question mark

Was ist eine geeignete Methode, um auf die Werte privater Felder von außerhalb der Klasse zuzugreifen?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 4. Kapitel 3
some-alt