Verified Commit 94dc9d56 authored by Jonas Sulzer's avatar Jonas Sulzer 馃帗
Browse files

馃摝 NEW: add Typage.java as given in enonc茅e



Signed-off-by: Jonas Sulzer's avatarJonas Sulzer <jonas@violoncello.ch>
parent 85c182e8
package Ex_04;
class Typage {
public static void main(String[] args) {
A[] a = new A[5];
a[0] = new B(8);
a[1] = new B(8);
a[2] = new C(4, 2);
a[3] = new C(4, 2);
a[4] = new D();
int somme = 0;
for (int i = 0; i < a.length; i++) {
if (a[i] instanceof D) {
System.out.println(((D) a[i]).d);
} else {
if (a[i] instanceof B) {
somme = somme + ma(a[i]);
if (a[i] instanceof C) {
C c = a[i];
c.mc();
}
}
}
System.out.println(somme);
}
}
static int ma(B x) {
return x.b;
}
}
class A {
public A() {};
}
class B extends A {
protected int b;
public B(int b) {
super();
this.b = b;
}
}
class C extends B {
protected int c;
public C(int b, int c) {
super(b);
c = b;
}
public void mc() {
System.out.println("Bzz..");
}
}
class D extends A {
protected int d = 20;
public D() {
super();
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment