帮助这个示例 [英] Help with this sample

查看:66
本文介绍了帮助这个示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图学习plymorphism。

我的样本是


公共类Class1

{

public static void Main(string [] args)

{

Cls1 x = new Cls1();

Cls2 y = new Cls2( );

Cls3 y = new Cls3();


Object [] coll = new Object [] {x,y,z};


foreach(对象obj in coll)

{

Console.WriteLine(obj.Rtn());

//这里是问题。

//如何获得Rtn()的返回值;

//我想使用循环和概念多态性


}

}

}

公共类Cls1

{

public string Rtn()

{

return" From Cls1" ;;

}

}

公共类Cls2

{

公共字符串Rtn()

{

返回从Cls2;

}

}


公共类Cls3

{

public strin g Rtn()

{

返回来自Cls3;

}

}

trying to learn plymorphism.
My sample is

public class Class1
{
public static void Main(string[] args)
{
Cls1 x = new Cls1();
Cls2 y = new Cls2();
Cls3 y = new Cls3();

Object[] coll = new Object[] {x,y,z};

foreach (Object obj in coll)
{
Console.WriteLine(obj.Rtn());
// here is the issue.
//How to get the return value of Rtn();
//I want to use the loop and the concept of polymorphism

}
}
}
public class Cls1
{
public string Rtn()
{
return "From Cls1";
}
}
public class Cls2
{
public string Rtn()
{
return "From Cls2";
}
}

public class Cls3
{
public string Rtn()
{
return "From Cls3";
}
}

推荐答案

为了展示多态性,您的类Cls1,Cls2,Cls3需要

都来自基类定义了您的常用方法。


使用一个名为Rtn()的方法声明一个抽象基类。从本课程中获取你的课程,然后在你的foreach循环中,调用

基类上的方法,而不是具体的类


例如


公共摘要MyBaseClass

{

公共抽象字符串Rtn();

}


公共类Cls1:MyBaseClass

{

公共覆盖字符串Rtn()

{

返回从1级开始;

}

}

//为您的其他人重复此操作课程


调用方法时使用


foreach(myBaseClass c in coll)

{

Console.Writeln(c.Rtn());

}


希望这会有所帮助。


ps如果您不想使用抽象类,那么您可以创建一个

具体基类并将该方法标记为受保护的虚拟


11月26,晚上8:23,Praveen < prav ... @ newsgroup.nospamwrote:
In order to exhibit polymorphism, your classes Cls1, Cls2, Cls3 need to
all derive from a base class with your common method defined.

Declare an abstract base class with one method called Rtn(). Derive
your classes from this class and then during your foreach loop, call
the method on the base class rather than the concrete classes

eg

public abstract MyBaseClass
{
public abstract string Rtn();
}

public class Cls1 : MyBaseClass
{
public override string Rtn()
{
return "From class 1";
}
}
//repeat this for your other classes

when calling the method use

foreach (MyBaseClass c in coll)
{
Console.Writeln( c.Rtn() );
}

Hope this helps.

ps if you dont want to use an abstract class then you can just create a
concrete base class and mark the method as protected virtual

On Nov 26, 8:23 pm, "Praveen" <prav...@newsgroup.nospamwrote:

试图学习plymorphism。

我的样本是

公共类Class1

{

public static void Main(string [] args)

{

Cls1 x = new Cls1();

Cls2 y = new Cls2();

Cls3 y = new Cls3();


Object [] coll = new Object [] {x,y,z};


foreach(对象obj in coll)

{

Console.WriteLine(obj.Rtn());

//这是问题。

//如何获取返回值Rtn();

//我想使用循环和多态的概念


}

}

}


公共类Cls1

{

公共字符串Rtn()

{

return" From Cls1" ;;

}

}


公共类Cls2

{

公共字符串Rtn()
{

返回来自Cls2;

}

}


公共类Cls3

{

公共字符串Rtn()

{

返回"来自Cls3" ;;

}

}
trying to learn plymorphism.
My sample is

public class Class1
{
public static void Main(string[] args)
{
Cls1 x = new Cls1();
Cls2 y = new Cls2();
Cls3 y = new Cls3();

Object[] coll = new Object[] {x,y,z};

foreach (Object obj in coll)
{
Console.WriteLine(obj.Rtn());
// here is the issue.
//How to get the return value of Rtn();
//I want to use the loop and the concept of polymorphism

}
}
}

