G
Guest
Gast
Gegenben seien folgende Klassen :
<code>public abstract class Shape2
{
private int x;
private int y;
Shape2(int newx, int newy) {
moveTo(newx, newy);
}
public abstract double flaeche();
}
public abstract class Shape3 extends Shape2 implements Comparable<Shape2>{
public Shape3(int newx, int newy) {
super(newx, newy);
// TODO Auto-generated constructor stub
}
}
class Rectangle extends Shape3 {
private int width;
private int height;
// constructor
Rectangle(int newx, int newy, int newwidth, int newheight) {
super(newx, newy);
setWidth(newwidth);
setHeight(newheight);
}
// accessors for the width & height
int getWidth() { return width; }
int getHeight() { return height; }
void setWidth(int newwidth) { width = newwidth; }
void setHeight(int newheight) { height = newheight; }
public double flaeche(){
return (width * height);
}
public int compareTo(Shape2 a){ //<<<<<<<<<<------ hier
if ( this.flaeche() < a.flaeche() ) return -1;
if ( this.flaeche() > a.flaeche() ) return 1;
return 0;
}
}
class Circle extends Shape3 {
private int radius;
// constructor
Circle(int newx, int newy, int newradius) {
super(newx, newy);
setRadius(newradius);
}
// accessors for the radius
int getRadius() { return radius; }
void setRadius(int newradius) { radius = newradius; }
// draw the circle
void draw() {
System.out.println(
"Drawing a Circle at" + getX() + ", " + getY() +
"), radius " + getRadius());
}
public double flaeche(){
return (2*Math.PI*radius);
}
public int compareTo(Shape2 a){ //<<<<<<<------ hier
if ( this.flaeche() < a.flaeche() ) return -1;
if ( this.flaeche() > a.flaeche() ) return 1;
return 0;
}
}
</code>
sowie eine Test-Klasse die die main methode enthält , einen kreis und ein rechteck anlegt und dann die compareto ausführt.
das ganze funktioniert soweit auch, jedoch stellt sich für mich die frage warum ich ( siehe "<<<--hier " markierungen)
dort der methode public int compareTo(.....) ein Shape2 a als Parameter übergeben kann, obwohl das ja eigentlich eine abstrakte oberklasse von den jeweiligen objekten Circle und Rectangle ist....
oder geht das, weil Shape2 a es dann offen lässt ob das "a" ein Circle oder Rectangle ist ?
<code>public abstract class Shape2
{
private int x;
private int y;
Shape2(int newx, int newy) {
moveTo(newx, newy);
}
public abstract double flaeche();
}
public abstract class Shape3 extends Shape2 implements Comparable<Shape2>{
public Shape3(int newx, int newy) {
super(newx, newy);
// TODO Auto-generated constructor stub
}
}
class Rectangle extends Shape3 {
private int width;
private int height;
// constructor
Rectangle(int newx, int newy, int newwidth, int newheight) {
super(newx, newy);
setWidth(newwidth);
setHeight(newheight);
}
// accessors for the width & height
int getWidth() { return width; }
int getHeight() { return height; }
void setWidth(int newwidth) { width = newwidth; }
void setHeight(int newheight) { height = newheight; }
public double flaeche(){
return (width * height);
}
public int compareTo(Shape2 a){ //<<<<<<<<<<------ hier
if ( this.flaeche() < a.flaeche() ) return -1;
if ( this.flaeche() > a.flaeche() ) return 1;
return 0;
}
}
class Circle extends Shape3 {
private int radius;
// constructor
Circle(int newx, int newy, int newradius) {
super(newx, newy);
setRadius(newradius);
}
// accessors for the radius
int getRadius() { return radius; }
void setRadius(int newradius) { radius = newradius; }
// draw the circle
void draw() {
System.out.println(
"Drawing a Circle at" + getX() + ", " + getY() +
"), radius " + getRadius());
}
public double flaeche(){
return (2*Math.PI*radius);
}
public int compareTo(Shape2 a){ //<<<<<<<------ hier
if ( this.flaeche() < a.flaeche() ) return -1;
if ( this.flaeche() > a.flaeche() ) return 1;
return 0;
}
}
</code>
sowie eine Test-Klasse die die main methode enthält , einen kreis und ein rechteck anlegt und dann die compareto ausführt.
das ganze funktioniert soweit auch, jedoch stellt sich für mich die frage warum ich ( siehe "<<<--hier " markierungen)
dort der methode public int compareTo(.....) ein Shape2 a als Parameter übergeben kann, obwohl das ja eigentlich eine abstrakte oberklasse von den jeweiligen objekten Circle und Rectangle ist....
oder geht das, weil Shape2 a es dann offen lässt ob das "a" ein Circle oder Rectangle ist ?