在C#中,构造函数与类型转化

来源:百度知道 编辑:UC知道 时间:2024/05/25 17:45:19
在C#中,定义一个类
class student
{
string n;
student(string name)
{
n=name;
}
}
main()
{
string name1="admin";
student stu=(student)name1; (1)
或student stu=new student(name1); (2)
}
请问第(1)种方式可以实现调用student的构造函数吗?

不可以,因为他们没有继承关系

如果两个类是有继承关系的,那样是可以转换的
比如:
class A
{
}
class B:A
{
}

这样的 可以
B b=new b()
A a =(A)b

不过这里实际上并不是执行构造,而是实例转换

楼主回家卖红薯去吧~

不能
不同的类型不能相互转换