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
‣Copie d’objets
‣Constructeur de copie (surcharge de constructeurs)
‣Composition d’objets
Sumary
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
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
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
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
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
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
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
Constructeur de copie (surcharge de constructeurs)
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
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
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
Composition d’objets
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
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
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
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
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
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
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 =