Université de Bordeaux
Licence Sciences & Technologies - Semestre 4
J1IN4W01 : Programmation 2
TD6
Test1AlgoSelection
class A {
public void f() {
System.out.println("methode f, classe A");
}
}
class B extends A {
public void f() {
System.out.println("methode f, classe B");
}
}
public class Test1AlgoSelection {
public static void main(String[] args) {
A a = new A();
a.f();
B b = new B();
b.f();
A a1 = new B();
a1.f();
}
}
Test2AlgoSelection
class A {
public void f() {
System.out.println("methode f, classe A");
}
public void g() {
f();
}
}
class B extends A {
public void f() {
System.out.println("methode f, classe B");
}
}
public class Test2AlgoSelection {
public static void main(String[] args) {
A a = new A();
a.g();
B b = new B();
b.g();
A a1 = new B();
a1.g();
}
}
Test3AlgoSelection
class A {
public void f(A a) {
System.out.println("methode f, classe A");
}
public void g(A a) {
System.out.println("methode g, classe A");
}
}
class B extends A {
public void f(B b) {
System.out.println("methode f, classe B");
}
public void g(A a) {
System.out.println("methode g, classe B");
}
}
public class Test3AlgoSelection {
public static void main(String[] args) {
A a = new A();
A a1 = new A();
A a2 = new B();
B b = new B();
B b1 = new B();
a1.f(a);
a1.f(a2);
a1.f(b1);
a2.f(a);
a2.f(a2);
a2.f(b1);
b.f(a);
b.f(a2);
b.f(b1);
a1.g(a);
a1.g(a2);
a1.g(b1);
a2.g(a);
a2.g(a2);
a2.g(b1);
b.g(a);
b.g(a2);
b.g(b1);
}
}
Lapin
class Nourriture {
}
class Herbe extends Nourriture {
}
class Animal {
public void mange (Nourriture n) {
System.out.println("Je mange toute sorte de nourriture");
}
}
class Lapin extends Animal{
public void mange (Herbe h) {
System.out.println("Je mange de l'herbe");
}
}
public class AppliLapinMange{
public static void main(String[] args) {
Herbe h = new Herbe();
Nourriture n = h;
Lapin l = new Lapin();
l.mange(h);
l.mange(n);
Animal a = l;
a.mange(h);
a.mange(n);
}
}
Test4AlgoSelection
class A{
int i=1;
int f(){
return i;
}
}
class B extends A{
int i=5;
int f(){
return super.f()+i;
}
}
class Test{
public static void main(String[] args){
A b=new B();
System.out.println(b.i);
System.out.println(b.f());
}
}