2017-06-14 1 views
-3

J'ai le code ci-dessous et je continue d'obtenir l'erreur doit avoir un corps car il n'est pas marqué abstrait extern ou partiel.Erreur: doit avoir un corps car il n'est pas marqué abstrait extern ou partiel

public interface Shape{ 
    int GetArea(); 
} 
public class shape:Shape{ 
    int height; 
    int width; 
    int radius; 
    int GetArea(); 

} 
class Rectangle :Shape{ 
    int height; 
    int width; 
    Rectangle(int height,int width){ 
     this.height = height; 
     this.width = width; 
    } 
    int GetArea(){ 
     int area = height*width; 
     return area; 
    } 
} 
+3

Votre méthode 'GetArea' en La classe 'shape' n'a pas de corps de méthode, mais elle n'est pas marquée abstract (ni votre classe). Toutes les méthodes non abstraites doivent avoir un corps de méthode. – nbokmans

+0

pouvez-vous donner le code s'il vous plaît – shamila

Répondre

2

Vous devriez implémenter l'interface Shape comme celui-ci (ne pas oublier le mot-clé public dans la mise en œuvre):

public class shape : Shape 
{ 
    public int GetArea() 
    { 
     return 0;//Return an int value 
    } 

} 
class Rectangle : Shape 
{ 
    public int GetArea() 
    { 
     int area = height * width; 
     return area; 
    } 
} 
3

Essayez ceci:

//1. Change interface name from Shape to IShape; to 
// be in line with standards and to avoid being 
// too close to your original class name, `shape` 

public interface IShape { 
    int GetArea(); 
} 

//2. Change your class from `shape` to `Shape`; again 
// to be in line with standards. Also given we 
// changed to IShape above, amend the interface name 
// here also. 

//3. Add keyword `abstract`. Since we don't have a body 
// (i.e. a definition) for the GetArea method, we can't 
// instantiate it, so it has to be abstract 
public abstract class Shape:IShape { 
    //4. Make our fields protected; since we can't instantiate 
    // this class there's no point having them here unless 
    // they're made visible to any classes which inherit from 
    // this class. 
    protected int height; 
    protected int width; 
    protected int radius; 
    //5. Since GetArea is required (to implement interface IShape) 
    // but has no body defined, make it abstract. 
    public abstract int GetArea(); 
} 

//6. We changed the class from `shape` to `Shape`, so change here also. 
//7. You probably also want to make this class & it's constructor public 
// though I can't guarentee that (hence commented out) 
/* public */ class Rectangle : Shape { 
    /* public */ Rectangle(int height,int width){ 
     this.height = height; 
     this.width = width; 
    } 
    //8. To implement the interface this method needs to be made public. 
    //9. You also need to use the `override` keyword to say that this 
    // replaces the abstract method on the base class (`Shape`) 
    public override int GetArea() { 
     int area = height * width; 
     return area; 
    } 
}