2013-08-05 179 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(); 

希望这有助于