this innerhalb Konstructur

netrobot

Erfahrenes Mitglied
Code:
interface iShape2D{
	final double pi = 3.1415926;
	abstract double showArea();
}

class CCircle implements iShape2D{
	private double _radius = 0.0;
	CCircle(){
		this(_radius);
	}
	CCircle(double r){
		_radius = r;
	}
	public double showArea(){
		return _radius * _radius * pi;
	}
}

public class app{
	public static void main(String [] args){
		CCircle circle = new CCircle();
		System.out.println("circle area = " + circle.showArea());
	}
}
hier habe ich einen Fehler bei
Code:
CCircle(){
		this(_radius);
}
: cannot reference _radius before supertype constructor has been called.
1. es geht um interface nicht ableiten.
2. benutze ich this nicht super
 
Hallo!

Membervariablen sind als Aufrufparameter von (eigenen) Konstruktoren nicht erlaubt. Diese werden ja auch erst initialisiert nachdem der Konstruktor aufgerufen wurde...

Btw.
private double _radius = 0.0;

ist unnoetig da Membervariablen automatisch mit ihren Defaultwerten initialisiert werden.
Besser:
private double radius;

Machs deshalb besser so:
Code:
   class Circle implements IShape2D{
   	private double radius;
   	
   	Circle(){
   	}
   
   	Circle(double radius){
   		this.radius = radius;
   	}
  
   	public double calculateArea(){
   		return this.radius * this.radius * Math.PI;
   	}
   }
Btw. Klassennamen werden gross geschrieben...

Gruss Tom
 
Zurück