求助,如何才能实现呢

promising_90 2012-03-27 10:08:45
将运算符++和- -进行重载,使之可对字符型数据进行运算,即:对一个字符型变量a,当a=’L’时a++(或++a)的值为’M’, a- -(或- -a)的值为’K’
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication2
{
class text1
{
public char ch;

public text1()
{

Console.WriteLine("输入一个值:");

ch =char.Parse(Console.ReadLine());


}
public static text1 operator ++(text1 a)
{

text1 m = new text1();
if ('a' < a.ch && a.ch < 'z' || 'A' < a.ch && a.ch < 'Z')
m.ch = char.Parse("a.ch + 1");
else Console.WriteLine("error");
return m;
}


public static text1 operator --(text1 a)
{

text1 n = new text1();
if ('a' < a.ch && a.ch < 'z' || 'A' < a.ch && a.ch < 'Z')
n.ch = char.Parse("a.ch - 1");
return n;
}

}
class text
{
static void Main()
{

text1 a = new text1();
text1 b = ++a;

Console.WriteLine(b);

}
}
}
...全文
137 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
promising_90 2012-03-27
  • 打赏
  • 举报
回复
非常感谢
bdmh 2012-03-27
  • 打赏
  • 举报
回复

构造函数
public text1()
{
}
public text1(char c)
{
ch = c;
}
main函数
static void Main()
{
Console.WriteLine("输入一个值:");

char ch = char.Parse(Console.ReadLine());
text1 a = new text1(ch);
text1 b = ++a;

Console.WriteLine(b);

}

这样就不会再次输入了,你的代码还有错误,自己查查吧,别把char和string搞乱套了

111,126

社区成员

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

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

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