public class MaClasse {
.... ....
}
fichiers .java
javac
compilation
0100 1010101010
fichiers .class (bytecode)
jvm
Pokemon |
---|
- name : String ; - level : int ; - type : String ; |
+ Pokemon (name: String, level: int, type: String) + faireEvoluer() |
public class Pokemon {
private String name;
private int level;
private String type;
public Pokemon(String name, int level, String type) {
this.name = name;
this.level = level;
this.type = type;
}
...
}
Pokemon pikachu = new Pokemon("Pikachu", 20, "Électrique");
Nouvelles instances !!!!
Pokemon pikachu = new Pokemon("Pikachu", 20, "Électrique");
Objets créés
@
pikachu
Pokemon pikachu2 = pikachu;
@
pikachu2
pikachu2.faireEvoluer();
public class SuperHero{
private String name;
public SuperHero(String name){
this.name = name;
}
}
....
SuperHero batman = new SuperHero("batman");
SuperHero batmanAgain = new SuperHero("batman");
( batman == batmanAgain ) ==> false
public class SuperHero{
private String name;
public SuperHero(String name){
this.name = name;
}
public boolean equals(Object obj){
return (obj instanceof SuperHero)
&& (this.name.equals(((SuperHero) obj).name));
}
}
....
SuperHero batman = new SuperHero("batman");
SuperHero batmanAgain = new SuperHero("batman");
(batman.equals(batmanAgain)) ==> true
int monAge = 28;
int monAgeAgain = 28;
(monAge == monAgeAgain) => true
Pokemon |
---|
- name : String ; - level : int ; - type : String ; |
+ Pokemon (name: String, level: int, type: String) + faireEvoluer() |
Dresseur |
---|
- name : String ; |
+ Dresseur (name: String) |
0..*
public class Dresseur {
private String name;
private Pokemon[] pokemons;
public Dresseur(String name, Pokemon[] pokemons){
this.name = name;
this.pokemons = pokemons;
}
}
Pokemon pikachu = new Pokemon("Pikachu", 20, "Électrique");
Pokemon[] pokemonsSacha = new Pokemon[1];
pokemonsSacha[0] = pikachu;
Pokemon[] pokemonsOndine = new Pokemon[1];
pokemonsOndine[0] = pikachu;
Dresseur sacha = new Dresseur("Sacha", pokemonsSacha);
Dresseur ondine = new Dresseur("Ondine", pokemonsOndine);
class Pokemon{
public Pokemon(Pokemon exemple){
this.name = exemple.name;
this.level = exemple.level;
this.type = exemple.type;
}
}
...
Pokemon pikachu = new Pokemon("Pikachu", 20, "Électrique");
Pokemon[] pokemonsSacha = new Pokemon[1];
pokemonsSacha[0] = new Pokemon(pikachu);
Pokemon[] pokemonsOndine = new Pokemon[1];
pokemonsOndine[0] = new Pokemon(pikachu);
Dresseur sacha = new Dresseur("Sacha", pokemonsSacha);
Dresseur ondine = new Dresseur("Ondine", pokemonsOndine);
public class Humain {
private String name;
public Humain(String pName) {
name = pName;
}
public String commentTappellesTu() {
return "Je m'appelle "+name+ " and I'm only human after all";
}
}
Humain humain = new Humain("Butcher");
System.out.println(humain.commentTappellesTu());
--> Je m'appelle Butcher and I'm only human after all
public class SuperHero extends Humain {
private String pouvoir;
public SuperHero(String pName, String pouvoir) {
super(pName);
this.pouvoir = pouvoir;
}
public String getPouvoir() {
return "Mon pouvoir magique est : "+pouvoir;
}
}
Humain humain = new SuperHero("Peter Parker", "araignée");
System.out.println(humain.commentTappellesTu());
--> Je m'appelle Peter Parker and I'm only human after all
public class Batman extends SuperHero {
public Batman(String pName, String pouvoir) {
super(pName, pouvoir);
}
public String commentTappellesTu() {
return "Je m'appelle Batman";
}
}
Humain humain = new SuperHero("Bruce Wayne", "la richesse");
System.out.println(humain.commentTappellesTu());
--> Je m'appelle Batman
public class Batman extends SuperHero {
public Batman(String pName, String pouvoir) {
super(pName, pouvoir);
}
public String commentTappellesTu() {
return "Je m'appelle Batman";
}
public boolean equals(Object obj){
return obj instanceof Batman;
}
}
Humain humain = new Batman("Bruce Wayne", "la richesse");
Batman batman = new Batman("Bruce Wayne", "bocou d'argent");
System.out.println(humain.equals(batman));
--> true
public String recupererPouvoir(Humain humain){
String result = null;
if(humain instanceof SuperHero){
result = ((SuperHero) humain).getPouvoir();
}
return result;
}
private -> class
-> package
protected -> package + héritage
public -> tout le monde