public class Cls1
{
public string Rtn()
{
return "From Cls1";
}
}

public class Cls2
{
public string Rtn()
{
return "From Cls2";
}
}

public class Cls3
{
public string Rtn()
{
return "From Cls3";
}
}





通过继承和接口在.NET中存在多态性。你没有在你的例子中说明了接口的使用,而且你几乎没有看到继承的表面。这是一篇简单的文章,你可能想要阅读:b / b
面向对象编程中的多态性
http://en.wikipedia.org/wiki/Polymor...ed_programming

Cls3 y = new Cls3();
Cls3 y = new Cls3();



您的代码无法编译,因为上面的行试图重新定义y。

Your code won''t compile because the line above attempts to redefine "y".


foreach(对象obj in coll)

{

Console.WriteLine(obj.Rtn());
foreach (Object obj in coll)
{
Console.WriteLine(obj.Rtn());



你不能调用obj.Rtn(),因为obj是Type,System.Object,它是

没有提供名为Rtn的方法。你必须施放obj类型为

有一个名为Rtn的方法:


foreach(对象obj in coll)

{

if(obj是Cls1)

Console.WriteLine(((Cls1)obj).Rtn());

else if(obj)是Cls2)

Console.WriteLine(((Cls2)obj).Rtn());

else if(obj是Cls3)

Console.WriteLine(((Cls3)obj).Rtn());

}


显然,这根本不是很动态。


因为你想迟到绑定到一个Cls1,Cls2和Cls3的实例来调用

Rtn,这是一个方法,每个这些类有相同的

签名,你可以使用一个界面:


界面IReturn

{

string Rtn();

}


class Cls1:IReturn

{

public string Rtn(){return" From Cls1" ;; }

}


class Cls2:IReturn

{

public string Rtn(){return 来自Cls2; }

}


....


你可以这样使用界面:


IReturn [] returnsColl = new IReturn [] {

new Cls1(),new Cls2()

};


foreach(在returnColl中返回IRET)

Console.WriteLine(returningObject.Rtn());


-

Dave Sexton


" Praveen" < pr ***** @ newsgroup.nospamwrote in message

news:%2 **************** @ TK2MSFTNGP03.phx.gbl .. 。

You can''t call obj.Rtn() because obj is of the Type, "System.Object", which
doesn''t provide a method named, "Rtn". You must cast "obj" to a Type that
has a method named, "Rtn":

foreach (object obj in coll)
{
if (obj is Cls1)
Console.WriteLine(((Cls1) obj).Rtn());
else if (obj is Cls2)
Console.WriteLine(((Cls2) obj).Rtn());
else if (obj is Cls3)
Console.WriteLine(((Cls3) obj).Rtn());
}

Obviously, this isn''t very dynamic at all.

Since you''d like to late-bind to an instance of Cls1, Cls2 and Cls3 to call
"Rtn", a method which each of these classes has in common with the same
signature, you could use an interface:

interface IReturn
{
string Rtn();
}

class Cls1 : IReturn
{
public string Rtn() { return "From Cls1"; }
}

class Cls2 : IReturn
{
public string Rtn() { return "From Cls2"; }
}

....

And you can use the interface as such:

IReturn[] returningColl = new IReturn[] {
new Cls1(), new Cls2()
};

foreach (IReturn returningObject in returningColl)
Console.WriteLine(returningObject.Rtn());

--
Dave Sexton

"Praveen" <pr*****@newsgroup.nospamwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...


试图学习plymorphism。

我的样本是


公共类Class1

{

public static void Main(string [] args)

{

Cls1 x = new Cls1();

Cls2 y = new Cls2();

Cls3 y = new Cls3();


Object [] coll = new对象[] {x,y,z};


foreach(对象obj in coll)

{

Console.WriteLine (obj.Rtn());

//这是问题。

//如何获得Rtn()的返回值;

//我想使用循环和多态的概念


}

}

}


公共类Cls1

{

公共字符串Rtn()

{

返回从Cls1;

}

}


公共类Cls2

{

public string Rtn()

{

return" From Cls2" ;;

}

}


公共类Cls3

{

公共字符串Rtn()

{

返回" From Cls3" ;;

}

}
trying to learn plymorphism.
My sample is

public class Class1
{
public static void Main(string[] args)
{
Cls1 x = new Cls1();
Cls2 y = new Cls2();
Cls3 y = new Cls3();

Object[] coll = new Object[] {x,y,z};

foreach (Object obj in coll)
{
Console.WriteLine(obj.Rtn());
// here is the issue.
//How to get the return value of Rtn();
//I want to use the loop and the concept of polymorphism

}
}
}
public class Cls1
{
public string Rtn()
{
return "From Cls1";
}
}
public class Cls2
{
public string Rtn()
{
return "From Cls2";
}
}

public class Cls3
{
public string Rtn()
{
return "From Cls3";
}
}



嗨Praveen,


如果没有

你不能从object类型的变量调用Rtn方法将它转换为适当的类类型。

但是既然你想使用多态,你就不想强制转换它。


多态在这种情况下可以应用,可以通过以下方式在

中完成


- 从公共基本类型中提取Cls1,Cls2和Cls3。

基类型应该有一个虚方法Rtn,你应该在每个类Cls1,Cls2和cls3中覆盖




示例:

class BaseType

{

protected virtual s tt Rtn()

{

返回String.Empty;

}


}

class Cls1:BaseType

{

protected override string Rtn()

{

返回从Cls1;;

}

}

使用BaseType []而不是object []





- 创建一个定义方法Rtn的特定接口。

在每个类Cls1,Cls2中实现Rtn方法amd Cls3。

界面IBaseType

{

string Rtn();

}

class Cls1:IBaseType

{

public string Rtn()

{

return" From Cls1" ;;;

}

}

使用IBaseType []代替对象[]


此致,

Hameer Saleem

" Praveen"写道:
Hi Praveen,

You cannot invoke the Rtn method from the variable of type object without
casting it to the appropriate class type.
But since you want to use polymorphism, you dont want to cast it.

polymorphism can be applied in this case, can be accomplished in the
following ways

--Derive Cls1, Cls2 and Cls3 from a common base type.
The base type should have a virtual method Rtn which you should override
in each of the classes Cls1, Cls2 amd Cls3.

Example:
class BaseType
{
protected virtual string Rtn()
{
return String.Empty;
}

}
class Cls1 : BaseType
{
protected override string Rtn()
{
return "From Cls1";;
}
}
Use BaseType[] instead of object[]

OR

--Create a particular Interface which defines the method Rtn.
Implement the Rtn method in each of the classes Cls1, Cls2 amd Cls3.
interface IBaseType
{
string Rtn();
}
class Cls1 : IBaseType
{
public string Rtn()
{
return "From Cls1";;
}
}
Use IBaseType[] instead of object[]

Regards,
Hameer Saleem
"Praveen" wrote:

试图学习plymorphism。

我的样本是


公共类Class1

{

public static void Main(string [] args)

{

Cls1 x = new Cls1() ;

Cls2 y = new Cls2();

Cls3 y = new Cls3();


Object [] coll = new Object [] {x,y,z};


foreach(对象obj in coll)

{

控制台。 WriteLine(obj.Rtn());

//这是问题。

//如何获得Rtn()的返回值;

//我想使用循环和多态的概念


}

}

}


公共类Cls1

{

公共字符串Rtn()

{

返回从Cls1;

}

}


公共类Cls2

{

公共字符串Rtn()

{

返回从Cls2;

}

}


公共类Cls3

{

public string Rtn()

{

return" From Cls3" ;;

}

}

trying to learn plymorphism.
My sample is

public class Class1
{
public static void Main(string[] args)
{
Cls1 x = new Cls1();
Cls2 y = new Cls2();
Cls3 y = new Cls3();

Object[] coll = new Object[] {x,y,z};

foreach (Object obj in coll)
{
Console.WriteLine(obj.Rtn());
// here is the issue.
//How to get the return value of Rtn();
//I want to use the loop and the concept of polymorphism

}
}
}
public class Cls1
{
public string Rtn()
{
return "From Cls1";
}
}
public class Cls2
{
public string Rtn()
{
return "From Cls2";
}
}

public class Cls3
{
public string Rtn()
{
return "From Cls3";
}
}


这篇关于帮助这个示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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