简体   繁体   English

在C#中带有参数的构造函数中的正确赋值是什么?

[英]What is correct assignment in a constructor with arguments in C#?

What must I choose author or Author after this. 此后我必须选择作者还是作者this. ? Other questions what means the icons beside those options one has a "spanner" and the other has two icons something like a long block more other Little that I don't identify what is that Little and what mean those icons? 还有其他问题,这些选项旁边的图标是什么意思,一个带有“扳手”,另一个带有两个像长块的其他图标。Little,我不知道那个Little是什么,这些图标是什么意思? And if I want to use a second constructor in the same class that is a constructor without arguments is ok to make the constructor like the code after the image? 如果我想在同一类中使用第二个构造函数,那就是没有参数的构造函数可以使该构造函数像图像后的代码一样吗?

在此处输入图片说明

Other question in the next code Is ok the following constructor without arguments? 下一个代码中的其他问题下面的无参数构造函数可以吗? or can be made that way or is something absurd to do that assignment? 还是可以那样做,或者做这项工作是荒谬的? if I don't want specific default value would be better to set this.author = null; 如果我不希望使用特定的默认值,最好设置this.author = null; or this.author = ""; this.author = ""; or what must be done? 还是必须做什么?

 public Book()
 {
     this.author = Author;
     this.title = Title;
 }

You should understand what is field and what is property. 您应该了解什么是字段,什么是属性。

Fields - keep your data. 字段-保留您的数据。

Properties - it's accessors to your data, they like methods but with some sintacsic shugar. 属性-它是数据的访问者,它们喜欢方法,但带有一些简单的技巧。

When you have such simple class like Book it can look like this: 当您拥有像Book这样的简单类时,它可能如下所示:

class Book
{
    public string Title { get; set; }
    public string Author { get; set; }
}

And you can create new book like this: 您可以像这样创建新书:

var book = new Book {Author = "Name", Title = "Some Book"};

C# has autoproperty, so this code: C#具有自动属性,因此此代码:

public string Title { get; set; }

Is the same as this: 与此相同:

private string title;
public string Title
{
    get { return title; }
    set { title = value; }
}

About your question. 关于你的问题。 In case you like keep your code as is. 如果您希望保持原样。 Constructor should be: 构造函数应为:

public Book(string title, string author)
{
    this.title = title;
    this.author = author;
}

Basically, when you declare some private field or method it's for your class only. 基本上,当您声明某些私有字段或方法时,仅适用于您的类。

When you declare public property or method (public fields is bad, do not do this unless absolutly necessary), it's for consumers of your class. 当您声明公共属性或方法时(公共字段是错误的,除非绝对必要,否则不要这样做),它适用于您的类的使用者。

A constructor without any parameters is called a default constructor . 没有任何参数的构造函数称为默认构造函数

You don't have to write a default constructor if you don't want to. 如果您不想这样做,则不必编写默认的构造函数。 For example, if you want Book book = new Book (); 例如,如果您要Book book = new Book (); to be a syntax error then you shouldn't write a default constructor. 成为语法错误,那么您不应该编写默认的构造函数。

If you do create a default constructor then it should initialize your class instance in some way that makes sense in the context of your program. 如果确实创建了默认构造函数,则它应该以某种在程序上下文中有意义的方式初始化类实例。 Think about what book should look like after executing Book book = new Book (); 考虑执行Book book = new Book ();之后应该是什么样的book Book book = new Book (); .

You could pick some book and author as the default... 您可以选择一些书和作者作为默认书...

  public Book()
  {
     title = "The Hitchhikers Guide to the Galaxy";
     author = "Douglas Adams";
  }

... or you could use some value that makes clear that the instance hasn't really been initialized... ...或者您可以使用一些值来表明实例尚未真正初始化...

  public Book()
  {
     title = "<<< title not set >>>";
     author = "<<< author not set >>>";
  }

... or you could use some other suitable value ... ...或者您可以使用其他合适的值...

  public Book()
  {
     title = String.Empty;
     author = String.Empty;
  }

... or even do nothing... ...甚至什么都不做...

  public Book()
  {
  }

In your specific code, the difference of using this.Author or this.author is not much. 在您的特定代码中,使用this.Authorthis.author的区别不大。 Generally, I prefer this.Author because later if you add some code in the setter of Book.Author , you may also need to run the additional code, that is why you create a setter. 一般情况下,我更喜欢this.Author因为以后如果在二传手添加一些代码Book.Author ,你可能还需要运行额外的代码,这就是为什么你创建一个二传手。

But your second constructor is wrong: 但是您的第二个构造函数是错误的:

this.author = Author;

This is equivalent to: 这等效于:

this.author = this.Author

It will assign null to author . 它将为author分配null You should use the arguments from constructor like your first constructor. 您应该像第一个构造函数一样使用构造函数的参数。

The spanner represents a Property. 扳手代表一个属性。 The blocks represent a field. 块代表一个字段。 Setting a field is more efficient than setting a property. 设置字段比设置属性更有效。 Setting the property requires two operations instead of one. 设置属性需要两项操作,而不是一项。 Set the field when you can. 尽可能设置字段。 Setting a property is useful when something has to occur when the property changes. 当属性更改时必须发生某些事情时,设置属性很有用。 You can add your OnChange code to the setter of the property. 您可以将OnChange代码添加到属性的设置器中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM