Vererbung (CSharp)

Aus Byte-Welt Wiki
Zur Navigation springenZur Suche springen

Im folgenden wird in anschaulicher Weise die Vererbung in C#, welches ein Teilbereich der Objektorientierung ist, beschrieben. Alle Programme sehen, wenn man sie ausführt, genau gleich aus und bedienen sich auch gleich. Auch wenn der Quellcode immer wieder etwas anders ist.


Fangen wir mit einem einfachen kleinen Programm an, das ein Fenster öffnet und drei Buttons erstellt. Zwei davon sind rot:

<syntaxhighlight lang=csharp">    

                                                                                                  

                                                      </syntaxhighlight lang=csharp">

<syntaxhighlight lang=csharp">using System;

using System.Drawing; using System.Windows.Forms;

public class MyProgram {

 public static void Main(string[] args) {
   Form f = new Form();
   f.Font = SystemFonts.MessageBoxFont;
   f.Text = "My Window";
   FlowLayoutPanel p = new FlowLayoutPanel();
   Button b1 = new Button();
   Button b2 = new Button();
   Button b3 = new Button();
   b1.Text = "Button 1";
   b2.Text = "Button 2";
   b3.Text = "Button 3";
   b1.BackColor = Color.FromArgb(255,0,0);
   b3.BackColor = Color.FromArgb(255,0,0);
   p.Controls.Add(b1);
   p.Controls.Add(b2);
   p.Controls.Add(b3);
   f.Controls.Add(p);
   p.Width = 300;
   p.Height = 100;
   f.Width = 350;
   f.Height = 150;
   Application.EnableVisualStyles();
   Application.Run(f);
 }

}</syntaxhighlight lang=csharp">

Durch "new" wird ein neues Objekt (eine Instanz) erstellt, das eine Kopie des Hauptobjekts ist.


Betrachten wir uns nun Form. Man kann in C# eine neue Klasse erstellen (sofern die Klasse, von der sie sich ableitet, nicht "private" ist), die sämtliche Eigenschaften der Klasse, von der sie abgeleitet wurde, erbt.

In folgendem Programm habe ich nun eine Klasse MyForm erstellt, die mittels : von der Klasse Form alle Eigenschaften erbt:

<syntaxhighlight lang=csharp">using System.Windows.Forms;

public class MyForm : Form {

 public MyForm() {
 }

}                                                                                                                                  </syntaxhighlight lang=csharp">

<syntaxhighlight lang=csharp">using System;

using System.Drawing; using System.Windows.Forms;

public class MyProgram {

 public static void Main(string[] args) {
   MyForm f = new MyForm();
   f.Font = SystemFonts.MessageBoxFont;
   f.Text = "My Window";
   FlowLayoutPanel p = new FlowLayoutPanel();
   Button b1 = new Button();
   Button b2 = new Button();
   Button b3 = new Button();
   b1.Text = "Button 1";
   b2.Text = "Button 2";
   b3.Text = "Button 3";
   b1.BackColor = Color.FromArgb(255,0,0);
   b3.BackColor = Color.FromArgb(255,0,0);
   p.Controls.Add(b1);
   p.Controls.Add(b2);
   p.Controls.Add(b3);
   f.Controls.Add(p);
   p.Width = 300;
   p.Height = 100;
   f.Width = 350;
   f.Height = 150;
   Application.EnableVisualStyles();
   Application.Run(f);
 }

}</syntaxhighlight lang=csharp">

Im Programm MyProgram wird nun MyForm auch genau so genutzt und behandelt wie zuvor Form. Und das Ergebnis ist ebenfalls gleich.


(wird fortgesetzt...)


Siehe auch

Wikipedia: Vererbung in der Programmierung.