你怎么重载方法...... [英] how do you overload methods...

查看:67
本文介绍了你怎么重载方法......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试改变树视图的工作方式,我已经很新了。对于C#而言,现在我正在遇到超载问题。我尝试了以下代码

并且它在大喊大叫说没有重载方法需要0个参数。


#region tViewNodeCollection

公共类tViewNodeCollection:TreeNodeCollection

{

private tView _owner;

private tViewNode _parent;

/ //< summary>

///在tViewItem中创建一个集合

///< / summary>

/// < param name =" parent">< / param>

public tViewNodeCollection(tViewNode parent)

{

_parent = parent ;

}

///< summary>

///创建tViewNodeCollection的新实例

///< / summary>

///< param name =" owner">< / param>

public tViewNodeCollection(tView owner)

{

_owner =所有者;

}

///< summary>

///创建tViewNodeCollection的新实例

///< / summary>

///< param name =" owner">< / param>

public tViewNodeCollection(tView owner,tViewNode父母)

{

_owner =所有者;

_parent =父母;

}

}

#endregion

所以我尝试了一系列不同的东西:这个(东西)但是开始了

吓得我,因为我真的不明白超载 ​​- 有人可以给我一些关于这个的消息吗?

I''m trying to change the way a treeview works just a bit, i''m pretty new
to C# and now i''m running into overloading. I tried the following code
and it''s yelling at me saying that no overload method takes 0 arguments.

#region tViewNodeCollection
public class tViewNodeCollection : TreeNodeCollection
{
private tView _owner;
private tViewNode _parent;
/// <summary>
/// Create a collection within a tViewItem
/// </summary>
/// <param name="parent"></param>
public tViewNodeCollection(tViewNode parent)
{
_parent = parent;
}
/// <summary>
/// Create a new instance of tViewNodeCollection
/// </summary>
/// <param name="owner"></param>
public tViewNodeCollection(tView owner)
{
_owner = owner;
}
/// <summary>
/// Create a new instance of tViewNodeCollection
/// </summary>
/// <param name="owner"></param>
public tViewNodeCollection(tView owner, tViewNode parent)
{
_owner = owner;
_parent = parent;
}
}
#endregion
so then I tried a series of different :this(something) but that started
scaring me since I really don''t understand overloading - Could someone
shed some light on this for me?

推荐答案

一个重载的方法是一种方法,它与同一个

名称的所有方法都不同,只要它具有不同数量或类型的参数。

不同的返回类型不构成过载。如果你得到一个错误

说明没有重载有0个参数,那么你正在调用一个没有参数的方法,并且你没有带有零参数的重载方法。 />
使用带参数的构造函数调用实例化类或

提供带有零参数的显式构造函数。在C#中,如果你提供了一个

重载的构造函数,你就不再得到一个默认的构造函数(零

参数),需要提供一个。我认为这是你的问题。


Thomas P. Skinner [MVP]


" Benny Raymond" <是*** @ pocketrocks.com>在消息中写道

新闻:%2 **************** @ tk2msftngp13.phx.gbl ...
An overloaded method is a method that differs from all methods of the same
name in so far as it has a different number or types of its arguments. A
different return type does not constitute an overload. If you get an error
stating that no overload has 0 arguments then you are calling a method with
no arguments and you have no overloaded method that takes zero arguments.
Either instantiate the class with a constructor call with arguments or
provide an explicit constructor with zero arguments. In C# if you provide an
overloaded constructor you no longer get a default constructor (zero
arguments) and need to provide one. I think this is your problem.

Thomas P. Skinner [MVP]

"Benny Raymond" <be***@pocketrocks.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
我'我试图改变树视图的工作方式,我对C#很新,现在我遇到了重载问题。我尝试了下面的代码
并且它在大喊我说没有重载方法需要0个参数。

