<jupyter_start><jupyter_text>TD2  Classe de base / classe mère : `Personne`<jupyter_code>public class Personne {
    private int age;
    private String nom;

    public Personne(int a, String n) {
        this.age = a;
        this.nom = n;
    }
    
    public Personne(Personne p) {
        this.age = p.age;
        this.nom = p.nom;
    }

    public int age() { return this.age; }
    public String nom() { return this.nom; }    

    public void miseAJour(int a, String n) {
        System.out.println("Personne.miseAJour(int, String)");
        this.nom = n;
        this.age = a;
    }
    
    public void miseAJour() {
        System.out.println("Personne.miseAJour()");
        this.nom = "?";
        this.age = 0;
    }

    public String toString() {
        return ("[Personne] Nom : " + this.nom + " | Âge : " + this.age);
    }
}<jupyter_output><empty_output><jupyter_text>Classe dérivée / classe fille : `Employe`<jupyter_code>public class Employe extends Personne {
    private int salaire;

    public Employe(int a, String n, int s) {
        super(a,n); 
        this.salaire = s;
    }
    
    public Employe(Personne p, int s) {
        super(p);
        this.salaire = s;
    }
    
    public int salaire() { return this.salaire; }
}<jupyter_output><empty_output><jupyter_text>Héritage des attributs et méthodes<jupyter_code>Personne p1 = new Personne(29, "toto");
Employe e2 = new Employe(p1, 4000);
Employe e3 = new Employe(20, "titi", 1000);
System.out.println(e2.nom());
System.out.println(e3.age());
System.out.println(p1);
System.out.println(e2);
System.out.println(e3);
e2.miseAJour(35, "tutu");
e3.miseAJour();
System.out.println(e2);
System.out.println(e3);<jupyter_output><empty_output><jupyter_text>Enrichissement et redéfinition (override) Ajouter les méthodes suivantes dans la classe `Employe` :<jupyter_code>public void miseAJour(int a, String n, int s) {
        System.out.println("Employe.miseAJour(int, String, int)");
        this.miseAJour(a, n);
        this.salaire = s;
    }
    
    public void miseAJour() {
        System.out.println("Employe.miseAJour()");
        super.miseAJour();
        this.salaire = 0;
    }
    
    public String toString() {
        return ("[Employe] Nom : " + this.nom() + " | Âge : " + this.age() + " | Salaire : " + this.salaire );
    }
Personne p1 = new Personne(29, "toto");
Employe e2 = new Employe(p1, 4000);
System.out.println(p1);
System.out.println(e2);

e2.miseAJour(20, e2.nom() + " MAJ", 6000);
System.out.println(e2);

e2.miseAJour();
System.out.println(e2);<jupyter_output><empty_output><jupyter_text>Polymorphisme<jupyter_code>Personne p1 = new Personne(29, "toto");
Employe e1 = new Employe(29, "titi", 7000);
Personne p2 = new Employe(29, "titi", 7000);
Employe e2 = (Employe)p2;

System.out.println("p1");
p1.miseAJour();

System.out.println("e1");
e1.miseAJour();

System.out.println("p2");
p2.miseAJour();

e2.salaire();
Personne p1 = new Personne(29, "toto");
Personne p2 = new Personne(27, "tata");
Employe e1 = new Employe(29, "titi", 7000);
Personne p3 = new Employe(34, "tutu", 4000);

Personne[] tabPers = new Personne[4];
tabPers[0] = p1;
tabPers[1] = p2;
tabPers[2] = e1;
tabPers[3] = p3;

for (int i=0; i < tabPers.length; i++) {
    System.out.println(tabPers[i].toString());
}<jupyter_output><empty_output><jupyter_text>Vérification (check) et conversion (cast) de type<jupyter_code>Personne p1 = new Personne(29, "toto");
Personne p2 = new Personne(27, "tata");
Employe e1 = new Employe(29, "titi", 7000);

Personne[] tabPers= new Personne[3];
tabPers[0] = p1;
tabPers[1] = p2;
tabPers[2] = e1;

for (int i=0; i < tabPers.length; i++) {
    Personne p = tabPers[i];
    System.out.println(p);
    
    System.out.println(p.salaire());
    
    if (p instanceof Employe) {
        Employe e = (Employe)p;
        System.out.println(e.salaire());
    }
}<jupyter_output><empty_output><jupyter_text>Membres de classe (`static`)<jupyter_code>public class Point {
    private int x;
    private int y;
    private static int compteurPoints = 0;
    
    public static int getCompteurPoints() { return Point.compteurPoints; }
    
    public Point(int cx, int cy) {
        this.x = cx;
        this.y = cy;
        compteurPoints++;
    }
}
Point p1 = new Point(10, 10);
Point p2 = new Point(20, 20);
Point p3 = p2;
System.out.println("compteur : " + Point.getCompteurPoints());<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15532444042505086, 'fraction_numerical': 0.032104906172281254, 'mean_word_length': 3.3672260612043434, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '219094', 'license': 'MIT', 'n_tokens_mistral': 1813, 'n_tokens_neox': 1713, 'n_words': 363, 'path': 'notebooks/TD2.ipynb', 'repo_name': 'Skymirrh/Java2STPI', 'size': '4423'}