主题:本人初学C#,在继承这儿遇到问题
本人初学C#,在继承这儿遇到问题,自己写了一个小小的程序
首先是类class1
using System;
using System.Collections.Generic;
using System.Text;
namespace inherit
{
public class Class1
{
private string a;
public Class1()
{
a = "hello world";
}
public string wa
{
get
{
return a;
}
}
public override string ToString()
{
return "321";
}
}
}
然后是class2:
using System;
using System.Collections.Generic;
using System.Text;
namespace inherit
{
public class Class2 : Class1
{
private string b;
public Class2()//:base()
{
b = "123";
}
public string ba
{
get
{
return b;
}
}
public override String ToString()
{
// return base.ToString();
return "I am Great";
}
}
}
主函数:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace inherit
{
class Program
{
static void Main(string[] args)
{
Class2 wang = new Class2();
MessageBox.Show(wang.ba);
MessageBox.Show(wang.wa);
MessageBox.Show(wang);
}
}
}
错误提示
错误 2 参数“1”: 无法从“inherit.Class2”转换为“string” 14 29
即此行 MessageBox.Show(wang); 有错,我应该怎么改啊!谢谢
有截图:
[img]http://img496.photo.163.com/wangboy2004/58880282/1577714277.jpg[/img]
首先是类class1
using System;
using System.Collections.Generic;
using System.Text;
namespace inherit
{
public class Class1
{
private string a;
public Class1()
{
a = "hello world";
}
public string wa
{
get
{
return a;
}
}
public override string ToString()
{
return "321";
}
}
}
然后是class2:
using System;
using System.Collections.Generic;
using System.Text;
namespace inherit
{
public class Class2 : Class1
{
private string b;
public Class2()//:base()
{
b = "123";
}
public string ba
{
get
{
return b;
}
}
public override String ToString()
{
// return base.ToString();
return "I am Great";
}
}
}
主函数:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace inherit
{
class Program
{
static void Main(string[] args)
{
Class2 wang = new Class2();
MessageBox.Show(wang.ba);
MessageBox.Show(wang.wa);
MessageBox.Show(wang);
}
}
}
错误提示
错误 2 参数“1”: 无法从“inherit.Class2”转换为“string” 14 29
即此行 MessageBox.Show(wang); 有错,我应该怎么改啊!谢谢
有截图:
[img]http://img496.photo.163.com/wangboy2004/58880282/1577714277.jpg[/img]