Die Präsentation wird geladen. Bitte warten

Die Präsentation wird geladen. Bitte warten

Objektorientierung in Java Java-Kurs 2008 - LE 5.

Ähnliche Präsentationen


Präsentation zum Thema: "Objektorientierung in Java Java-Kurs 2008 - LE 5."—  Präsentation transkript:

1 Objektorientierung in Java Java-Kurs 2008 - LE 5

2 2 Objektorientierung in Java Java-Kurs 2008 - LE 5 Human robert = new Human("Robert","pulshead@cs.tu-berlin.de“); Human katrin = new Human("Katrin", "langk@cs.tu-berlin.de");

3 / 37 3 Picture by Friedemann Wulff-Woesten, www.eisenrah.com robert.talk( „Wozu Objektorientierung?“ );

4 / 37 4 Name Alter Haarfarbe Geschlecht robert.talk( „Objekte haben Attribute“ );

5 / 37 5 Name Alter Haarfarbe Geschlecht Picture by Joe Huminiak, flickr.com robert.talk( „Objekte haben Attribute“ );

6 / 37 6 Klasse Mensch Instanzen robert.talk( „Klasse vs. Instanz“ );

7 / 37 7 class Human { String name; int age; } robert.talk( „Klassen definieren“ );

8 / 37 8 class Human { String name; int age; static void main(){ Human robert= new Human(); } robert.talk( „Instanzen erzeugen“ );

9 / 37 9 class Human { String name; int age; } ____________________________________________ Human robert= new Human(); robert.name= "Robert Lubkoll"; robert.age= 29; System.out.println("Name: " + robert.name); System.out.println("Age: " + robert.age); z.B. HumanTest.java Main-Methode Human.java robert.talk( „Zugriff auf Attribute“ );

10 / 37 10 robert.talk( „Compilieren mehrerer Klassen“ ); Human.java HumanTest.java Compilieren: javac Human.java HumanTest.java Ausführen: java HumanTest

11 / 37 11 Picture by Kevin Cole, flickr.com laufen laufen laufen robert.talk( „Methoden implementieren Verhalten“ );

12 / 37 12 class Human { String name; int age; void celebrateBirthday() { this.age= this.age + 1; if(this.age >=18){ //TODO: sex drugs and rock'n'roll } robert.talk( „Methoden in Klassen definieren“ );

13 / 37 13 Human katrin= new Human(); katrin.age= 30; katrin.celebrateBirthday(); System.out.println(„Age: “ + katrin.age); ___________________________________________ Ausgabe Age: 31 robert.talk( „Methoden einer Instanz aufrufen“ );

14 / 37 14 Ich (Instanz) bin ein Mensch (Klasse), ich habe Beine (Attribute), ich kann laufen (Methoden). robert.talk( „Eselsbrücke“ );

15 / 37 15 Picture by TedsBlog, flickr.com robert.talk( „Konstruktoren“ );

16 / 37 16 class Human { String name; int age; Human() { this.name= "unnamed"; this.age= 0; } Human robert= new Human(); robert.talk( „Standard-Konstruktor“ );

17 / 37 17 class Human { String name; int age; Human( String aName, int anAge ) { this.name= aName; this.age= anAge; } Human robert = new Human("Robert", 29); robert.talk( „Konstruktoren mit Parametern“ );

18 / 37 18 Picture by yousoundhollow, flickr.com robert.talk( „Vorsicht: Kollisionsgefahr!“ );

19 / 37 19 BAAAAD class Human { String name; int age; Human(String name){ name= name; age= 0; } Human(String aName){ this.name= aName; this.age= 0; } GOOOOD robert.talk( „Namespace“ );

20 / 37 20 Picture by pizzo, flickr.com katrin.talk( „Objekte haben eine Identität“ );

21

22 / 37 22 Human a = new Human(); Human b = a; a.age = a.age + 1; int a = 0; int b = a; a = a + 1; Ergebnis: a ist: 1 b ist: 0 Ergebnis: a.age ist: 1 b.age ist: 1 !!! katrin.talk( „Objekte sind... anders“ );

23 / 37 23 Referenz a Referenz b katrin.talk( „Referenzen“ );

24 / 37 24 Referenz a Referenz b katrin.talk( „Referenzen“ );

25 / 37 25 Referenz a Referenz b katrin.talk( „Referenzen“ );

26 / 37 26 katrin.talk( „NULL-Referenzen“ );

27 / 37 27 Exception in thread "main" java.lang.NullPointerException at Human.shakeHands(Human.java:20) at Human.main(Human.java:26) katrin.talk( „NULL-Referenzen“ );

28 / 37 28 Human nobody; nobody.celebrateBirthday(); Human nobody = null; nobody.celebrateBirthday(); Human somebody = new Human(); somebody.celebrateBirthday(); Human alsoSomebody = somebody; alsoSomebody.celebrateBirthday(); Compilerfehler ! Laufzeitfehler ! OKOK OKOK katrin.talk( „Variablen initialisieren!“ );

29 / 37 29 Picture by Helga Kopp (Helga_262), flickr.com katrin.talk( „Kapselung“ );

30 / 37 30 Human katrin= new Human(); katrin.age= 30; katrin.celebrateBirthday(); System.out.println(„Age: “ + katrin.age); ___________________________________________ Ausgabe Age: 31 katrin.talk( „direkter Variablenzugriff -“ );

31 / 37 31 Human katrin= new Human(); katrin.age= -2; katrin.celebrateBirthday(); System.out.println(„Age: “ + katrin.age); ___________________________________________ Ausgabe Age: -1 katrin.talk( „Kein Schutz vor undefiniertem Zustand!“ );

32 / 37 32 class Human { String name; int age; void setAge(int anAge) { if(anAge > 0){ this.age= anAge; } int getAge(){ return this.age; } katrin.talk( „Getter- und Setter-Methoden“ );

33 / 37 33 Daten katrin.talk( „Idee von Kapselung“ );

34 / 37 34 int age katrin.talk( „Flexibilität durch Kapselung“ );

35 / 37 35 Date birthdate katrin.talk( „Flexibilität durch Kapselung“ );

36 36 class Human { String name; int age; Human( String aName, int anAge ){ this.name= aName; this.age= anAge; } void celebrateBirthday() { this.age++; } static void main(String [] args) { Human robert = new Human(„Robert“, 29); robert.celebrateBirthday(); }

37 / 37 37 katrin.talk( „Conclusion“ );

38 38 Danke! Quellen: Piktogramme: IIT Bombay http://www.designofsignage.com Fotos: http://www.flickr.com


Herunterladen ppt "Objektorientierung in Java Java-Kurs 2008 - LE 5."

Ähnliche Präsentationen


Google-Anzeigen