2013-08-05 237 views
1

我正要開始學習C#並遇到zetcode C# tutorial(任何有關漂亮的教程網站或pdf的建議都會被讚賞)。由於我之前正在使用Python進行一些編程,因此我發現C#並不那麼困難。但是,有一點讓我感到困惑的是使用這個網站的東西。創建類的實例

using System; 

public class Being {} 

public class CSharpApp 
{ 
    static void Main() 
    { 
     Being b = new Being();// I don't understand this 
     Console.WriteLine(b); 
    } 
} 

爲什麼不乾脆:

b=new Being(); 

爲什麼是使用類的名稱的網站,同時在兩個地方?它只是C#的方式,還是它是寫它的一種方式?

+13

請繼續閱讀教程,這是_extremely_基本的東西。 –

+0

您也可以使用'var b = new Being();'; –

+0

您必須在實例化之前聲明對象的類型。例如'是b; b = new存在();'會起作用,但簡單地使用'Being b = new Being();'更容易。 – Tim

回答

12

好吧,你有2個部分它。

,第一部分是b

Being b; 

聲明它實際上告訴你打算使用Being類型的變量名稱爲b


第二編譯器部分是作業b

b = new Being(); 

其中可變b與物體,在這種情況下,是Being


C#允許你兩個部分合併成1條線的一個新實例分配,從而產生以下:

Being b = new Being(); 
+2

+1。也許值得注意的是完成的緣故,有時候你想要一個不同的參考。如果'存在'實現了一個接口,我們可能會明確地要求''ICommonInterface b = new Being()'。 –

0

這就是C#工作的方式。你需要提供班級的名字。如果你不喜歡這種語法 - 你也可以這樣做:

var being = new Being(); 
1

第一行定義一個類

public class Being {} 

和第二代碼被創建日的一個實例在課堂上。

Being b = new Being() 
+0

這裏是描述什麼是類和對象的鏈接。 http://www.adobe.com/devnet/actionscript/learning/oop-concepts/objects-and-classes.html http://en.wikipedia.org/wiki/Class_(computer_programming) –

4

第一 「存在」 定義型可變b。這表示「b是對Being類型的對象的引用」。您可以將其更改爲var b = Being(),編譯器將根據等號右側的表達式推斷B的類型。

第二個「存在」是表達式的一部分,該表達式提供變量b的初始。在這種情況下,這是對默認構造函數Being()的調用。你可以在很多方面分配的b值:

Being b = null; // don't give it any value yet 
Being b = new Being(); // make a new Being object using the default constructor 
Being b = new Being("abcde"); // use a different custom constructor 
Being b = GiveMeABeing(); // call some other method that will return a Being object 
3

在變量聲明中的第Being告訴編譯器如何可以識別和處理的對象。new Being()告訴編譯器如何構建(實例化)對象。當你利用接口和子類時,這種事情很有用。

abstract class IMusicalInstrument { 
    public Play(); 
} 

class Trumpet : IMusicalInstrument { 
    public Play() { 
    // etc. 
    } 
} 

class Piano : IMusicalInstrument { 
    public Play() { 
    // etc. 
    } 
} 

在這樣做時,你可以利用返回未知IMusicalInstrument方法:

IMusicalInstrument instrument = GetARandomInstrument(); 

..放心,你可以Play()他們,儘管不知道究竟他們是什麼。

0

您必須聲明類型並實例化該類型,因此在您的示例中,您將指定要聲明一個Being,然後實際創建(讀取:new up)一個Being對象。

至於你爲什麼你需要指定兩次它的問題,你的聲明類型可能是一個基類,但你實例化一個派生類,像這樣:

public class Animal {} 

public class Human : Animal {} 

現在在你的代碼,你可以聲明一個Animal,但實際上實例化一個Human,因爲HumanAnimal,是這樣的:在C#

Animal myHuman = new Human(); 
0

的對象後,現實生活中的物體建模。例如,一個球具有半徑,彈性,重量,顏色等。您還可以對球進行操作,例如投擲,滾動,放下,旋轉等​​等。在C#中,您可以創建類似於此的類定義球:

public class Ball 
{ 
    // Radius in inches 
    public double Radius { get; set; } 
    public double Bounciness { get; set; } 
    // Weight in lbs 
    public double Weight { get; set; } 
    public string Color { get; set; } 
    // more properties 

    // constructor - this is called when your class is instantiated (created) 
    public Ball() 
    { 

    } 

    // throw the ball 
    public void Throw() 
    { 
     // method for throwing ball 
    } 

    // roll the ball 
    public void Roll() 
    { 
     // method for rolling ball 
    } 

    // drop the ball 
    public void Drop() 
    { 
     // method for dropping ball 
    } 

    // spin the ball 
    public void Spin() 
    { 
     // method for spinning the ball 
    } 

    // more methods for interacting with a ball 
} 

然後,你將宣佈球的情況下,設置屬性和調用方法是這樣的:

Ball ball = new Ball(); 
ball.Color = "Red"; 
ball.Weight = 1.2; // 1.2 lbs 
ball.Radius = 12; // 12 inches 
ball.Bounciness = 0.2; // for use in a physics engine perhaps 
ball.Throw(); // throw the ball 
ball.Drop(); // drop the ball 
// etc 
1

爲什麼不乾脆:

b = new Being(); ? 

我假設你有知識約=運營商將rhs分配給lhs。在上面的聲明中,你正在給b分配一些東西嗎?

編譯器如何知道什麼是b?編譯器不知道什麼是b!所以,你必須說bBeing類型的局部變量是什麼,下面的代碼做

Being b; 

現在什麼都不是b你想儲存什麼b中才能使用它的權利?因此,創建一個type Being的實例並存儲它。這就是下面的代碼做

b = new Being(); 

我們都合併並告訴編譯器bBeing類型和其持有的Being新實例。

Being b = new Being(); 

希望這有助於