TECHNOLOGIE
أهلا و سهلا بالزوار
TECHNOLOGIE
أهلا و سهلا بالزوار
TECHNOLOGIE
هل تريد التفاعل مع هذه المساهمة؟ كل ما عليك هو إنشاء حساب جديد ببضع خطوات أو تسجيل الدخول للمتابعة.


TECHNOLOGIE
 
الرئيسيةأحدث الصورالتسجيلدخول

 

 Exercice JAVA masquage (shadowing) et héritage

اذهب الى الأسفل 
كاتب الموضوعرسالة
Admin
المدير
المدير
Admin


ذكر عدد المساهمات : 138
نقاط : 454
السٌّمعَة : 0
تاريخ التسجيل : 30/01/2011

Exercice JAVA masquage (shadowing) et héritage Empty
مُساهمةموضوع: Exercice JAVA masquage (shadowing) et héritage   Exercice JAVA masquage (shadowing) et héritage Emptyالسبت أغسطس 06, 2011 6:05 pm

Indiquer l'affichage du programme. Attention aux situations de masquage.


class Alphabet {
public static void main(String args[]) {
A[] as = new A[3];
as[0] = new A(1);
as[1] = new B(2);
as[2] = new C(3);

for (int i = 0; i < as.length; i++) {
as[i].afficherClasse();
System.out.println("-----");
}

for (int i = 0; i < as.length; i++) {
as[i].afficherVariables();
System.out.println("-----");
}
}
}

class A {
protected int a = 5;

public A(int a) {
this.a = a;
}

public void afficherClasse() {
System.out.println("Classe A");
}

public void afficherVariables() {
System.out.println("a = " + a);
}
}

class B extends A {
protected int b = 6;

public B(int b) {
super(2 * b);
a = b;
}

public void afficherClasse() {
super.afficherClasse();
System.out.println("Classe B");
}

public void afficherVariables() {
super.afficherVariables();
System.out.println("b = " + b);
}
}

class C extends B {
protected int b = 7;
protected int c = 8;

public C(int c) {
super(3 * c);
b = c;
}

public void afficherClasse() {
super.afficherClasse();
System.out.println("Classe C");
}

public void afficherVariables() {
super.afficherVariables();
System.out.println("c = " + c);
}
}
الرجوع الى أعلى الصفحة اذهب الى الأسفل
https://technologie.mam9.com
Admin
المدير
المدير
Admin


ذكر عدد المساهمات : 138
نقاط : 454
السٌّمعَة : 0
تاريخ التسجيل : 30/01/2011

Exercice JAVA masquage (shadowing) et héritage Empty
مُساهمةموضوع: Corrigé   Exercice JAVA masquage (shadowing) et héritage Emptyالسبت أغسطس 06, 2011 6:06 pm

L'affichage correct et donné ci-dessous.
Classe A
-----
Classe A
Classe B
-----
Classe A
Classe B
Classe C
-----
a = 1
-----
a = 2
b = 6
-----
a = 9
b = 6
c = 8
La difficulté principale réside dans l'affichage du dernier objet (de type C).

Pourquoi l'attribut b vaut-il 6 et non pas 3 ?

Réponse : à cause du masquage. En effet, si on observe la hiérarchie de classes donnée, on peut remarquer qu'un objet de type C possède en fait deux attributs b : l'un hérité de B et l'autre redéfini dans C (qui masque le premier).

Lorsque la méthode afficherVariables va être invoquée sur l'objet de type C, elle va appeler la méthode afficherVariables de la super-classe (B). Cette dernière va afficher l'attribut b de la super-classe, qui vaut 6.

Pour bien comprendre la nuance, remplacez la méthode afficherVariables de la classe C par la suivante :


public void afficherVariables() {
super.afficherVariables();
System.out.println("b (masquant) = " + b);
System.out.println("c = " + c);
}


Ré-exécutez le programme. L'affichage devrait alors être le suivant:
Classe A
-----
Classe A
Classe B
-----
Classe A
Classe B
Classe C
-----
a = 1
-----
a = 2
b = 6
-----
a = 9
b = 6
b (masquant) = 3
c = 8
-----
الرجوع الى أعلى الصفحة اذهب الى الأسفل
https://technologie.mam9.com
 
Exercice JAVA masquage (shadowing) et héritage
الرجوع الى أعلى الصفحة 
صفحة 1 من اصل 1

صلاحيات هذا المنتدى:لاتستطيع الرد على المواضيع في هذا المنتدى
TECHNOLOGIE :: منتدى الإعلامية :: Programmation en Java-
انتقل الى: