In [ ]:
public class Value {
public int number;
public Value (int number) {
this.number = number;
}
}
In [ ]:
public class Services {
public static void zero(Value x) {
x.number = 0;
}
}
In [ ]:
Value a = new Value(15),
b = new Value(10),
c,
d;
b = a;
c = a;
Services.zero(a);
d = a;
System.out.println("a: " + a.number);
System.out.println("b: " + b.number);
System.out.println("c: " + c.number);
System.out.println("d: " + d.number);
In [ ]:
public class A {
public String toString() {
return "Eu sou A";
}
}
In [ ]:
public class B extends A {
public String toString() {
return "Eu sou B";
}
public int getIdade() {
return 45;
}
}
In [ ]:
A var1 = new A();
B var2 = var1;
System.out.println(var2);
In [ ]:
B var1 = new B();
A var2 = var1;
System.out.println(var2);
In [ ]:
B var1 = new B();
A var2 = var1;
System.out.println(var2.getIdade());
In [ ]:
B var1 = new B();
A var2 = var1;
System.out.println(var1.getIdade());
In [ ]:
public class C1 {
protected int valor;
public C1(int valor) {
this.valor = valor;
}
public String toString() {
return Integer.toString(valor);
}
}
In [ ]:
public class C2 extends C1 {
public C2(int valor) {
super(valor * 2);
}
}
In [ ]:
public class C3 extends C2 {
public C3(int valor) {
super(valor);
}
public String toString() {
return Integer.toString(valor / 2);
}
}
In [ ]:
public class C4 extends C1 {
public C4(int valor) {
super(valor);
}
public String toString() {
return Integer.toString(valor / 2);
}
}
In [ ]:
C1 cs[] = {new C1(50),
new C2(30),
new C3(40),
new C4(20)};
for (int c = 0; c < cs.length; c++)
System.out.println(cs[c]);