• Aucun résultat trouvé

UE 2I002 (ex LI230) : éléments de programmation par objets avec Java TD4 - Composition, copie d’objets

N/A
N/A
Protected

Academic year: 2022

Partager "UE 2I002 (ex LI230) : éléments de programmation par objets avec Java TD4 - Composition, copie d’objets"

Copied!
21
0
0

Texte intégral

(1)

UE 2I002 (ex LI230) : éléments de programmation par objets avec Java

TD4 - Composition, copie d’objets

!

Juliana Silva Bernardes

[email protected]

http://www.lcqb.upmc.fr/julianab/teaching/JAVA/

(2)

2

‣Copie d’objets

‣Constructeur de copie (surcharge de constructeurs)

‣Composition d’objets

Sumary

(3)

3

Copie d’objets

public class Point {

// variables d’instance private int x;

private int y;

//Constructeurs

public Point() {this(0, 0);}

public Point(int x, int y) { this.x = x; this.y = y;

}

public void setXY(int x, int y){

this.x = x; this.y = y;

}

public String ToString(){

return “[“ + x + “,” + y + “]”;

} }

public class TestPoint {


public static void main(String [] args) {

Point p1 = new Point();

}

x=0 y=0

p1

(4)

4

Copie d’objets

public class TestPoint {


public static void main(String [] args) {

Point p1 = new Point();

Point p2 = p1;

}

x=0 y=0

p1 p2

public class Point {

// variables d’instance private int x;

private int y;

//Constructeurs

public Point() {this(0, 0);}

public Point(int x, int y) { this.x = x; this.y = y;

}

public void setXY(int x, int y){

this.x = x; this.y = y;

}

public String ToString(){

return “[“ + x + “,” + y + “]”;

} }

(5)

5

Copie d’objets

public class TestPoint {


public static void main(String [] args) {

Point p1 = new Point();

Point p2 = p1;

p2.setXY(3,5);

}

x=3 y=5

p1 p2

public class Point {

// variables d’instance private int x;

private int y;

//Constructeurs

public Point() {this(0, 0);}

public Point(int x, int y) { this.x = x; this.y = y;

}

public void setXY(int x, int y){

this.x = x; this.y = y;

}

public String ToString(){

return “[“ + x + “,” + y + “]”;

} }

(6)

6

Copie d’objets

public class TestPoint {


public static void main(String [] args) {

Point p1 = new Point();

Point p2 = p1;

p2.setXY(3,5);

System.out.println(p1);

}

x=3 y=5

p1 p2

public class Point {

// variables d’instance private int x;

private int y;

//Constructeurs

public Point() {this(0, 0);}

public Point(int x, int y) { this.x = x; this.y = y;

}

public void setXY(int x, int y){

this.x = x; this.y = y;

}

public String ToString(){

return “[“ + x + “,” + y + “]”;

} }

[3,5]

java TestPoint

(7)

7

Copie d’objets

public class Point {

// variables d’instance private int x;

private int y;

//Constructeurs

public Point() {this(0, 0);}

public Point(int x, int y) { this.x = x; this.y = y;

}

public void setXY(int x, int y){

this.x = x; this.y = y;

}

public String ToString(){

return “[“ + x + “,” + y + “]”;

}

public Point clone(){

return new Point (x, y);

} }

(8)

8

Copie d’objets

public class TestPoint {


public static void main(String [] args) { Point p1 = new Point();

Point p2 = p1.clone();

!

}

x=0 y=0

p1

public class Point {

// variables d’instance private int x;

private int y;

//Constructeurs

public Point() {this(0, 0);}

public Point(int x, int y) { this.x = x; this.y = y;

}

public void setXY(int x, int y){

this.x = x; this.y = y;

}

public String ToString(){

return “[“ + x + “,” + y + “]”;

}

public Point clone(){

return new Point (x, y);

} }

p2

x=0

y=0

(9)

9

Copie d’objets

public class TestPoint {


public static void main(String [] args) {

Point p1 = new Point();

Point p2 = p1.clone();

p2.setXY(7,8);

System.out.println("p1 ==>" + p1);

System.out.println("p2 ==>" + p2);

}

x=0 y=0

p1 p2

public class Point {

// variables d’instance private int x;

private int y;

//Constructeurs

public Point() {this(0, 0);}

public Point(int x, int y) { this.x = x; this.y = y;

}

public void setXY(int x, int y){

this.x = x; this.y = y;

}

public String ToString(){

return “[“ + x + “,” + y + “]”;

}

public Point clone(){

return new Point (x, y);

} }

x=7 y=8

p1 => [0,0]

java TestPoint

p2 => [7,8]

(10)

10

Constructeur de copie (surcharge de constructeurs)

(11)

11

Constructeur de copie (surcharge de constructeurs)

public class Point {

// variables d’instance private int x;

private int y;

//Constructeurs

public Point() {this(0, 0);}

public Point(int x, int y) { this.x = x; this.y = y;

}

public Point(Point p) {

this.x = p.x; this.y = p.y;

}

public void setXY(int x, int y){

this.x = x; this.y = y;

}

public String ToString(){

return “[“ + x + “,” + y + “]”;

} }

(12)

12

public class TestPoint {


public static void main(String [] args) { Point p1 = new Point();

Point p2 = new Point(p1);

} }

public class Point {

// variables d’instance private int x;

private int y;

//Constructeurs

public Point() {this(0, 0);}

public Point(int x, int y) { this.x = x; this.y = y;

}

public Point(Point p) {

this.x = p.x; this.y = p.y;

}

public void setXY(int x, int y){

this.x = x; this.y = y;

}

public String ToString(){

return “[“ + x + “,” + y + “]”;

} }

x=0 y=0

p1 p2

x=0 y=0

Constructeur de copie (surcharge de constructeurs)

(13)

13

public class TestPoint {


public static void main(String [] args) {

Point p1 = new Point();

Point p2 = new Point(p1);

p2.setXY(7,8);

System.out.println("p1 ==>" + p1);

System.out.println("p2 ==>" + p2);

}

x=0 y=0

p1 p2

public class Point {

// variables d’instance private int x;

private int y;

//Constructeurs

public Point() {this(0, 0);}

public Point(int x, int y) { this.x = x; this.y = y;

}

public Point(Point p) {

this.x = p.x; this.y = p.y;

}

public void setXY(int x, int y){

this.x = x; this.y = y;

}

public String ToString(){

return “[“ + x + “,” + y + “]”;

} }

x=7 y=8

p1 => [0,0]

java TestPoint p2 => [7,8]

Constructeur de copie (surcharge de constructeurs)

(14)

14

Composition d’objets

(15)

15

Composition d’objets

public class Point3D { private Point p;

private int z;

public Point3D() {

this(new Point(0,0), 0);

}

public Point3D(Point p, int z) {

this.z = z; this.p = p;

}

public void setP(Point p){

this.p =p;

}

public String toString(){

return "[" + p.getX() + "," +

p.getY() + "," + z + "]";

} }

public class TestPoint3D {


public static void main(String [] args) {

Point p1 = new Point (2,4);

}

x=2 y=4

p1

(16)

16

Composition d’objets

public class Point3D { private Point p;

private int z;

public Point3D() {

this(new Point(0,0), 0);

}

public Point3D(Point p, int z) {

this.z = z; this.p = p;

}

public void setP(Point p){

this.p =p;

}

public String toString(){

return "[" + p.getX() + "," +

p.getY() + "," + z + "]";

} }

public class TestPoint3D {


public static void main(String [] args) {

Point p1 = new Point (2,4);

Point3D p3d = new Point3D(p1, 6);

}

x=2 y=4

p1

z=6

p3d

p =

(17)

17

Composition d’objets

public class Point3D { private Point p;

private int z;

public Point3D() {

this(new Point(0,0), 0);

}

public Point3D(Point p, int z) {

this.z = z; this.p = p;

}

public void setP(Point p){

this.p =p;

}

public String toString(){

return "[" + p.getX() + "," +

p.getY() + "," + z + "]";

} }

public class TestPoint3D {


public static void main(String [] args) {

Point p1 = new Point (2,4);

Point3D p3d = new Point3D(p1, 6);

p1.setXY(7, 8);

}

z=6

p3d

x=7 y=8

p1

p =

(18)

18

Composition d’objets

public class Point3D { private Point p;

private int z;

public Point3D() {

this(new Point(0,0), 0);

}

public Point3D(Point p, int z){

this.z = z; this.p = p;

}

public void setP(Point p){

this.p =p;

}

public String toString(){

return "[" + p.getX() + "," +

p.getY() + "," + z + "]";

} }

public class TestPoint3D {


public static void main(String [] args) {

Point p1 = new Point (2,4);

Point3D p3d = new Point3D(p1, 6);

p1.setXY(7, 8);

System.out.println(p3d);

}

z=6

p3d

x=7 y=8

p1

p =

[7,8,6]

java TestPoint3D

(19)

19

Composition d’objets

public class Point3D { private Point p;

private int z;

public Point3D() {

this(new Point(0,0), 0);

}

public Point3D(Point p, int z){

this.z = z; this.p = p;

}

public void setP(Point p){

this.p =p;

}

public String toString(){

return "[" + p.getX() + "," +

p.getY() + "," + z + "]";

} }

public class TestPoint3D {


public static void main(String [] args) { Point p1 = new Point (2,4);

!

}

x=2 y=4

p1

(20)

20

Composition d’objets

public class Point3D { private Point p;

private int z;

public Point3D() {

this(new Point(0,0), 0);

}

public Point3D(Point p, int z){

this.z = z; this.p = p;

}

public void setP(Point p){

this.p =p;

}

public String toString(){

return "[" + p.getX() + "," +

p.getY() + "," + z + "]";

} }

public class TestPoint3D {


public static void main(String [] args) {

Point p1 = new Point (2,4);

Point3D p3d = new Point3D(p1.clone(), 6);

!

}

z=6

p3d

x=2 y=4

p1

p =

x=2

y=4

(21)

21

Composition d’objets

public class Point3D { private Point p;

private int z;

public Point3D() {

this(new Point(0,0), 0);

}

public Point3D(Point p, int z){

this.z = z; this.p = p;

}

public void setP(Point p){

this.p =p;

}

public String toString(){

return "[" + p.getX() + "," +

p.getY() + "," + z + "]";

} }

public class TestPoint3D {


public static void main(String [] args) {

Point p1 = new Point (2,4);

Point3D p3d = new Point3D(p1.clone(), 6);

p1.setXY(7, 8);

}

z=6

p3d

x=7 y=8

p1

p =

x=2

y=4

Références

Documents relatifs

– et d’e↵ectuer les instructions n´ecessaires pour la cr´eation d’un objet de cette classe Les constructeurs sont appel´es quand on cr´ee (mot-cl´e new) un objet de cette

On ne peut pas utiliser l’opérateur « point » (.) sur les variables de la classe Chien ailleurs que dans la classe Chien

‣ Un tableau est une structure de données contenant un groupe d'éléments tous du même type. ‣ Le type des éléments peut être un type primitif ou

‣ boolean, byte, short, char, int, long, float et double, sont associées des classes Boolean, Byte, Short, Character, Integer, Long, Float et Double. ‣ Ces classes ont des

6OF EFT QSPQSJÏUÏT JOEVJUFT QBS MF QPMZNPSQIJTNF FTU RVF MJOUFSQSÏUFVS +BWB FTU DBQBCMF EF USPVWFS MF USBJUFNFOU Ë FČFDUVFS MPST EF MBQQFM EVOF NÏUIPEF TVS VO PCKFU "JOTJ

-F DPODFQU EF DMBTTF BCTUSBJUF TF TJUVF FOUSF DFMVJ EF DMBTTF FU DFMVJ EJOUFSGBDF $FTU VOF DMBTTF RVPO OF QFVU QBT EJSFDUFNFOU JOTUBODJFS DBS DFSUBJOFT EF TFT NÏUIPEFT OF TPOU

‣ On peut affecter à un champ ou une variable d'un type une expression de type moins élevé dans la hiérarchie des types. int a

Q 60.2 Ecrire les classes ElemTrain (abstraite), Wagon (abstraite), Motrice, WVoyageur et WMarchandise avec au moins un constructeur avec param` etres et une red´ efinition de la