这题真不会做~望高手指点!谢谢!

interface IA
{
void GetLongName(string name1,string name2,string longName);
//比较两个姓名长短,longName返回名字较长的一个,否则返回“两个一样长”
}
类A
class A
{
public void Compare(IA ic, out string compareStr)
{
string name1, name2;
//此处省去若干行,请自行补充
ic.GetMax(name1, name2, out compareStr);
}
}
实例化类A,怎样调用Compare方法获取名字长的一个





这是老师出的一个题,真有点看不懂,望高手帮忙实现!谢谢!
...全文
166 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
[Quote=引用楼主 sunle818 的回复:]
interface IA
{
        void GetLongName(string name1,string name2,string longName);
//比较两个姓名长短,longName返回名字较长的一个,否则返回“两个一样长”
}
类A
class A
    {
        public void Compare(IA ic, out string compareStr)
        {
            string name1, name2;
  //此处省去若干行,请自行补充
            ic.GetMax(name1, name2, out compareStr);         
        }
  }
实例化类A,怎样调用Compare方法获取名字长的一个


这是老师出的一个题,真有点看不懂,望高手帮忙实现!谢谢!
[/Quote]

很明显你那个类A掉了个接口继承,应该是 class A:IA
在A类里实现IA接口的GetLongName方法,
在Compare方法调用时,第一个接口参数直接用this,我想这才是你老师要考的一个知识点
至于Compare方法的内容也太容易了,你老师应该不会是考这个的
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 mylifeisforthewar 的回复:]
你定义的接口有什么用呢~!?

[/Quote]


同意!!
Jave.Lin 2009-12-11
  • 打赏
  • 举报
回复
太久没用Console了。

写错了一点局部应用。
Jave.Lin 2009-12-11
  • 打赏
  • 举报
回复
这是错误题,应该这样用。

interface IA 
{
void GetLongName(string name1,string name2,ref string longName);
}
类A
class A:IA
{
public void GetLongName(string name2,string name2,ref string longName)
{
longName=name1.Lenght>name1.Lenght?name1:name2;
}
}
class B
{
public void Compare(A a, ref string compareStr)
{
string name1, name2;
a.GetLongName(name1, name2, ref compareStr);
}
}


以上代码基础按你的源码来改,以下代码这样用更直观一点。

interface IA 
{
string GetLongName(string name1,string name2);
}
类A
class A:IA
{
public string GetLongName(string name2,string name2)
{
return name1.Lenght>name1.Lenght?name1:name2;
}
}
void Main
{
public string Compare(string name1, string name2)
{
A a=new A();
return a.GetLongName(name1, name2);
}
}
Mylifeisforthewar 2009-12-11
  • 打赏
  • 举报
回复
你定义的接口有什么用呢~!?
kingy2008 2009-12-11
  • 打赏
  • 举报
回复
实现得不错~!如果能加除错和判空那就更好了!
ProjectDD 2009-12-11
  • 打赏
  • 举报
回复
对的,你已经实现了,这题就是 考你如何用接口,和out参数
wendaoyang 2009-12-11
  • 打赏
  • 举报
回复
8楼的经典,我都当笔记抄下来了
Rotel-刘志东 2009-12-11
  • 打赏
  • 举报
回复
接口不能实例化的。
wuyq11 2009-12-11
  • 打赏
  • 举报
回复
接口不能直接实例化
不提供实现 它有类来实现
jbo126 2009-12-11
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 lijing3333 的回复:]
这语句是不是有问题啊?  接口也可以当参数传进方法啊?或许是我无知...
[/Quote]
有什么不可以吗,就像八楼的那样?传一个实现该接口的类的实例
  • 打赏
  • 举报
回复
顶咯~高手们帮忙解决下咯!
我是这么写的你们看对不!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication39
{
interface IA
{
void GetLongName(string name1, string name2, out string longName);
//比较两个姓名长短,longName返回名字较长的一个,否则返回“两个一样长”
}
//类A
class B : IA
{
public void GetLongName(string name1, string name2, out string longName)
{
if (name1.Length > name2.Length)
{
longName = name1;
}
else if (name1.Length < name2.Length)
{
longName = name2;
}
else
{
longName = "两个一样长";
}
}
}
class A
{
public void Compare(IA ic, out string compareStr)
{
string name1, name2;
//此处省去若干行,请自行补充
name1 = Console.ReadLine();
name2 = Console.ReadLine();
ic.GetLongName(name1, name2, out compareStr);

}
}
class Program
{
static void Main(string[] args)
{
B myclass = new B();
A myclass2 = new A();
string compareStr;
myclass2.Compare(myclass, out compareStr);
Console.WriteLine("长的那个是:" + compareStr);
}
}
}
yudi010 2009-12-11
  • 打赏
  • 举报
回复
没看懂
cymandhxl 2009-12-11
  • 打赏
  • 举报
回复
把接口实例化一下
lijing3333 2009-12-11
  • 打赏
  • 举报
回复
这语句是不是有问题啊? 接口也可以当参数传进方法啊?或许是我无知...
ProjectDD 2009-12-11
  • 打赏
  • 举报
回复
的确楼主的问题代码对不上号,
HarveyYan 2009-12-11
  • 打赏
  • 举报
回复

interface IA
{
void GetLongName(string name1,string name2,string longName);
//比较两个姓名长短,longName返回名字较长的一个,否则返回“两个一样长”
}
IA ic
ic.GetMax(name1, name2, out compareStr);


I服了Your老师
bancxc 2009-12-11
  • 打赏
  • 举报
回复
愣是没看懂

宝_爸 2009-12-11
  • 打赏
  • 举报
回复
我也没太看懂:(

111,123

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