#region tViewNodeCollection
公共类tViewNodeCollection:TreeNodeCollection
{私人tView _owner;
私人tViewNode _parent;
///< summary>
///在tViewItem中创建一个集合
///< / summary>
///< param name =" parent">< / param>
public tViewNodeCollection(tViewNode parent)
{
_parent = parent;
}
///< summary>
///创建一个新的tViewNodeCollection实例
///< / summary>
///< param name =" owner">< / param>
公共tViewNodeCollection(tView所有者)
{
_owner =所有者;
}
///<摘要>
///创建tViewNodeCollection的新实例
///< / summary>
///< param name =" owner">< / param>
公共tViewNodeCollection(tView owne r,tViewNode parent)
{
_owner = owner;
_parent = parent;
}
}
#endregion

然后我尝试了一系列不同的东西:这个(东西)但是开始吓唬我,因为我真的不明白超载 ​​- 有人可以为我说明这个吗?
I''m trying to change the way a treeview works just a bit, i''m pretty new
to C# and now i''m running into overloading. I tried the following code
and it''s yelling at me saying that no overload method takes 0 arguments.

#region tViewNodeCollection
public class tViewNodeCollection : TreeNodeCollection
{
private tView _owner;
private tViewNode _parent;
/// <summary>
/// Create a collection within a tViewItem
/// </summary>
/// <param name="parent"></param>
public tViewNodeCollection(tViewNode parent)
{
_parent = parent;
}
/// <summary>
/// Create a new instance of tViewNodeCollection
/// </summary>
/// <param name="owner"></param>
public tViewNodeCollection(tView owner)
{
_owner = owner;
}
/// <summary>
/// Create a new instance of tViewNodeCollection
/// </summary>
/// <param name="owner"></param>
public tViewNodeCollection(tView owner, tViewNode parent)
{
_owner = owner;
_parent = parent;
}
}
#endregion
so then I tried a series of different :this(something) but that started
scaring me since I really don''t understand overloading - Could someone
shed some light on this for me?



我相信TreeViewCollection的构造函数已被设置为

private,因此它不能被继承。

错误信息虽然有点误导(它表明有一个

公共构造函数没有零参数 - 因此在这种情况下你是

可以从你派生的类中调用它。)


见例如......
http://www.groupsrv.com/dotnet/viewtopic.php?t=16350

希望这有帮助


Br,


马克。


PS

Benny,虽然这对你/你公司来说真的是开放的

首选,当命名时类型你应该(恕我直言)不给它们添加前缀

" t" - 这真的是一个Delphi的东西(但如果它适合你那么请

忽略我的建议)。


PPS

你提到你不理解重载,本质上是创建

重载意味着你要向类添加一个额外的函数调用

,它具有相同的名称区别于它的类型签名。

例如


public void SayHello()

{SayHello();} //只是调用重载方法来保存类中的代码


public void SayHello(字符串名称)

{string display =" Hello" ;;

如果name.Length> 0

显示+ =名称;

Console.WriteLine(显示);

}


这意味着Class会提供两个方法调用..

1允许你在没有任何参数的情况下使用SayHello而另一个允许你使用字符串参数向SayHello使用



***我认为你对重载和

覆盖也有点困惑 - 它们是两个不同的概念。理想情况下,您应该阅读

,因为它们对于良好的OOP至关重要***


Thomas P. Skinner [MVP]" < to*@bu.edu>在消息中写道

news:uK ************** @ TK2MSFTNGP15.phx.gbl ...
I believe that the constructor of the TreeViewCollection has been set to
private so effectively it can''t be inherited.
The error message is a bit misleading though (it suggests that there is a
public constructor that does not have zero args - and hence in that case you
would be able to call it from your derived class) .

see for instance...
http://www.groupsrv.com/dotnet/viewtopic.php?t=16350

Hope this helps

Br,

Mark.

P.S.
Benny, although this is really something that is open to your/ your company
preference, when naming types you should (IMHO) not prefix them with
"t" -that is really a Delphi thing (but if it works for you then please
ignore my advice).

P.P.S.
You mention that you do not understand overloading, well in essence creating
an overload means that you are adding an additional function call to a class
which has the same name that is distinguished by it''s type signature.
For instance

