\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{{[}deleted{]}} \pdfinfo{ /Title (java-f-r-beginner-03-oop.pdf) /Creator (Cheatography) /Author ({[}deleted{]}) /Subject (Java für Beginner \textgreater{} \#03 OOP Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{0B20A3} \definecolor{LightBackground}{HTML}{EFF1F9} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{Java für Beginner \textgreater{} \#03 OOP Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{{[}deleted{]}} via \textcolor{DarkBackground}{\uline{cheatography.com/69240/cs/17462/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}{[}deleted{]} \\ \uline{cheatography.com/deleted-69240} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 15th October, 2018.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{3} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Einleitung}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Java hat einen ziemlich "OOP-Lastigen" Programmier Styl. (Objekt Orientierte Programmierung). Das hei{\ss}t das man Objekte so sehen soll wie man es in der Echten Welt t{\"a}te. \newline % Row Count 4 (+ 4) `In der OOP hat jedes Objekt seine eigene Identit{\"a}t. Ein Apfel ist Ein Objekt, so auch eine Tasse. Auch 2 Äpfel welche gleich aussehen, sind im eigentlichen 2 Seperate Äpfel mit deren eigenen Eigenschaften und Verhalten` \newline % Row Count 9 (+ 5) {\bf{Klassen}} beschreiben wie ein Objekt sein wird / was es haben wird. \newline % Row Count 11 (+ 2) In anderen Worten k{\"o}nnen {\bf{Klassen wie BluePrints angesehen werden.}} \newline % Row Count 13 (+ 2) Man kann die selbe Klasse als BluePrint für unendlich Objekte benutzen. \newline % Row Count 15 (+ 2) Jede Klasse hat einen Namen, Variablen (Attribute) und Methoden (Verhalten). Hier ist ein Beispiels-Bild: \newline % Row Count 18 (+ 3) \seqsplit{https://api.sololearn.com/DownloadFile?id=2429}% Row Count 19 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Funktionen \& Variablen}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{Methoden (Auch Funktionen/Verhalten gennant)}} sind Annsammlungen von Statements um eine Operation durchzuführen. Wie zB. auch die Methode `println(x)` in `System.out`. \newline % Row Count 4 (+ 4) Funktionen die ein `static`-Keyword besitzen {\bf{k{\"o}nnen}} auch nur mit Hilfe der Klasse (BluePrints) aufgerufen werden, aber dann haben diese Methoden auch nur die \seqsplit{`static`-Variablen/Funktionen} der Klasse zu Verfügung. \newline % Row Count 9 (+ 5) `class MyClass \{` \newline % Row Count 10 (+ 1) `~` \newline % Row Count 11 (+ 1) `~public int var = 2;` \newline % Row Count 12 (+ 1) `~` \newline % Row Count 13 (+ 1) `~void hallo()\{ \seqsplit{System.out.println("Hallo");} \}` \newline % Row Count 15 (+ 2) `~int addieren(int x1, int x2)\{ return x1 + x2; \}` \newline % Row Count 17 (+ 2) `~` \newline % Row Count 18 (+ 1) `~static void test()\{` \newline % Row Count 19 (+ 1) `~~this.var= 2;// {\bf{\textgreater{}Fehler: }} Die Variable "var" kann nur eine erstellte Instanz besitzen.` \newline % Row Count 22 (+ 3) `~\}` \newline % Row Count 23 (+ 1) `~` \newline % Row Count 24 (+ 1) `\}` \newline % Row Count 25 (+ 1) {\bf{Variablen}} \newline % Row Count 26 (+ 1) Eine Klasse kann {\bf{"2 Typen von Variablen"}} haben: \newline % Row Count 28 (+ 2) - Variable mit `static`-Keyword, welche für das gesamte BluePrint gilt \newline % Row Count 30 (+ 2) } \tn \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Funktionen \& Variablen (cont)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{- Normale Variable, welche für jedes Objekt anders ist. \newline % Row Count 2 (+ 2) Im oberen Fall ist die Variable `var` eine Objekt-Variable.% Row Count 4 (+ 2) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Siehe {\bf{Konstruktor}} um zu sehen wie man ein neues Objekt erstellt. \newline Siehe {\bf{Getter \& Setter}} für extrigen "Variablen-Schutz".} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{0.89586 cm} x{4.08114 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Zugriff Modifizierer}} \tn % Row 0 \SetRowColor{LightBackground} public & Für Jeden Zugreifbar \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \seqsplit{private} & Nur für die Klasse \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \seqsplit{protected} & Nur für die Klasse und Sub-Klassen \tn % Row Count 4 (+ 2) % Row 3 \SetRowColor{white} static & Keyword, damit man eine Methode/Variable auch nur durch die Klasse/das BluePrint bekommen kann \tn % Row Count 7 (+ 3) % Row 4 \SetRowColor{LightBackground} final & Keyword, dass eine Variable nicht ver{\"a}nderbar ist \tn % Row Count 9 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Konstruktor}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Eine Klasse kann au{\ss}er Funktionen und Methoden noch mehrere Konstrukt{\"o}re haben. \newline % Row Count 2 (+ 2) Ein Konstruktor ist aufgebaut wie eine Methode, aber nur ist er dafür {\bf{ein neues Objekt seines Klassen-BluePrints zu erstellen.}} \newline % Row Count 5 (+ 3) Ein Konstruktor sieht wie folgt aus: (Syntax) \newline % Row Count 6 (+ 1) `public {\emph{KlassenName}}(...)\{ Code \}` \newline % Row Count 7 (+ 1) Ein Beispiel für die Klasse MyClass, welche eine Variable `var` hat. (aus "Getters und Setters"): \newline % Row Count 9 (+ 2) `public MyClass(int wert) \{ // Beim erstellen dieser Klasse muss ein Wert übergeben werden` \newline % Row Count 11 (+ 2) `~this.var = wert; // Setzt die Klassen-Variable "val" auf den mitgelieferten Wert` \newline % Row Count 13 (+ 2) `\}` \newline % Row Count 14 (+ 1) Und nun kann man mit: \newline % Row Count 15 (+ 1) `MyClass instanz1 = new MyClass(224);` \newline % Row Count 16 (+ 1) Eine neue Instanz/Objekt der Klasse `MyClass` erstellen.% Row Count 18 (+ 2) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Getter \& Setter}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{Getter \& Setter}} ist ein Konzept in der Programmierung, wo dafür gedacht ist dass man {\bf{das Setzen einer Variable überprüfen kann}}, um Fehler zu vermeiden. \newline % Row Count 4 (+ 4) Dies macht man, indem man die eigentliche {\bf{Variable auf `private` setzt}}, und eine Methode zum "Setzten" sowie eine zum "Kriegen" erstellt. \newline % Row Count 7 (+ 3) Ein klassisches Beispiel w{\"a}re es, dass man einen `int` macht, der nur im Positiven bereich gesetzt werden darf: \newline % Row Count 10 (+ 3) `class MyClass \{` \newline % Row Count 11 (+ 1) `~` \newline % Row Count 12 (+ 1) `~private int var = 2;` \newline % Row Count 13 (+ 1) `~` \newline % Row Count 14 (+ 1) `~void setVar(int wert)\{` \newline % Row Count 15 (+ 1) `~~if(wert \textless{} 0) \{ wert = 0; \} // Angegebenen Wert vor dem setzten überprüfen und {\"a}ndern` \newline % Row Count 18 (+ 3) `~~ this.var = wert;` \newline % Row Count 19 (+ 1) `~\}` \newline % Row Count 20 (+ 1) `~` \newline % Row Count 21 (+ 1) `~int getVar()\{ return this.var; \}` \newline % Row Count 22 (+ 1) `~` \newline % Row Count 23 (+ 1) `\}` \newline % Row Count 24 (+ 1) Hiermit kann ich mir ziemlich sicher sein, dass mir `.getVar()` immer eine positive Zahl gibt. \newline % Row Count 26 (+ 2) In jedem Java Programm wird diese Programmierweise bevorzugt, was auch gut ist und man sich auch angew{\"o}hnen sollte.% Row Count 29 (+ 3) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Kurz gesagt sind Getter und Setter eine "Idioten-Schutz", damit Variablen keinen nicht vorhergesehen Wert erhalten!} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Wert VS Referenz}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Wenn man einer Methode einen Primitiven-Datentypen übergibt, kann man sich sicher sein dass er nicht ver{\"a}ndert wird. \newline % Row Count 3 (+ 3) Übergibt man jedoch eine Klasse, k{\"o}nnen die Variablen des Objektes ver{\"a}ndert werden. \newline % Row Count 5 (+ 2) Bsp: \newline % Row Count 6 (+ 1) `void doSomething(int num) \{ num = 22; \}` \newline % Row Count 7 (+ 1) `int num = 5`; \newline % Row Count 8 (+ 1) `doSomething(num);` \newline % Row Count 9 (+ 1) \seqsplit{`System.out.println(num);} // Immernoch 5 hier` \newline % Row Count 10 (+ 1) Aber mit einer Klasse: \newline % Row Count 11 (+ 1) `class MyClass \{ int num = 4; \}` \newline % Row Count 12 (+ 1) `void doSomething(MyClass obj) \{ obj.num = 22; \}` \newline % Row Count 13 (+ 1) `MyClass obj = new MyClass();` \newline % Row Count 14 (+ 1) `doSomething(obj);` \newline % Row Count 15 (+ 1) \seqsplit{`System.out.println(obj.num);} // 22`% Row Count 16 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Vererbung}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Vererben einer Klasse/Interfaces hei{\ss}t, dass die Klasse alle Methoden/Variablen hat, von welchem sie erbt. \newline % Row Count 3 (+ 3) Dies sieht dann so aus: \newline % Row Count 4 (+ 1) {\bf{Mutter-Klasse:}} \newline % Row Count 5 (+ 1) `class A \{` \newline % Row Count 6 (+ 1) `~` \newline % Row Count 7 (+ 1) `~public int variable = 2;` \newline % Row Count 8 (+ 1) `~` \newline % Row Count 9 (+ 1) `~public void test()\{ \seqsplit{System.out.println("Hallo} A") \}` \newline % Row Count 11 (+ 2) `~` \newline % Row Count 12 (+ 1) `\}` \newline % Row Count 13 (+ 1) {\bf{Klasse welche von A erbt:}} \newline % Row Count 14 (+ 1) `class B {\bf{extends}} A \{` \newline % Row Count 15 (+ 1) `~` \newline % Row Count 16 (+ 1) `~@Override // Methode von A überschreiben` \newline % Row Count 17 (+ 1) `~public void test()\{ \seqsplit{System.out.println("Hallo} B"); \}` \newline % Row Count 19 (+ 2) `~` \newline % Row Count 20 (+ 1) `\}` \newline % Row Count 21 (+ 1) {\bf{Code Beispiel}} \newline % Row Count 22 (+ 1) `A obj = new A();` \newline % Row Count 23 (+ 1) `obj.test(); // GIbt "Hallo A" aus` \newline % Row Count 24 (+ 1) `obj = new B(); // Wir k{\"o}nnen die Variable "obj" auf ein Objekt vom Typ "B" setzten, weil B von A erbt` \newline % Row Count 27 (+ 3) `obj.test(); // Gibt "Hallo B" aus` \newline % Row Count 28 (+ 1) `obj.variable = 4; // Und weil "obj" immernoch vom Typ "A" ist, hat es auch immer noch seine Variablen` \newline % Row Count 31 (+ 3) } \tn \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Vererbung (cont)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Zudem kann die Klasse, welche erbt, die Methoden auf ein anderes Verhalten umschreiben. (Den Code der Methode umschreiben)% Row Count 3 (+ 3) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}