From: Jérôme Benoit Date: Thu, 15 Feb 2018 13:40:49 +0000 (+0100) Subject: TP2: Factor out common code in the abstract class. X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=009c5b1e63be46a50ba2c551c96369f461f44a93;p=TP_POO.git TP2: Factor out common code in the abstract class. Signed-off-by: Jérôme Benoit --- diff --git a/TP2/Cercle.java b/TP2/Cercle.java index f13be69..62772da 100644 --- a/TP2/Cercle.java +++ b/TP2/Cercle.java @@ -1,22 +1,17 @@ class Cercle extends Forme { - private Point pOri; private double rayon; Cercle(Point pO, double r) { - pOri = pO; + super(pO); rayon = r; } public void dessiner(Piletransformations pile) { String className = this.getClass().getSimpleName(); Point pTrans = pile.getCurrentTransformation(); - Point pOriTrans = pOri.additionner(pTrans); - System.out.println(className + " " + pOri.toString() + "->" + pOriTrans.toString() + " " + rayon); - } - - public void deplacer(Point p) { - pOri = pOri.additionner(p); + Point pOriTrans = super.getpOri().additionner(pTrans); + System.out.println(className + " " + super.getpOri().toString() + "->" + pOriTrans.toString() + " " + rayon); } } diff --git a/TP2/Forme.java b/TP2/Forme.java index df9fc44..0e08ddf 100644 --- a/TP2/Forme.java +++ b/TP2/Forme.java @@ -1,7 +1,22 @@ -abstract class Forme { +public abstract class Forme { + private Point pOri; - abstract public void dessiner(Piletransformations pile); - abstract public void deplacer(Point p); + public abstract void dessiner(Piletransformations pile); + Forme() { + pOri = new Point(); + } + + Forme(Point p) { + pOri = p; + } + + public Point getpOri() { + return pOri; + } + + public void deplacer(Point p) { + pOri = pOri.additionner(p); + } } diff --git a/TP2/Image.java b/TP2/Image.java index 8f10393..6ec38bf 100644 --- a/TP2/Image.java +++ b/TP2/Image.java @@ -1,18 +1,17 @@ class Image extends Forme { final int IMAGE_MAX_SIZE = 100; - Point pOri; Forme[] formeCollection; int formeNumber; Image() { - pOri = new Point(); + super(); formeCollection = new Forme[IMAGE_MAX_SIZE]; formeNumber = 0; } Image(Point p) { - pOri = p; + super(p); formeCollection = new Forme[IMAGE_MAX_SIZE]; formeNumber = 0; } diff --git a/TP2/Segment.java b/TP2/Segment.java index a421af7..5fd0d73 100644 --- a/TP2/Segment.java +++ b/TP2/Segment.java @@ -1,6 +1,5 @@ class Segment extends Forme { - private Point pOri; private Point pDest; /* Segment() { @@ -9,21 +8,21 @@ class Segment extends Forme { } */ Segment(Point pO, Point pD) { - pOri = pO; + super(pO); pDest = pD; } public void dessiner(Piletransformations pile) { String className = this.getClass().getSimpleName(); Point pTrans = pile.getCurrentTransformation(); - Point pOriTrans = pOri.additionner(pTrans); + Point pOriTrans = super.getpOri().additionner(pTrans); Point pDestTrans = pDest.additionner(pTrans); - System.out.println(className + " " + pOri.toString()+ "->" + pOriTrans.toString() + " " + + System.out.println(className + " " + super.getpOri().toString()+ "->" + pOriTrans.toString() + " " + pDest.toString() + "->" + pDestTrans.toString()); } public void deplacer(Point p) { - pOri = pOri.additionner(p); + super.deplacer(p); pDest = pDest.additionner(p); }