public void SayHello()
{SayHello();} //just calling overload method to save code in class

public void SayHello(string name)
{ string display = "Hello";
if name.Length > 0
display += name;
Console.WriteLine(display);
}

Which will mean that the Class would provide two method calls..
1 that allows you to SayHello without any arguments and the other to allow
you to SayHello with a string argument.

***I think you are also getting a little bit confused with overloading and
overriding - they are two DIFFERENT concepts. You should ideally read up on
both since they are crucial to good OOP***

"Thomas P. Skinner [MVP]" <to*@bu.edu> wrote in message
news:uK**************@TK2MSFTNGP15.phx.gbl...
重载方法是一个只要它的参数的数量或类型不同,该方法与同一个名称的所有方法都不同。不同的返回类型不构成过载。如果你得到一个错误
声明没有重载有0个参数,那么你正在调用一个没有参数的方法,并且你没有重载方法需要零参数。使用带有
参数的构造函数调用实例化类,或者提供带有零参数的显式构造函数。在C#中如果你提供了一个重载的构造函数,你就不再得到一个默认的构造函数(零参数),需要提供一个。我认为这是你的问题。

Thomas P. Skinner [MVP]

Benny Raymond <是*** @ pocketrocks.com>在消息中写道
新闻:%2 **************** @ tk2msftngp13.phx.gbl ...
An overloaded method is a method that differs from all methods of the same
name in so far as it has a different number or types of its arguments. A
different return type does not constitute an overload. If you get an error
stating that no overload has 0 arguments then you are calling a method
with no arguments and you have no overloaded method that takes zero
arguments. Either instantiate the class with a constructor call with
arguments or provide an explicit constructor with zero arguments. In C# if
you provide an overloaded constructor you no longer get a default
constructor (zero arguments) and need to provide one. I think this is your
problem.

Thomas P. Skinner [MVP]

"Benny Raymond" <be***@pocketrocks.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
我正在尝试改变树视图的工作方式,我对C#很新,现在我遇到了超载问题。我尝试了下面的代码
并且它在大喊我说没有重载方法需要0个参数。

#region tViewNodeCollection
公共类tViewNodeCollection:TreeNodeCollection
{私人tView _owner;
私人tViewNode _parent;
///< summary>
///在tViewItem中创建一个集合
///< / summary>
///< param name =" parent">< / param>
public tViewNodeCollection(tViewNode parent)
{
_parent = parent;
}
///< summary>
///创建一个新的tViewNodeCollection实例
///< / summary>
///< param name =" owner">< / param>
公共tViewNodeCollection(tView所有者)
{
_owner =所有者;
}
///<摘要>
///创建tViewNodeCollection的新实例
///< / summary>
///< param name =" owner">< / param>
公共tViewNodeCollection(tView owne r,tViewNode parent)
{
_owner = owner;
_parent = parent;
}
}
#endregion

然后我尝试了一系列不同的东西:这个(东西)但是开始吓唬我,因为我真的不明白超载 ​​- 有人可以为我说明这个吗?
I''m trying to change the way a treeview works just a bit, i''m pretty new
to C# and now i''m running into overloading. I tried the following code
and it''s yelling at me saying that no overload method takes 0 arguments.

#region tViewNodeCollection
public class tViewNodeCollection : TreeNodeCollection
{
private tView _owner;
private tViewNode _parent;
/// <summary>
/// Create a collection within a tViewItem
/// </summary>
/// <param name="parent"></param>
public tViewNodeCollection(tViewNode parent)
{
_parent = parent;
}
/// <summary>
/// Create a new instance of tViewNodeCollection
/// </summary>
/// <param name="owner"></param>
public tViewNodeCollection(tView owner)
{
_owner = owner;
}
/// <summary>
/// Create a new instance of tViewNodeCollection
/// </summary>
/// <param name="owner"></param>
public tViewNodeCollection(tView owner, tViewNode parent)
{
_owner = owner;
_parent = parent;
}
}
#endregion
so then I tried a series of different :this(something) but that started
scaring me since I really don''t understand overloading - Could someone
shed some light on this for me?




