2015-09-19 53 views
0

我不明白我爲什麼或者怎樣能夠實例化一個變量並在類的開始處什麼也沒有指定,但是一旦我進入了類的編碼,能夠分配它來使用它。 例如,在團結,我可以:爲什麼我在使用之前實例化

public class MapGenerator : MonoBehaviour { 

// right here I can instantiate the variable... 
    public int width; 
    public int height; 

// and then here I actually use it!! 
    void GenerateMap() { 
     map = new int[width,height]; 
     RandomFillMap(); 

有人可以請清除此爲我?

+4

爲什麼這個標籤'Java'它將給U上的一個錯誤? – Michael

+1

你沒有實例化一個變量,你聲明它。 – rhughes

+0

對不起,使用了錯誤的術語。仍在學習我所能做的一切。 – Letholor

回答

1

JSL §4.12.5規定:

每個類變量,實例變量,或陣列部件被初始化爲默認值 當它是:

之前其值是用於 程序中的每個變量的值必須(§15.9,§15.10):

對於字節類型,默認值爲零,即值爲 (字節)0。

對於short類型,默認值爲0,即 (short)0的值。

對於int類型,默認值是零,即,0

對於類型長,默認值是零,即,0L。

對於浮點類型,默認值爲正零,即0.0f。

對於double類型,默認值爲正零,即0.0d。

對於char類型,默認值爲空字符,即 '\ u0000'。

對於布爾類型,默認值爲false。

對於所有引用類型(§4.3),默認值爲空。

你給的代碼隱做到這一點在Java中:

public int width; 
public int height; 

public MapGenerator() { // Constructor 
    width = 0; 
    height = 0; 
} 

void GenerateMap() { 
    map = new int[width,height]; 
} 

我認爲C#有類似的功能。

+1

這個答案是針對Java的,儘管問題是關於C# – rhughes

+1

非常感謝你。這幫了很多忙。我想知道關於C#和Java的代碼。我不確定是否能夠標記這兩種語言,因爲它們在初學者級別上非常相似,而這正是我所處的位置。 – Letholor

+0

@Letholor很高興我能幫忙:) –

2

Value Types (C# Reference)

每個值類型具有初始化 該類型的默認值的隱式默認構造函數。

注:

  • 注重的是初始只對類成員運行。
  • 局部變量在使用前必須初始化。

的類型的默認值:

bool: false 
byte: 0 
char: '\0' 
decimal: 0.0M 
double: 0.0D 
enum: The value produced by the expression (E)0, where E is the enum identifier. 
float: 0.0F 
int: 0 
long: 0L 
sbyte: 0 
short: 0 
struct: The value produced by setting all value-type fields to their default values and all reference-type fields to null. 
uint: 0 
ulong: 0 
ushort: 0 

類會員 - OK

public class Sample1 
{ 
    int i; 
    public void PrintI() 
    { 
     //Prints 0 
     Console.WriteLine(i.ToString()); 
    } 
} 

局部變量 - 錯誤

public class Sample2 
{ 
    public void PrintI() 
    { 
     int i; 
     //Compile Error: Use of unassigned local variable 'i' 
     Console.WriteLine(i.ToString()); 
    } 
} 
0

這是值類型和引用類型之間的區別。

值類型(通過值傳遞)就是那個值。雖然引用類型是指引用值的地方。

所有類型都有一個默認值,對於引用類型它是空的,你可以認爲它是「Nothing」,對於它不能的值類型,值不能爲空,所以有一個默認值值。

這裏它的工作原理是因爲int是一個值類型,這意味着即使是單元化的,它也有一個默認值(對於所有值爲0的數字類型),如果它是一個引用類型,嘗試嘗試時會得到一個NullReferenceException未初始化使用它。

因此,例如:

public class Player 
{ 
     public string Name; 
} 

    int i; // This is the same as int i = 0 as default(int) == 0 
    i.ToString(); // does not crash as i has a default value; 

    Player p; // This is a reference type, the value fo default(Player) is null 
    p.ToString(); // This will crash with a NullReferenceException as p was never initialized and you're effectivelly calling a method on "null". 
0

的傢伙說..there是和每個變量類型默認值...如果ü沒有任何值賦給變量,將給予默認值,而不是...但如果ü想給這些變量在創建對象或實例,你可以使用構造這樣

public class MapGenerator 
{ 
    public int width; 
    public int height; 

    public MapGenerator(int Con_width, int Con_height) 
    { 
     width = Con_width; 
     height = Con_height; 
    } 
} 

現在的類的任何對象ü創建必須有一個的時候,又有價值寬度和高度值

MapGenerator mymap = new MapGenerator(200,200); 

現在如果u試圖平時對象

MapGenerator mymap = new MapGenerator(); 

,因爲你改變了這種默認constractor到新的自定義constractor

相關問題