Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 错误:必须有一个主体,因为它没有标记为抽象外部或局部_C#_Interface_Abstract Class_Abstract - Fatal编程技术网

C# 错误:必须有一个主体,因为它没有标记为抽象外部或局部

C# 错误:必须有一个主体,因为它没有标记为抽象外部或局部,c#,interface,abstract-class,abstract,C#,Interface,Abstract Class,Abstract,我有下面的代码,我不断得到错误必须有一个机构,因为它没有被标记为抽象外部或部分 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

我有下面的代码,我不断得到错误必须有一个机构,因为它没有被标记为抽象外部或部分

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;
    }
}

您应该像这样实现
Shape
界面(不要忘记实现中的
public
关键字):

试试这个:

//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;
    }
}

shape
类中的
GetArea
方法没有方法体,但它没有标记为抽象(您的类也没有)。所有非抽象方法都必须有一个方法体。请给出代码好吗
//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;
    }
}