Tag 3 Unterricht und Aufgaben

This commit is contained in:
wbs
2026-08-12 14:15:13 +02:00
parent a969e3cdc7
commit 78ac1b3936
73 changed files with 2928 additions and 0 deletions
@@ -0,0 +1,42 @@
namespace OP6_78_FIAE1;
public class Tag_08_Konstruktoren
{
public static void Main()
{
var auto = new Auto();
var auto2 = new Auto("Orange");
//auto.einfärben("Rot");
Console.WriteLine(auto.lackfarbe);
Console.WriteLine(auto.preis);
}
}
class Produkt
{
public int preis;
public Produkt(int preis)
{
this.preis = preis;
}
}
class Auto : Produkt
{
public string lackfarbe;
public Auto() : this("UNBEMALT")
{
}
public Auto(string lackfarbe) : base(100)
{
einfärben(lackfarbe);
}
public void einfärben(string lackfarbe)
{
this.lackfarbe = lackfarbe;
}
}
+57
View File
@@ -0,0 +1,57 @@
namespace OP6_78_FIAE1;
public class Tag_08_New
{
public static void Main()
{
var autoViaOverride = new ViaOverride.Auto();
autoViaOverride.SetPreis(100);
var produktViaOverride = (ViaOverride.Produkt)autoViaOverride;
produktViaOverride.SetPreis(100);
Console.WriteLine("---");
var autoViaOvershadow = new ViaOvershadow.Auto();
autoViaOvershadow.SetPreis(100);
var produktViaOvershadow = (ViaOvershadow.Produkt)autoViaOvershadow;
produktViaOvershadow.SetPreis(100);
}
class ViaOverride
{
public class Produkt
{
public virtual void SetPreis(int preis)
{
Console.WriteLine(preis);
}
}
public class Auto : Produkt
{
public override void SetPreis(int preis)
{
Console.WriteLine("Dies ist ein Auto mit Preis " + preis);
}
}
}
class ViaOvershadow
{
public class Produkt
{
public void SetPreis(int preis)
{
Console.WriteLine(preis);
}
}
public class Auto : Produkt
{
public new void SetPreis(int preis)
{
Console.WriteLine("Dies ist ein Auto mit Preis " + preis);
}
}
}
}
@@ -0,0 +1,33 @@
namespace OP6_78_FIAE1;
public class Tag_08_Override_Abstract
{
public static void Main()
{
var auto = new Auto();
auto.SetPreis(99);
}
abstract class Produkt
{
protected int preis;
public abstract void SetPreis(int preis); // abstract -> MUST be overridden
}
class Auto: Produkt
{
// ersetzen
public override void SetPreis(int preis)
{
if (preis < 100) throw new ArgumentException("zu günstig");
this.preis = preis;
}
// erweitern
public void fahren()
{
Console.WriteLine("Brumm!");
}
}
}
@@ -0,0 +1,39 @@
namespace OP6_78_FIAE1;
public class Tag_08_Override_Virtual
{
public static void Main()
{
var auto = new Auto();
auto.SetPreis(99);
}
class Produkt
{
protected int preis;
public virtual void SetPreis(int preis) // virtual -> MAY be overridden
{
this.preis = preis;
}
}
class Auto: Produkt
{
// ersetzen
public override void SetPreis(int preis)
{
if (preis < 100) throw new ArgumentException("zu günstig");
this.preis = preis;
//or reuse:
//base.SetPreis(preis);
}
// erweitern
public void fahren()
{
Console.WriteLine("Brumm!");
}
}
}
@@ -0,0 +1,55 @@
namespace OP6_78_FIAE1;
public class Tag_08_Properties
{
public static void Main()
{
var p = new Person("Alice");
//p.Name = "Alice"; // not allowed
Console.WriteLine(p.Name);
p.Age = 18;
Console.WriteLine(p.Age);
Console.WriteLine(p.IstVolljährig);
//p.Age = 17;
Console.WriteLine(p.IstVolljährig2);
}
}
class Person
{
public bool IstVolljährig { get; private set; }
public bool IstVolljährig2 => Age >= 18;
// Exact same as:
// public bool IstVolljährig2 {
// get { return Age >= 18; }
// }
public Person(string name)
{
Name = name;
}
private int _age; // Zusatz: Schauen Sie sich das 'field' Keyword von C# an.
public int Age {
get => _age;
set
{
IstVolljährig = value >= 18;
_age = value;
}
}
public string Name { get; private set; }
// Basically same as:
// private string name;
// public string getName()
// {
// return name;
// }
// private void setName(string value)
// {
// name = value;
// }
}