C#构造函数和方法

时间:2018-09-15 11:05:08

标签: c#

我正在详细研究C#,因此遇到了我无法理解的文本。有人可以向我解释吗?

最后一段不可理解

enter image description here

4 个答案:

答案 0 :(得分:1)

您可以执行此操作以进一步了解他在上一段中的含义

  public class Wine
        {
            public decimal Price;
            public int Year;
            public static int wineCategory = 8;
            public int capturedCategory; 
            public int capturedCategory2; 
            public int vote;    
            public Wine(decimal price)
            {
                Price = price;
            }

            public Wine(decimal price, int year) : this(price)
            {
                Year = year;
            }

            public Wine(decimal price, DateTime year) : this(price, year.Year)
            {
            }

            public Wine(decimal price, DateTime year, int wineCategory) : this(price, year.Year)
            {
                capturedCategory = wineCategory; 
            }
            //in  this overload I  can use the static property 
            public Wine(decimal price, DateTime year, int wineCategory,int vote) : this(price, new DateTime(year.Year,1,1),Wine.wineCategory)
            {
                vote= vote;
            }
           // but I can't do this 
            public Wine(decimal price, DateTime year, int wineCategory,int vote) : this(price, new DateTime(year.Year,1,1),this.capturedCategory)
            {
                vote =vote;
            }


        }

要恢复 您可以执行以下重载,因为静态字段是在实例之前构造的,因此可以被正确接受并正确编译

 public Wine(decimal price, DateTime year, int wineCategory,int vote) : this(price, new DateTime(year.Year,1,1),Wine.wineCategory)
                {
                    vote= vote;
                }

但是你不能这样做

public Wine(decimal price, DateTime year, int wineCategory,int vote) : this(price, new DateTime(year.Year,1,1),this.capturedCategory)
                {
                    vote= vote;
                }

因为您尚未在内存中构建实例

更像是先出现的鸡蛋和鸡肉:)

答案 1 :(得分:1)

假设您的类中有一个名为Bob的(非静态)方法。

最后一段表示您的表达式(this (price, year.Year)位)不能调用Bob

如果Bob方法是static,则表达式可以编译。

让我们看一个例子:

public class Runner
{
    public static string Bob1()
    {
        return "Hi";
    }
    public static string Bob2()
    {
        return "Hi";
    }

    public Runner(string hello)
    {
        // Some logic here
    }

    public Runner() : this(Bob2()) { }

可以编译,但是:

public class Runner
{
    public static string Bob1()
    {
        return "Hi";
    }
    public static string Bob2()
    {
        return "Hi";
    }

    public Runner(string hello)
    {
        // Some logic here
    }

    public Runner() : this(this.Bob1()) { }

不会。 这是因为第一个代码块使用static Bob2,而第二个代码块使用(非静态)Bob1

答案 2 :(得分:1)

欢迎叠放!因此,如果要使用 different 选项从类创建对象(例如wine对象),则构造函数重载非常有用。 文字说您可以在: this(param a, param b) {函数中使用表达式。 例如Wine(decimal price, DateTime time) : this(price, time.year)

表达式在这里time.year,是另一个对象的函数调用。 您可能已经了解到,可以使用this.function()从您自己的对象中调用函数。但是,如果尚未实例化函数,则无法调用它。

示例:

public class Wine
{
    private decimal price;
    private int year;
    public Wine (decimal p){price =p;}
    public Wine (decimal p, int y) : this(p){ year=y;}
    public Wine (decimal p, DateTime time) : this(p, this.getYear(time)){} //this is not possible
    public int getYear(DateTime time){return time;}
}

由于只有在每个构造函数完成后才创建对象,因此无法从您自己的类中调用this.函数(此处为this.getYear(time)函数)。但是,可以使用静态功能。

答案 3 :(得分:1)

最后一段基本上说你不能这样做:

class Foo {
    public int I { get; }
    // some more properties...

    public Foo(int i) { 
        this.I = i; 
        // a few other properties are set here
    }
    public Foo() : this(GetBar()) {} // this line gives you an error

    public int Get5() {
        // Here is looks at some other properties of Foo and returns something based on that
    }
}

该行给您一个错误,因为它实际上是this(this.Get5())。如书中所述,您不能使用this关键字将参数传递给另一个构造函数,因为到那时,该对象尚未正确创建。试想一下Get5需要正确初始化对象才能返回预期结果。

但是,静态方法是可以的,因为它们属于类型本身。