一个零参数的私有构造函数不会阻止从

类继承。基本上,零args的私有构造函数通常用于
强制编程以使用重载的
构造函数来实例化类。派生类需要在基类中显式调用

重载构造函数。

TreeNodeCollection似乎有点奇怪,因为没有构造函数是

记录在案。这意味着必须通过公共

静态方法构建类。但是,我找不到一个记录在案的人。似乎由TreeView类通过一些微妙的
方法实例化了
TreeNodeCollection。在这种情况下,我同意不能从TreeNodeCollection继承。

BTW,TreeNodeCollection没有记录为密封。结果是相同的

我猜。有趣。


Thomas P. Skinner [MVP]


" Mark Broadbent" <无**** @ nospam.com>在留言中写道

新闻:eK ************** @ TK2MSFTNGP14.phx.gbl ...
A private constructor with zero args doesn''t prevent inheritance from that
class. Basically the private constructor with zero args is normally used to
force the programming to instantiate the class with an overloaded
constructor. The derived class would need to explicitly invoke the
overloaded constructor in the base class.

The TreeNodeCollection does seem a bit strange in that no constructors are
documented. That would mean the class must be constructed via a public
static method. However I can''t find one documented. It appears that the
TreeNodeCollection gets instantiated by the TreeView class by some subtle
method. In this case I agree that one can''t inherit from TreeNodeCollection.
BTW, TreeNodeCollection is NOT documented as sealed. The result is the same
I guess. Interesting.

Thomas P. Skinner [MVP]

"Mark Broadbent" <no****@nospam.com> wrote in message
news:eK**************@TK2MSFTNGP14.phx.gbl...
我相信构造函数TreeViewCollection的设置已经设置为私有,因此它不能被继承。
错误信息有点误导(它表明有一个
公共构造函数没有没有args - 因此在这种情况下
你可以从派生类中调用它。)

看看例如...
http://www.groupsrv.com/dotnet/viewtopic.php?t=16350

希望这会有所帮助

Br,

马克。

PS
Benny,虽然这实际上是对你/你的公司偏好开放的东西,当命名类型你应该(恕我直言)不在它们前面加上t时。 - 这真的是一个Delphi的东西(但如果它适合你那么
请忽略我的建议)。

PPS
你提到你不理解重载,实质上是好的
创建一个重载意味着你要添加一个额外的函数调用
到一个具有相同名称的类,它的类型是
签名。
例如<公共void SayHello()
{SayHello();} //只是调用重载方法来保存类中的代码

public void SayHello(string name)
{string display =" Hello" ;;
如果name.Length> 0
显示+ =名称;
Console.WriteLine(显示);
}

这意味着Class将提供两个方法调用..
1允许你SayHello没有任何参数,另一个允许你使用字符串参数使用SayHello。

***我认为你对重载也有点困惑和覆盖 - 它们是两个不同的概念。理想情况下,你应该阅读
,因为它们对于良好的OOP至关重要。

Thomas P. Skinner [MVP]" < to*@bu.edu>在消息中写道
新闻:uK ************** @ TK2MSFTNGP15.phx.gbl ...
I believe that the constructor of the TreeViewCollection has been set to
private so effectively it can''t be inherited.
The error message is a bit misleading though (it suggests that there is a
public constructor that does not have zero args - and hence in that case
you would be able to call it from your derived class) .

see for instance...
http://www.groupsrv.com/dotnet/viewtopic.php?t=16350

Hope this helps

Br,

Mark.

P.S.
Benny, although this is really something that is open to your/ your
company preference, when naming types you should (IMHO) not prefix them
with "t" -that is really a Delphi thing (but if it works for you then
please ignore my advice).

P.P.S.
You mention that you do not understand overloading, well in essence
creating an overload means that you are adding an additional function call
to a class which has the same name that is distinguished by it''s type
signature.
For instance

public void SayHello()
{SayHello();} //just calling overload method to save code in class

