c#构造函数问题 [英] c# constructor issues

查看:103
本文介绍了c#构造函数问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private string _brand = "";

private string _model = "";

Public Class (string brand)

{

     _brand = brand;
}

Public Class (string model)

{

     _model = model
}





有一个错误说明调用不明确,我需要两个构造函数但只有一个参数是不同的。



There is an error stating the call is ambiguous, I need two constructors but only one parameter that is different.

推荐答案

构造函数的目的是创建一个具有已定义状态的类的实例。

构建只有部分成员初始化的实例打破了上述声明。



C ++或C#等语言(和其他语言)提供了重载的概念函数名称,这意味着相同的函数名称可以表示不同的函数实现。区分功能(具有相同名称!)的唯一方法是在至少一个参数位置上使用不同数量的参数和/或不同的参数类型。此参数类型列表称为函数的签名。如果两个函数的签名不同,则可能会出现重载,否则,重载是不明确的。



构造函数是特殊函数(它们没有返回类型,它们的名称是与它们所属的类的名称相同,加上它们具有初始化成员的特殊方法(在C ++中)并选择所需的基类构造函数(如果类派生自基类)。



正如在其他解决方案中指出的那样,你违反了重载概念(两个函数(在这种情况下是构造函数)具有相同的签名。



你的班级也应该有一个更好的名字,你的代码也会被不小心张贴 - 因为大写被打破了,所以根本不会编译,因为你没有发布整个班级代码。



让我们很容易通过邮寄方式在第一位可编辑的代码中发布答案。您的粗心职位的奖项是它被严重投票 - 现在的风险很高整个问题及相关讨论很快就会完全删除。



请使用[改善问题]链接更新您的问题,并将固定的C#代码放入< pre lang =cs> ...< / pre> 阻止(正如一些好心灵为你所做的那样)。



您的类应该类似于:
The purpose of constructors is to create an instance of a class with a defined state.
Constructing an instance that has only part of the members initialized is breaking the above mentioned claim.

Languages like C++ or C# (and others) provide the concept of overloading of function names, which means that the same function name can denote different function implementations. The only way to distinguish the function (with the same name!) is with differeing number of parameters and/or differing parameter types on at least one parameter position. This list of parameter types is called the signature of a function. If the signature of two functions is different, overloading may be possible, otherwise, overloading is ambiguous.

Constructors are special functions (they have no return type and their name is the same as the name of the class they belong to, plus they have special means to initialize members (in C++) and select the needed base class constructors (if the class derives from a base class).

As pointed out in the other solutions, you violate the overloading concept (two functions (constructors in this case) have the same signature.

You class should also have a better name and your code is carelessly posted - this won't compile at all since capitalization is broken and since you did not post the whole class code.

Please make it easy to us to post an answer by post in the first place compilable code. The prize of your careless post is that it is severly voted down - the risk now is high that the whole question and associated discussions get completely deleted soon.

Please update your question by using the [Improve Question] link and place the fixed C# code into <pre lang="cs">...</pre> block (as was already done for you by some nice soul).

Your class should look something like:
// Car class that encapsulates brand and model
class Car
{
   private string _brand;
   private string _model;
   public Car(string brand, string model) { _brand = brand; _model = model; }
   ...
}



干杯

Andi


Cheers
Andi


两个构造函数的签名是相同的,因为它们都只接受一个string类型的参数。

为了能够区分两个构造函数,你需要在每个构造函数定义中使用两个不同的类型。



干杯!
The signature of both constructors is the same, as both of them take only one argument of type string.
To be able to differentiate between the two constructors you'd need to use two different types in each constructor definition.

Cheers!


如果查看那些构造函数的签名,它们都会占用一个字符串。编译器或调用者如何能够区分品牌和模型?



为什么在中没有其他参数?单个构造函数,通知编译器和调用者你正在尝试更新哪个字符串?



或者,从内容中确定它是哪个字符串 - 再次单个构造函数



或者,不要在构造函数中传递字符串,但有2个方法,如 UpdateBrand UpdateModel



您可能会发现本文很有用 C#中构造函数介绍 [ ^ ]
If you look at the "signature" of those constructors they both take a single string. How is the compiler, or the caller for that matter, able to tell the difference between a brand and a model?

Why not have another parameter in a single constructor that informs both the compiler and the caller which string you are trying to update?

Or, determine which string it is from the content - again a single constructor

Or, don't pass the string in the constructor but have 2 methods such as UpdateBrand and UpdateModel

You might find this article useful An Intro to Constructors in C#[^]


这篇关于c#构造函数问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