diff options
| author | Florian Fischer <florian.fischer@muhq.space> | 2023-04-25 22:56:27 +0200 |
|---|---|---|
| committer | Florian Fischer <florian.fischer@muhq.space> | 2023-04-25 22:56:27 +0200 |
| commit | c8af53de24e3e7535726138cb5ca8caeaf6d8cb1 (patch) | |
| tree | 0e3173dfb3606d8173b43d912156b6da8c4b46c1 /Info/10/2/Polymorphismus/slides.md | |
| parent | 1b2dd19ec6a75e1fe0b6275def842b6753a38f8f (diff) | |
| download | unterricht-c8af53de24e3e7535726138cb5ca8caeaf6d8cb1.tar.gz unterricht-c8af53de24e3e7535726138cb5ca8caeaf6d8cb1.zip | |
add my polymorphism slides
Diffstat (limited to 'Info/10/2/Polymorphismus/slides.md')
| -rw-r--r-- | Info/10/2/Polymorphismus/slides.md | 651 |
1 files changed, 651 insertions, 0 deletions
diff --git a/Info/10/2/Polymorphismus/slides.md b/Info/10/2/Polymorphismus/slides.md new file mode 100644 index 00000000..5b524b65 --- /dev/null +++ b/Info/10/2/Polymorphismus/slides.md @@ -0,0 +1,651 @@ +--- +author: Florian Fischer +theme: Rochester +aspectratio: 169 +header-includes: | + \usepackage[school]{pgf-umlcd} + \usepackage{unicode-math} + \usepackage{pifont} + \usepackage{tikz} + \usepackage{minted} + \usetikzlibrary{calc} + \pgfdeclareradialshading[droplet color]{droplet}{\pgfqpoint{-10bp}{-10bp}}{color(0bp)=(droplet color!50!white); color(9bp)=(droplet color!75!white); color(18bp)=(droplet color!85!black); color(25bp)=(droplet color); color(50bp)=(droplet color!50!white)} + \colorlet{droplet color}{blue!50!cyan} +--- + +## Lessons learned? + +::: columns +:::: {.column} + +:::: +:::: column + +:::: +::: + +## Erkenntnis + +Auch in Software müssen, wie in der Realität, Zugriff fest geregelt sein. + +\vspace{1cm} +\pause + +### Dazu bieten Java die Zugrifsmodifikatoren +* `private`{.java} - Nur Zugriff innerhalb der selben Klasse +* `public`{.java} - Uneingeschränkter Zugriff + +\pause + +\begin{tikzpicture}[overlay, remember picture] + \node[rotate=-20,scale=3] at (12, 3) {\Huge \color{red} \textbf{!}}; +\end{tikzpicture} + +## Grundsatz der Datenkapselung + +Zwei Aspekte sind wichtig: + +\vspace{0.5cm} + +> 1. **Nur** Objekt selbst sollte Zugriff auf seine Daten haben +> 2. **Benötigter** Zugriff von außen soll über von dem Objekt bereitgestellte Methoden erfolgen + +\begin{tikzpicture}[overlay, remember picture] + \only<3> { + \node[rotate=-30,scale=3] at (12, 3) {\Huge \color{red} \textbf{!}}; + } +\end{tikzpicture} + +## Datenkapselung Anwendung {.t} + +```{.java .linenos} +class Bankkonto { + public double kontostand; + private kontonummer; + + public gibBetrag() { return kontostand; } + private gibKontoNummer() { return kontonummer; } +} +``` + +## Datenkapselung Anwendung {.t} + +```{.java .linenos highlightlines=\{2\}} +class Bankkonto { + public double kontostand; + private kontonummer; + + public gibBetrag() { return kontostand; } + private gibKontoNummer() { return kontonummer; } +} +``` + +## Datenkapselung Anwendung {.t} + +```{.java .linenos highlightlines=\{2\}} +class Bankkonto { + public double kontostand; + private kontonummer; + + public gibBetrag() { return kontostand; } + private gibKontoNummer() { return kontonummer; } +} +``` + +\vspace{1cm} + +* Zeile 2: Uneingeschränkter Zugriff + +## Datenkapselung Anwendung {.t} + +```{.java .linenos highlightlines=\{2,6\}} +class Bankkonto { + public double kontostand; + private kontonummer; + + public gibBetrag() { return kontostand; } + private gibKontoNummer() { return kontonummer; } +} +``` + +\vspace{1cm} + +* Zeile 2: Uneingeschränkter Zugriff + +## Datenkapselung Anwendung {.t} + +```{.java .linenos highlightlines=\{2,6\}} +class Bankkonto { + public double kontostand; + private kontonummer; + + public gibBetrag() { return kontostand; } + private gibKontoNummer() { return kontonummer; } +} +``` + +\vspace{1cm} + +* Zeile 2: Uneingeschränkter Zugriff +* Zeile 6: Kein Zugriff von außen möglich + +## Lessons learned! + +::: columns +:::: {.column} + +:::: +:::: column + +:::: +::: + +\begin{tikzpicture}[ + overlay, + remember picture, + raindrop/.pic={ + code={\tikzset{scale=1/10} + \shade [shading=droplet] + (0,0) .. controls ++(0,-1) and ++(0,1) .. (1,-2) + arc (360:180:1) + .. controls ++(0,1) and ++(0,-1) .. (0,0) -- cycle; + } +}] + \only<2-> { + \path (3.65,3) pic {raindrop}; + \path (3.85,2.8) pic {raindrop}; + } + + \draw[transform canvas={shift={(8,1)}}, scale=2, line width=2pt] (3ex,2.5ex) -- (1.5ex,3ex) -- (0ex,2.5ex) to [out=-90, in=165] (1.5ex,0ex) to [out=15, in=-90] cycle (1.5ex,1.5ex) node {\Large \ding{51}}; +\end{tikzpicture} + +## Wdh. Vererbung - Auto + +{.center height=85%} + +## Wdh. Vererbung - Auto + +\begin{tikzpicture} + \begin{class}[text width=3cm]{AUTO}{0,0} + \attribute{...} + \operation{...} + \end{class} + + \begin{class}[text width=3cm]{BAUTEIL}{9,0} + \attribute{...} + \operation{...} + \end{class} + + \association{AUTO}{1}{besteht aus >}{BAUTEIL}{< verbaut in}{n} +\end{tikzpicture} + +\vspace{1cm} +Hilft uns diese Modellierung beim Konstruieren eines Autos? + +## Wdh. Vererbung Spezialisierung - Auto + +\begin{tikzpicture} + \begin{class}[text width=3cm]{AUTO}{0, 2.5} + \attribute{...} + \operation{...} + \end{class} + + \begin{class}[text width=3cm]{BAUTEIL}{5, 2.5} + \attribute{...} + \operation{...} + \end{class} + + \association{AUTO}{1}{}{BAUTEIL}{n}{} + + \begin{class}[text width=4cm]{Motor}{10, 0} + \inherit{BAUTEIL} + \attribute{...} + \operation{...} + \end{class} + + \begin{class}[text width=4cm]{Felge}{10, 2.5} + \inherit{BAUTEIL} + \attribute{...} + \operation{...} + \end{class} + + \begin{class}[text width=4cm]{Windschutzscheibe}{10, 5} + \inherit{BAUTEIL} + \attribute{...} + \operation{...} + \end{class} +\end{tikzpicture} + +## Wdh. Vererbung in Java {.t} + +Das Java Schlüsselwort ... + + +## Wdh. Vererbung in Java {.t} + +Das Java Schlüsselwort `extends`{.java} drückt eine Vererbungsbeziehung zwischen zwei Klassen aus. + +\vspace{0.5cm} + +\pause + +::: {.block} +### Syntax der Klassendefinition +`class Unterklasse extends Oberklasse {...}`{.java} +::: + +\vspace{0.5cm} + +\pause + +Die Unterklasse erbt alle Methoden und Attribute der Oberklasse. +Man spricht: "Eine Felge *ist* ein Bauteil". + +\pause + +\begin{tikzpicture}[overlay, remember picture] + \node[rotate=-30,scale=3] at (12, 3) {\Huge \color{red} \textbf{!}}; +\end{tikzpicture} + +## Wdh. Vererbung in Java + +::: columns +:::: {.column width=40%} +```java +class Oberklasse { + private int zahl; + + public void Oberklasse(int n) + { + zahl = n; + } + + public void gibZahl() { + return zahl; + } +} +``` +:::: + +:::: {.column width=50%} +```java +class Unterklasse extends Oberklasse { + public void Unterklasse(int n) { + // Oberklasse Initialisierung? + } +} +``` +:::: +::: + +## Wdh. Vererbung in Java + +::: columns +:::: {.column width=40%} +```java +class Oberklasse { + private int zahl; + + public void Oberklasse(int n) + { + zahl = n; + } + + public void gibZahl() { + return zahl; + } +} +``` +:::: + +:::: {.column width=50%} +```{.java highlightlines=\{3\}} +class Unterklasse extends Oberklasse { + public void Unterklasse(int n) { + super(n); + } +} +``` +:::: +::: + +## Wdh. Vererbung in Java {.t} + +\vspace{1cm} + +Die Oberklasse ist über das Java Schlüsselwort ... benutzbar. + +## Wdh. Vererbung in Java {.t} + +\vspace{1cm} + +Die Oberklasse ist über das Java Schlüsselwort `super`{.java} benutzbar. + +\vspace{1cm} +\pause + +### Beispiele + +* `super(...)`{.java} Konstruktoraufruf der Oberklasse +* `super.zahl`{.java} Attribut der Oberklasse +* `super.zahlGeben()`{.java} Methodenaufruf der Oberklasse + +\pause + +\begin{tikzpicture}[overlay, remember picture] + \node[rotate=-30,scale=3] at (12, 3) {\Huge \color{red} \textbf{!}}; +\end{tikzpicture} + +## Vererbung - Problem + +\begin{tikzpicture} + \begin{class}[text width=4cm]{Sparkonto}{0,0} + \attribute{kontostand} + \attribute{kontonummer} + \attribute{zinssatz} + \operation{Einzahlen(betrag)} + \operation{Abheben(betrag)} + \operation{KontostandGeben()} + \operation{Verzinsen()} + \end{class} + + \begin{class}[text width=4cm]{Girokonto}{5,0} + \attribute{kontostand} + \attribute{kontonummer} + \operation{Einzahlen(betrag)} + \operation{Abheben(betrag)} + \operation{KontostandGeben()} + \end{class} + + \begin{class}[text width=4cm]{Firmenkonto}{10,0} + \attribute{kontostand} + \attribute{kontonummer} + \attribute{dispokredit} + \operation{Einzahlen(betrag)} + \operation{Abheben(betrag)} + \operation{KontostandGeben()} + \operation{DispokreditSetzen(wert)} + \end{class} +\end{tikzpicture} + +\pause + +Mögliche Probleme dieser Modellierung? + +## Vererbung - Generalisierung + +\resizebox{!}{0.5\textheight}{ +\begin{tikzpicture} + \begin{class}[text width=4cm]{Sparkonto}{0,0} + \attribute{kontostand} + \attribute{kontonummer} + \attribute{zinssatz} + \operation{Einzahlen(betrag)} + \operation{Abheben(betrag)} + \operation{KontostandGeben()} + \operation{Verzinsen()} + \end{class} + + \begin{class}[text width=4cm]{Girokonto}{5,0} + \attribute{kontostand} + \attribute{kontonummer} + \operation{Einzahlen(betrag)} + \operation{Abheben(betrag)} + \operation{KontostandGeben()} + \end{class} + + \begin{class}[text width=4cm]{Firmenkonto}{10,0} + \attribute{kontostand} + \attribute{kontonummer} + \attribute{dispokredit} + \operation{Einzahlen(betrag)} + \operation{Abheben(betrag)} + \operation{KontostandGeben()} + \operation{DispokreditSetzen(wert)} + \end{class} +\end{tikzpicture} +} + +**Idee**: Auslagern der gemeinsamen Attribute und Methoden in eine Oberklasse + +**Arbeitsauftrag 3min**: Zeichne ein Klassendiagramm mit einer gemeinsamen Oberklasse + +## Generalisierung - KONTO + +\begin{tikzpicture} + \node[rotate=30,scale=3] at (0, 3) {\Huge \color{red} \textbf{!}}; + + \begin{class}[text width=4cm]{KONTO}{5,4} + \attribute{kontostand} + \attribute{kontonummer} + \operation{Einzahlen(betrag)} + \operation{Abheben(betrag)} + \operation{KontostandGeben()} + \end{class} + + \begin{class}[text width=4cm]{SPARKONTO}{0,0} + \inherit{KONTO} + \attribute{zinssatz} + \operation{Verzinsen()} + \end{class} + + + \begin{class}[text width=4cm]{GIROKONTO}{5,0} + \inherit{KONTO} + \end{class} + + \begin{class}[text width=4cm]{FIRMENKONTO}{10,0} + \inherit{KONTO} + \attribute{dispokredit} + \operation{DispokreditSetzen(wert)} + \end{class} +\end{tikzpicture} + +## Generalisierung - Java + +\small + +::: columns +:::: {.column width=50%} +```java +class Konto { + private int kontostand; + private int kontonummer; + + public void Konto(int kNr) { + kontostand = 0; + kontonummer = kNr; + } + + public void Einzahlen(int betrag) { + ... +} +``` +:::: + +:::: {.column width=50%} +```java +// Klassendefinition ? + private double zinssatz; + + public void Sparkonto(int kNr, + double zs) { + // KONTO Initialisierung? + zinssatz = zs; + } + + public void Verzinsen() { + ... +} +``` +:::: +::: + +## Generalisierung - Java + +\small + +::: columns +:::: {.column width=50%} +```java +class Konto { + private int kontostand; + private int kontonummer; + + public void Konto(int kNr) { + kontostand = 0; + kontonummer = kNr; + } + + public void Einzahlen(int betrag) { + ... +} +``` +:::: + +:::: {.column width=50%} +```{.java highlightlines=\{1\}} +class Sparkonto extends Konto { + private double zinssatz; + + public void Sparkonto(int kNr, + double zs) { + super(kNr); + zinssatz = zs; + } + + public void Verzinsen() { + ... +} +``` +:::: +::: + +## Generalisierung - Java + +\small + +::: columns +:::: {.column width=50%} +```java +class Konto { + private int kontostand; + private int kontonummer; + + public void Konto(int kNr) { + kontostand = 0; + kontonummer = kNr; + } + + public void Einzahlen(int betrag) { + ... +} +``` +:::: + +:::: {.column width=50%} +```{.java highlightlines=\{1,6\}} +class Sparkonto extends KONTO { + private double zinssatz; + + public void Sparkonto(int kNr, + double zs) { + super(kNr); + zinssatz = zs; + } + + public void Verzinsen() { + ... +} +``` +:::: +::: + +## Polymorphismus + +\begin{center} +griechisch Vielgestaltigkeit +\end{center} + +{.center} + +## Arbeitsauftrag Tierheimverwaltung + +* **AUFGABE 0:** Mache dich kurz mit dem Projekt Tierheim in der Online-IDE vertraut +* **AUFGABE 1:** Überlege dir wie du unterschiedliche Tiere im Tierheim speicherst und setze deine Überlegung um +* **AUFGABE 2:** Begründe warum Pluto (Objekt der Klasse Hund) nicht ins Tierheim aufgenommen werden kann + +## Polymorphismus + +*ist die Eigenschaft, dass ein **Referenzattribut** nicht nur Objekte der als Datentyp angegebenen Klasse referenzieren kann, sondern auch Objekte **aller** zu dieser Klasse existierenden **Unterklassen**.* + +## Polymorphismus + +\begin{tikzpicture}[scale=0.5] + \begin{object}[text width=3.5cm]{s1}{0,0} + \instanceOf{Tierheim} + \attribute{Tiere[0]} + \attribute{...} + \end{object} + + \begin{object}[text width=4cm]{a1}{12,0} + \instanceOf{Tier} + \attribute{name="Wolle"} + \attribute{geschlecht="ungewiss"} + \end{object} + + \begin{object}[text width=4cm]{a1}{12,5} + \instanceOf{Hund} + \attribute{name="Pluto" } + \attribute{geschlecht="maennlich"} + \attribute{rasse="comic"} + \end{object} + + \only<2->{ + \node[fill=black, circle, minimum size=0.075cm] (circle) at (2.2, -1.75) {}; + } + \only<2> { + \draw[line width=2pt, -latex] (circle) edge [bend right=15] (8, -0.5) {}; + } + \only<3> { + \draw[line width=2pt, -latex] (circle) edge [bend left=30] (8, 4.5) {}; + } +\end{tikzpicture} + + +## Arbeitsauftrag Tierheimverwaltung Vorsetzung + +* **AUFGABE 3:** Passe die Klasse Hund entsprechend an +* **AUFGABE 4:** Implementiere die Methode `Tierheim.besuchen()` und überlege dir welche Ausgaben das Programm machen wird. Überprüfe deine Vermutung in dem du das Programm ausführst. + +## Polymorphismus + +*ist die Eigenschaft, dass ein **Referenzattribut** nicht nur Objekte der als Datentyp angegebenen Klasse referenzieren kann, sondern auch Objekte **aller** zu dieser Klasse existierenden **Unterklassen**.* + +\vspace{0.25cm} + +*Wird ein **Objekt einer Unterklasse** über dieses Referenzattribut referenziert, so werden Attribute und Methoden verwendet, wie sie in **seiner Klasse** bekannt sind (und nicht die überschriebenen Methoden der Oberklasse).* + +\pause + +\begin{tikzpicture}[overlay, remember picture] + \node[rotate=-20,scale=3] at (12, -0.5) {\Huge \color{red} \textbf{!}}; +\end{tikzpicture} + +## Beispiel + +Was gibt dieses Programm aus? + +```java +Tier pluto = new Hund("pluto", "maennlich", "comic"); +pluto.gibLaut(); +``` + +\pause + +::: {.block} +### Ausgabe +`Wuff` +::: + +Da der Polymorphismus dafür sorgt das die Methode wie sie in der Unterklasse `Hund` bekannt ist ausgeführt wird. |
