Source de Prog113.java
import java.util.Vector;
public class Prog113 {
public static void main (String args[]) {
Chien1 milou = new Chien1("milou", 34);
Chien1 milouBis = new Chien1("milou", 34);
String mot = "abcd";
String abcd = "ab" + "cd";
Vector vecteur = new Vector();
vecteur.addElement(mot); vecteur.addElement(milou);
System.out.println("milou.toString() : "+milou
+ "\nmot.toString() : "+mot
+ "\nvecteur.toString() : "+vecteur);
System.out.println("mot.equals(abcd) : "+mot.equals(abcd)
+"\nmot.equals(milou) : "+mot.equals(milou)
+"\nmilou.equals(milouBis) : "+milou.equals(milouBis));
}
}
|
$ java Prog113 un nouvel ami ! milou un nouvel ami ! milou milou.toString() : Chien1@82ba41 mot.toString() : abcd vecteur.toString() : [abcd, Chien1@82ba41] mot.equals(abcd) : true mot.equals(milou) : false milou.equals(milouBis) : false |
Source de Chien2.java
public class Chien2 {
String nom;
int nombrePuce;
String aboiement;
Chien2(String s, int i, String a) {
nom = s;
nombrePuce = i;
aboiement=a;
}
public String toString() {
return nom+"("+nombrePuce+" puces, aboie : "+aboiement+")";
}
}
|
import java.util.Vector;
public class Prog115 {
public static void main (String args[]) {
Chien2 milou = new Chien2("milou", 34, "wouah");
Chien2 milouBis = new Chien2("milou", 34, "wouah");
String mot = "abcd";
String abcd = "ab" + "cd";
Vector vecteur = new Vector();
vecteur.addElement(mot); vecteur.addElement(milou);
System.out.println("milou.toString() : "+milou
+ "\nmot.toString() : "+mot
+ "\nvecteur.toString() : "+vecteur);
System.out.println("mot.getClass().getName() : "
+mot.getClass().getName()
+"\nmilou.getClass().getName() : "+milou.getClass().getName() );
System.out.println("milou.getClass().getSuperclass().getName() : "
+milou.getClass().getSuperclass().getName() ); }
}
|
$ java Prog115 milou.toString() : milou(34 puces, aboie : wouah) mot.toString() : abcd vecteur.toString() : [abcd, milou(34 puces, aboie : wouah)] mot.getClass().getName() : java.lang.String milou.getClass().getName() : Chien2 milou.getClass().getSuperclass().getName() : java.lang.Object |
Source de Prog64.java
public class Prog64 {
public static void main (String args[]) {
Chien2 milou, medor, fantome;
String chaine="abc";
Object ch = chaine;
fantome = null;
milou = new Chien2("milou",34,"wouah");
medor = new Chien2("medor",12,"harf");
System.out.println("milou : "+milou);
System.out.println("medor : "+medor);
System.out.println("fantome : "+fantome);
System.out.println("medor==null : "
+(medor==null));
System.out.println("fantome==null : "
+(fantome==null));
System.out.println("medor instanceof Chien2 : "
+(medor instanceof Chien2));
System.out.println("ch instanceof Chien2 : "
+(ch instanceof Chien2));
System.out.println("medor instanceof Object : "
+(medor instanceof Object));
System.out.println("fantome instanceof Chien2 : "
+(fantome instanceof Chien2));
System.out.println("fantome instanceof Object : "
+(fantome instanceof Object));
}
}
|
milou : milou(34 puces, aboie : wouah) medor : medor(12 puces, aboie : harf) fantome : null medor==null : false fantome==null : true medor instanceof Chien2 : true ch instanceof Chien2 : false medor instanceof Object : true fantome instanceof Chien2 : false fantome instanceof Object : false |
Source de Chien4.java
public class Chien4 {
String nom;
int nombrePuce;
String aboiement;
Chien4(String s, int i, String a) {
nom = s;
nombrePuce = i;
aboiement=a;
}
public String toString() {
return nom+"("+nombrePuce+" puces, aboie : "+aboiement+")";
}
public boolean equals(Object autre) {
if ((autre != null) && (this.getClass() == autre.getClass()))
return this.nom.equals(((Chien4)autre).nom);
else
return false;
}
}
|
Source de Prog41.java
public class Prog41 {
public public static void main (String args[]) {
Chien4 milou, medor, rantanplan, chienIdiot;
milou = new Chien4("milou", 34, "wouah");
medor = milou;
rantanplan =new Chien4("rantanplan", 999, "grrr");
chienIdiot =new Chien4("rantanplan", 999, "grrr");
System.out.println("milou == medor "
+ (milou == medor));
System.out.println("milou == chienIdiot "
+ (milou == chienIdiot));
System.out.println("rantanplan == chienIdiot "
+ (rantanplan == chienIdiot));
System.out.println("milou equals medor "
+ milou.equals(medor));
System.out.println("milou equals chienIdiot "
+ milou.equals(chienIdiot));
System.out.println("rantanplan equals chienIdiot "
+ rantanplan.equals(chienIdiot));
}
}
|
un nouvel ami ! milou un nouvel ami ! rantanplan un nouvel ami ! rantanplan milou == medor true milou == chienIdiot false rantanplan == chienIdiot false milou equals medor true milou equals chienIdiot false rantanplan equals chienIdiot true |
déclaration de 4 variables de type Chien4
==
|
|
|
|
nom de romeo : Durand tout sur juliette : DUPONT juliette : 0781075987765 67 juliette est une femme juliette pas equals homonyme |
|