fork(1) download
  1. using System;
  2.  
  3. class Vehicle { // Basis Klasse(parent class)
  4. public Vehicle() { // Standardkonstruktor
  5. Console.WriteLine("Base");
  6. }
  7. public Vehicle(string col) { // Überladenen paramentrisierten Konstruktor
  8. Console.WriteLine("param Base");
  9. color = col;
  10. }
  11. public Vehicle(string col, int zahl) { // Überladenen paramentrisierten Konstruktor
  12. Console.WriteLine("param Base");
  13. color = col;
  14. }
  15.  
  16. protected string color; // Vehicle Feldcolor
  17.  
  18. public void honk() { // Vehicle Methode innerhalbder Klasse definiert
  19. Console.WriteLine("Tuut, tuut");
  20. }
  21.  
  22. public void drucke() {
  23. System.Console.WriteLine("Vehicle Color:" + color);
  24. this.honk(); // Aufruf einer Methode innerhalb des Objekts(this)
  25. }
  26. }
  27.  
  28. class Car : Vehicle { // Abgeleitete Klasse (child)
  29. public Car() : base("Black", 2) { // Standard Konstructor und Aufruf des param. Basisklassenkonstruktors
  30. color = "red"; // Feld Initialisierung
  31. }
  32. public Car(string col) : base (col) { // Überladenen paramentrisierten Konstruktor und Aufruf des param. Basisklassenkonstruktors
  33. color = col; // Feld Initialisierung mit Paramater
  34. Console.WriteLine("Car");
  35. }
  36. public new string color; // Feldern
  37. //public int _maxSpeed { get; set;}; // Auto (implementation) property
  38. private int _maxSpeed;
  39. public int MaxSpeed {
  40. get {
  41. return _maxSpeed;
  42. }
  43. }
  44.  
  45. public new void drucke() {
  46. System.Console.WriteLine("Farbe: " + color);
  47. base.drucke();
  48. base.honk(); // Aufruf einer Methode der Basisklasse
  49. }
  50.  
  51. }
  52.  
  53. public class Test
  54. {
  55. public static void Main()
  56. {
  57. Car myObj = new Car("blue"); // Objekt myObj instanziieren
  58. //myObj._maxSpeed = 100;
  59. //myObj.MaxSpeed = 200;
  60. System.Console.WriteLine("Farbe: " + myObj.color);
  61. System.Console.WriteLine("Max Speed: " + myObj.MaxSpeed);
  62. myObj.color = "red";
  63. myObj.drucke();
  64. }
  65. }
Success #stdin #stdout 0.02s 15964KB
stdin
Standard input is empty
stdout
param Base
Car
Farbe: blue
Max Speed: 0
Farbe: red
Vehicle Color:blue
Tuut, tuut
Tuut, tuut