public void SayHello(string name)
{ string display = "Hello";
if name.Length > 0
display += name;
Console.WriteLine(display);
}

Which will mean that the Class would provide two method calls..
1 that allows you to SayHello without any arguments and the other to allow
you to SayHello with a string argument.

***I think you are also getting a little bit confused with overloading and
overriding - they are two DIFFERENT concepts. You should ideally read up
on both since they are crucial to good OOP***

"Thomas P. Skinner [MVP]" <to*@bu.edu> wrote in message
news:uK**************@TK2MSFTNGP15.phx.gbl...
重载方法是一种不同于所有相同名称的方法,只要它具有不同数量或类型的
参数。不同的返回类型不构成过载。如果你得到一个错误,说明没有重载有0个参数,那么你就是调用一个没有参数的方法,你没有重载方法,它接受零参数。要么使用带有参数的构造函数调用实例化类,要么提供带有零
参数的显式构造函数。在C#中如果你提供了一个重载的构造函数,你就不再获得一个默认的构造函数(零参数)并且需要提供一个。我认为这是你的问题。

Thomas P. Skinner [MVP]

Benny Raymond <是*** @ pocketrocks.com>在消息中写道
新闻:%2 **************** @ tk2msftngp13.phx.gbl ...
An overloaded method is a method that differs from all methods of the
same name in so far as it has a different number or types of its
arguments. A different return type does not constitute an overload. If
you get an error stating that no overload has 0 arguments then you are
calling a method with no arguments and you have no overloaded method that
takes zero arguments. Either instantiate the class with a constructor
call with arguments or provide an explicit constructor with zero
arguments. In C# if you provide an overloaded constructor you no longer
get a default constructor (zero arguments) and need to provide one. I
think this is your problem.

Thomas P. Skinner [MVP]

"Benny Raymond" <be***@pocketrocks.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
我正在尝试改变树视图的工作方式,我对C#很新,现在我遇到了超载问题。我尝试了下面的代码
并且它在大喊我说没有重载方法需要0个参数。

#region tViewNodeCollection
公共类tViewNodeCollection:TreeNodeCollection
{私人tView _owner;
私人tViewNode _parent;
///< summary>
///在tViewItem中创建一个集合
///< / summary>
///< param name =" parent">< / param>
public tViewNodeCollection(tViewNode parent)
{
_parent = parent;
}
///< summary>
///创建一个新的tViewNodeCollection实例
///< / summary>
///< param name =" owner">< / param>
公共tViewNodeCollection(tView所有者)
{
_owner =所有者;
}
///<摘要>
///创建tViewNodeCollection的新实例
///< / summary>
///< param name =" owner">< / param>
公共tViewNodeCollection(tView owne r,tViewNode parent)
{
_owner = owner;
_parent = parent;
}
}
#endregion

然后我尝试了一系列不同的东西:这个(东西)但是开始吓唬我,因为我真的不明白超载 ​​- 有人可以为我说明这个吗?
I''m trying to change the way a treeview works just a bit, i''m pretty new
to C# and now i''m running into overloading. I tried the following code
and it''s yelling at me saying that no overload method takes 0 arguments.

#region tViewNodeCollection
public class tViewNodeCollection : TreeNodeCollection
{
private tView _owner;
private tViewNode _parent;
/// <summary>
/// Create a collection within a tViewItem
/// </summary>
/// <param name="parent"></param>
public tViewNodeCollection(tViewNode parent)
{
_parent = parent;
}
/// <summary>
/// Create a new instance of tViewNodeCollection
/// </summary>
/// <param name="owner"></param>
public tViewNodeCollection(tView owner)
{
_owner = owner;
}
/// <summary>
/// Create a new instance of tViewNodeCollection
/// </summary>
/// <param name="owner"></param>
public tViewNodeCollection(tView owner, tViewNode parent)
{
_owner = owner;
_parent = parent;
}
}
#endregion
so then I tried a series of different :this(something) but that started
scaring me since I really don''t understand overloading - Could someone
shed some light on this for me?





这篇关于你怎么重载方法......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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