Java - Programmierung - Objektorientierte Programmierung 1 // Klassen und Objekte public class KlaObj public static void main(string args []) Klasse1 a; a = new Klasse1("heute", 47); Klasse1 b = new Klasse1 ("morgen", 11); a.show(); b.show(); new Klasse1("gestern", 1998).show(); new Klasse1("jetzt").show(); class Klasse1 int i; String s; Klasse1 (String string, int i) s = string; this.i = i; Klasse1 (String string) s = string; i = 123; void show() System.out.println (s + + i + " in Klasse1"); heute 47 in Klasse1 morgen 11 in Klasse1 gestern 1998 in Klasse1 jetzt 123 in Klasse1
Java - Programmierung - Objektorientierte Programmierung 2 // static Member in Klassen public class Static public static void main(string args []) System.out.println (A.count + " *** " + A.pi); // System.out.println (A.s); keine static Variable A a; a = new A ("heute"); System.out.println (a.count); A b = new A ("morgen"); a.show(); b.show(); new A("jetzt").show(); class A static int count = -1; static double pi; String s; static count++; pi = Math.PI; A (String string) s = string; count++; void show() ystem.out.print (count + " Objekte von A: hier "); System.out.println (s + \t + pi); 0 *** 3.14159 1 2 Objekte von A: hier heute 3.14159 2 Objekte von A: hier morgen 3.14159 3 Objekte von A: hier jetzt 3.14159
Java - Programmierung - Objektorientierte Programmierung 3 // Referenzen auf Objekte public class Ref public static void main(string args []) Kreis k1, k2; k1 = new Kreis (1, 2, 3); k2 = new Kreis (4, 5, 6); k1.show(); k2.show(); change_radius(k1, 7); k1.show(); k2 = k1; k2.show(); k2.move(4, 5); k1.show(); k2.show(); static void change_radius (Kreis k, int new_radius) k.r = new_radius; Kreis mit Mittelpunkt (1, 2) und Radius 3 Kreis mit Mittelpunkt (4, 5) und Radius 6 Kreis mit Mittelpunkt (1, 2) und Radius 7 Kreis mit Mittelpunkt (1, 2) und Radius 7 Kreis mit Mittelpunkt (4, 5) und Radius 7 Kreis mit Mittelpunkt (4, 5) und Radius 7 class Kreis int x, y, r; Kreis (int mx, int y, int r) x = mx; this.y = y; this.r = r; void move( int new_x, int new_y) x = new_x; y = new_y; void show() System.out.print ("Kreis mit Mittelpunkt (" + x + ", " + y); System.out.println (") und Radius " + r);
Java - Programmierung - Objektorientierte Programmierung 4 // Vererbung public class Vererb public static void main(string args []) Rotationskoerper rk; rk = new Rotationskoerper(5, 3); rk.print_art(); rk.show(); Kegel keg = new Kegel(5, 3); keg.print_art(); keg.show(); System.out.println ("Kegelvolumen " + keg. getvolumen()); class Rotationskoerper int durchmesser, hoehe; Rotationskoerper (int d, int h) durchmesser = d; hoehe = h; void print_art() System.out.print ("Rotationskoerper "); void show() System.out.print ("mit Durchmesser " + durchmesser); System.out.println (" und Hoehe " + hoehe); class Kegel extends Rotationskoerper Kegel (int d, int h) super (d, h); void print_art() System.out.print ("Kegel "); double getvolumen() return Math.PI / 12 * durchmesser * durchmesser * hoehe; Rotationskoerper mit Durchmesser 5 und Hoehe 3 Kegel mit Durchmesser 5 und Hoehe 3 Kegelvolumen 19.634954084936204
Java - Programmierung - Objektorientierte Programmierung 5 // Polymorphie public class Polym public static void main(string args []) int i; Rotationskoerper rk[] = new Rotationskoerper[3]; rk[0] = new Rotationskoerper(5, 3); rk[1] = new Kegel(6, 4); rk[2] = new Rotationskoerper(7, 5); for (i = 0; i < rk.length; i++) rk[i].print_art(); rk[i].show(); System.out.println ("Kegelvolumen " + ((Kegel)rk[1]). getvolumen()); class Rotationskoerper int durchmesser, hoehe; Rotationskoerper (int d, int h) durchmesser = d; hoehe = h; void print_art() System.out.println ("Rotationskoerper "); void show() System.out.print ("\tmit Durchmesser " + durchmesser); System.out.println (" und Hoehe " + hoehe); class Kegel extends Rotationskoerper Kegel (int d, int h) super (d, h); void print_art() System.out.println ("Kegel "); double getvolumen() return Math.PI / 12 * durchmesser * durchmesser * hoehe; Rotationskoerper mit Durchmesser 5 und Hoehe 3 Kegel mit Durchmesser 6 und Hoehe 4 Rotationskoerper mit Durchmesser 7 und Hoehe 5 Kegelvolumen 37.69911184307752
// Exception Java - Programmierung - Objektorientierte Programmierung 6 public class Except public static void main(string args[]) int i; Ausnahme a = new Ausnahme(); for(i = 0; i < 5; i++) try a.test(i); catch (A2 ausn2) System.out.println(ausn2); catch (A1 ausn1) System.out.println(ausn1); catch (Exception e) System.out.println(e); finally System.out.println("allgemein " + i); class Ausnahme void test(int i) throws A1, A2 if (i == 4 i == 9) throw new A1 ("Quadratzahl"); else if (i % 3 == 0) throw new A2 ("durch drei teilbar"); class A1 extends Exception A1 (String s) super (s); A2: durch drei teilbar allgemein 0 allgemein 1 allgemein 2 A2: durch drei teilbar allgemein 3 A1: Quadratzahl allgemein 4 class A2 extends Exception A2 (String s) super (s);
Java - Programmierung - Objektorientierte Programmierung 7 // Dynamische Objekte und abstrakte Klasse public class Dyn_obj public static void main(string args[]) Object obj = AddClass.load (args[0]); if ( obj!= null ) ((A_Base)obj).work(); class AddClass static Object load (String cl_name) Class new_cl = null; try new_cl = Class.forName(cl_name); catch (Exception e) System.out.print ("Klasse "+ cl_name); System.out.println(".class nicht gefunden"); // return null; -- sofortiger Abbruch moeglich try return new_cl.newinstance(); catch (Exception e) System.out.println("Objekt nicht erzeugt"); return null; abstract class A_Base abstract void work (); class Maus extends A_Base void work () System.out.println ("Wo ist der Speck?"); class Katze extends A_Base void work () System.out.println ("Wo ist die Maus?"); C:\>java Dyn_obj Hund Klasse Hund.class nicht gefunden Objekt nicht erzeugt C:\>java Dyn_obj Katze Wo ist die Maus? C:\>java Dyn_obj Maus Wo ist der Speck?
Java - Programmierung - Objektorientierte Programmierung 8 // final - Daten und Methoden public class Final public static void main(string args []) Zylinder z; z = new Zylinder(10.0, 2.5, 3.3, 4.4); System.out.println ("Zylindervolumen " + z.getvolumen()); class Kreis double radius, mx, my; Zylindervolumen 196.34954084936209 final double pi = Math.PI; Kreis (double r, double x, double y) // pi = 3.14; Can t assign a value to a final variable: pi radius = r; mx = x; my = y; final double getflaeche() return pi * radius * radius; class Zylinder extends Kreis double hoehe; Zylinder (double h, double r, double x, double y) super (r, x, y); hoehe = h; /* Final methods can t be overriden. Method double getflaeche() is final in Kreis double getflaeche() return 3.0 * radius * radius; */ double getvolumen() return getflaeche() * hoehe;
Java - Programmierung - Objektorientierte Programmierung 9 // Interface - Class public class IC public static void main(string args []) Z oz = new Z(1, 2, 3, 4); oz.work(); class A implements BIF int a; A (int x) a = x; public void show_bif() (A 1) bif = X (B 2) b*i*f = X (A 3) bif = X (C 3) #i#f# c = 4X System.out.println ("(A " + a + ") bif = " + bif); class B implements BIF int b; B (int x) b = x; public void show_bif() System.out.println ("(B " + b + ") b*i*f = " + bif); class C extends A implements IF int c; C (int x, int y) super(x); c = y; public void show_if() System.out.println ("(C " + a + ") #i#f# c = " + c + bif); class Z A oa; B ob; C oc; Z (int x1, int x2, int x3, int x4) oa = new A(x1); ob = new B(x2); oc = new C(x3, x4); void work() use_bif(oa); use_bif(ob); use_bif(oc); use_if(oc); void use_bif(bif obj) obj.show_bif(); void use_if(if obj) obj.show_if(); interface BIF char bif = X ; void show_bif(); interface IF extends BIF void show_if();
Java - Programmierung - Objektorientierte Programmierung 10 // Packages import Starter.AddClass; import Tiere.*; public class Verhalten public static void main(string args[]) Object obj = AddClass.load (args[0]); if ( obj!= null ) ((Tier_Base)obj).work(); package Tiere; public abstract class Tier_Base public abstract void work(); package Tiere; public class Katze extends Tier_Base public void work () System.out.println ("Wo ist die Maus?"); package Starter; package Tiere; public class AddClass public static Object load (String cl_name) public class Maus extends Tier_Base Class new_cl = null; public void work () try new_cl = Class.forName(cl_name); System.out.println catch (Exception e) ("Wo ist der Speck?"); System.out.print ("Klasse "+ cl_name); System.out.println(".class nicht gefunden") C:\>java Verhalten Tiere.Hund try return new_cl.newinstance(); Klasse Tiere.Hund.class nicht gefunden catch (Exception e) Objekt nicht erzeugt System.out.println("Objekt nicht C:\>java Verhalten Tiere.Katze erzeugt"); Wo ist die Maus? return null; C:\>java Verhalten Tiere.Maus Wo ist der Speck?
Java - Programmierung - Objektorientierte Programmierung 11 // Zugriffsrechte import ZA.ZA; public class Zugriff public static void main (String args []) ZA a = new ZA((byte)1, (byte)2, (byte)3); ZB b = new ZB(); a.show(); // a.priv++; not accessible from Zugriff // a.prot++; not accessible from Zugriff a.pub++; // a.ohne++; not accessible from Zugriff a.show(); b.dec_2(); // b.dec_3(); No method matching dec_3() found in ZB b.show(); import ZA.ZA; class ZB extends ZA // void dec_1 () priv--; not accessible from ZB protected void dec_2 () prot--; private void dec_3 () pub--; // public void dec_4 () ohne--; // not accessible from ZB package ZA; public class ZA private byte priv; protected byte prot; public byte pub; byte ohne; public ZA (byte a, byte b, byte c) priv = a; prot = b; pub = c; ohne = -1; public ZA () priv = 0; prot = 8; pub = 15; ohne = -2; public void show() System.out.println ("priv = " + priv + " prot = " + prot + " pub = " + pub + "\tohne = " + ohne); priv = 1 prot = 2 pub = 3 ohne = -1 priv = 1 prot = 2 pub = 4 ohne = -1 priv = 0 prot = 7 pub = 15 ohne = -2