Java:Duplicate local variable area
domxaline
-
domxaline -
domxaline -
Bonjour,
j'ai écris ce prg suivant
pendant la compilation j'ai erreur suivantes
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Duplicate local variable area
at InterfaceTest.main(InterfaceTest.java:29)
la ligne 29 est
Area area;
j'ai corrigé area par area1
après avoir corrigé,j'ai une autre erreur apparaît
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
No enclosing instance of type InterfaceTest is accessible. Must qualify the allocation with an enclosing instance of type InterfaceTest (e.g. x.new A() where x is an instance of InterfaceTest).
No enclosing instance of type InterfaceTest is accessible. Must qualify the allocation with an enclosing instance of type InterfaceTest (e.g. x.new A() where x is an instance of InterfaceTest).
at InterfaceTest.main(InterfaceTest.java:24)
ligne 24 est
Rectangle rect=new Rectangle();
public class InterfaceTest
{
interface Area
{
final static float pi=3.14F;
float compute(float x, float y);
}
class Rectangle implements Area
{
public float compute (float x, float y)
{
return(x*y);
}
}
class Circle implements Area
{
public float compute(float x, float y)
{
return (pi*x*x);
}
}
public static void main(String[]args)
{
Rectangle rect=new Rectangle();
Circle cir=new Circle();
Area area;
area=rect;
System.out.println("Area of Rectangle="+area.compute(10, 20));//area refers to cir object
Area area1;
area=cir;
System.out.println("Area of Circle="+area.compute(10,0 ));
}
}
j'ai écris ce prg suivant
pendant la compilation j'ai erreur suivantes
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Duplicate local variable area
at InterfaceTest.main(InterfaceTest.java:29)
la ligne 29 est
Area area;
j'ai corrigé area par area1
après avoir corrigé,j'ai une autre erreur apparaît
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
No enclosing instance of type InterfaceTest is accessible. Must qualify the allocation with an enclosing instance of type InterfaceTest (e.g. x.new A() where x is an instance of InterfaceTest).
No enclosing instance of type InterfaceTest is accessible. Must qualify the allocation with an enclosing instance of type InterfaceTest (e.g. x.new A() where x is an instance of InterfaceTest).
at InterfaceTest.main(InterfaceTest.java:24)
ligne 24 est
Rectangle rect=new Rectangle();
1 réponse
-
ok,j'ai compris mon erreur,il faut ecrire ainsi
interface Area//interface defined { final static float pi=3.14F; float compute(float x, float y); } class Rectangle implements Area //interface implemented { public float compute (float x, float y) { return(x*y); } } class Circle implements Area //another implementation { public float compute(float x, float y) { return (pi*x*x); } } public class InterfaceTest { public static void main(String[]args) { Rectangle rect=new Rectangle(); Circle cir=new Circle(); Area area;// interface object area=rect;//area refers to rect object System.out.println("Area of Rectangle="+area.compute(10, 20));//area refers to cir object System.out.println("Area of Circle="+area.compute(10,0 )); } }
merci d'avance