2014-01-16 23 views
2

這是我的代碼,我不知道什麼是錯的,爲什麼編譯器說struct Cat(字符串,字符串,布爾,布爾)'必須聲明一個正文,因爲它沒有標記爲抽象,外部或部分

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace _11._2 
{ 
class Cat 
{ 
    private string name = "cat"; 
    private string type = "gizei"; 
    private bool water = false; 
    private bool yalel = false; 
    public Cat(string name, string type, bool water, bool yalel); 
    public string meow() 
    { 
     if (yalel) 
      return "Meow Meow Meow"; 
     else 
      return "Meow"; 
    } 

    public bool thirsty() 
    { 
     return water ? true : false; 
    } 

    public string info() 
    { 
     return "My name is " + name + " and my type is " + type; 
    } 
} 
} 

我嘗試添加抽象的或外部的,但依然出現..

+1

嘗試'公貓(字符串名稱,字符串類型,布爾水,布爾yalel){/ *參數這裏的類成員賦值* /}',構造函數沒有正文。您可能需要將參數分配給類成員字段。 –

回答

4

這是由於以下行:

public Cat(string name, string type, bool water, bool yalel); 

這是一個構造函數聲明,所以你必須聲明主體,這種方法:

public Cat(string name, string type, bool water, bool yalel) 
{ 
    // Do something like : 
    // this.name = name; 
    // this.type = type; 
    // etc... 
} 
0

試試這個

public Cat(string name, string type, bool water, bool yalel) 
    { 

    } 
相關問題