Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Abstraktion | OOP-Prinzipien
C# Über die Grundlagen Hinaus
course content

Kursinhalt

C# Über die Grundlagen Hinaus

C# Über die Grundlagen Hinaus

1. Zusätzliche Strukturen & Dateiverarbeitung
2. Strukturen & Enumeratoren
3. Einführung in die Objektorientierte Programmierung (OOP)
4. OOP Essentials
5. OOP-Prinzipien

book
Abstraktion

Abstraktion ist ein grundlegendes Konzept in der objektorientierten Programmierung (OOP), das es Entwicklern ermöglicht, komplexe Implementierungsdetails zu verbergen und sich auf wesentliche Funktionalitäten zu konzentrieren. In C# wird Abstraktion durch abstrakte Klassen erreicht.

Eine abstrakte Klasse ist eine Klasse, die nicht instanziiert werden kann, was bedeutet, dass wir kein Objekt dieser Klasse erstellen können. Eine abstrakte Klasse kann Attribute und Methoden wie jede andere Klasse enthalten, jedoch kann sie auch abstrakte Methoden enthalten, die Blueprint-Methoden sind, die von abgeleiteten Klassen implementiert werden sollen.

Wir können eine abstrakte Klasse erstellen, indem wir das Schlüsselwort abstract vor der Klassendefinition hinzufügen. Zum Beispiel, lassen Sie uns eine abstrakte Klasse namens Shape erstellen:

cs

index

copy
12345678910111213141516171819
using System; abstract class Shape { protected float circumference; public float getCircumference() { return circumference; } } class ConsoleApp { static void Main() { Shape s = new Shape(); // Error: Cannot create an instance of an abstract class } }

Ähnlich können wir eine abstrakte Methode erstellen, indem wir das Schlüsselwort abstract vor ihrem Rückgabetyp hinzufügen. Eine abstrakte Methode hat keinen Körper - sie ist einfach ein Blueprint:

cs

index

copy
1234567891011
abstract class Shape { protected float circumference; public float getCircumference() { return circumference; } public abstract float getArea(); }

Der Zweck davon ist es, einen Bauplan für andere Klassen zu erstellen. Dies hilft, den Code zu vereinfachen. Um dies besser zu verstehen, schauen wir uns die Polymorphismus-Aufgabe aus Kapitel 5 an:

cs

index

copy
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
using System; class Shape { // Perimeter is the length of the 'outline' of a shape protected float perimeter; public float getPerimeter() { return perimeter; } public virtual float getArea() { return 0.0f; } // A virtual method can only be either 'public' or 'protected' protected virtual float calculatorPerimeter() { return 0.0f; } } class Rectangle : Shape { float width; float height; public Rectangle(float width, float height) { this.width = width; this.height = height; this.perimeter = getPerimeter(); } public override float getArea() { return width * height; } protected override float calculatorPerimeter() { return width * 2 + height * 2; } } class Square : Shape { float length; public Square(float length) { this.length = length * length; } public override float getArea() { return length * length; } protected override float calculatorPerimeter() { return 4 * length; } } class Circle : Shape { float radius; public Circle(float radius) { this.radius = radius; } // Area of a Circle = pi . r . r public override float getArea() { return 3.14f * radius * radius; } // Perimeter (or) Circumference: 2 . pi . r protected override float calculatorPerimeter() { return 2.00f * 3.14f * radius; } } class ConsoleApp { static void Main() { Rectangle r1 = new Rectangle(10f, 20f); Square s1 = new Square(10f); Circle c1 = new Circle(10f); Console.WriteLine(r1.getArea()); Console.WriteLine(s1.getArea()); Console.WriteLine(c1.getArea()); } }

Im obigen Beispiel beabsichtigen wir niemals, die Shape-Klasse zu verwenden, dennoch mussten wir einige Scheinimplementierungen der Methoden getArea und calculatePerimeter innerhalb der Shape-Klasse schreiben. Wir können diesen Code etwas vereinfachen, indem wir die Shape-Klasse abstrakt machen. Darüber hinaus können wir auch die Methoden getArea und calculatePerimeter abstrakt machen.

cs

index

copy
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
using System; abstract class Shape { // Perimeter is the length of the 'outline' of a shape protected float perimeter; public float getPerimeter() { return perimeter; } public abstract float getArea(); protected abstract float calculatorPerimeter(); } class Rectangle : Shape { float width; float height; public Rectangle(float width, float height) { this.width = width; this.height = height; this.perimeter = getPerimeter(); } public override float getArea() { return width * height; } protected override float calculatorPerimeter() { return width * 2 + height * 2; } } class Square : Shape { float length; public Square(float length) { this.length = length * length; } public override float getArea() { return length * length; } protected override float calculatorPerimeter() { return 4 * length; } } class Circle : Shape { float radius; public Circle(float radius) { this.radius = radius; } public override float getArea() { return 3.14f * radius * radius; } protected override float calculatorPerimeter() { return 2.00f * 3.14f * radius; } } class ConsoleApp { static void Main() { Rectangle r1 = new Rectangle(10f, 20f); Square s1 = new Square(10f); Circle c1 = new Circle(10f); // We cannot create an instance of 'Shape' but we can use type datatype "Shape" for creating variables, arrays or lists List<Shape> shapes = new List<Shape>(); shapes.Add(r1); shapes.Add(s1); shapes.Add(c1); foreach(Shape shape in shapes) { Console.WriteLine(shape.getArea()); } } }

1. Welches Schlüsselwort wird verwendet, um eine Klasse abstrakt zu machen?

2. Kann eine abstrakte Klasse instanziiert werden?

Welches Schlüsselwort wird verwendet, um eine Klasse abstrakt zu machen?

Welches Schlüsselwort wird verwendet, um eine Klasse abstrakt zu machen?

Wählen Sie die richtige Antwort aus

Kann eine abstrakte Klasse instanziiert werden?

Kann eine abstrakte Klasse instanziiert werden?

Wählen Sie die richtige Antwort aus

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 5. Kapitel 8
We're sorry to hear that something went wrong. What happened?
some-alt