在C#中要实现一个类继承多个父类有什么办法
4个回答
展开全部
C#中的父类与子类的继承关系与C和C++中的类似,这里先阐述最重要的一点:假如子类继承了父类,那么子类可以强制转换为父类,并且保证编译和运行都不出错;但是父类强制转换成子类的时候,编译可以通过运行通不过。请看如下代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FatherAndSon
{
class ClassA
{
static void Main(string[] args)
{
//编译和运行都能通过
ClassB b = new ClassB();
ClassA a = (ClassA)b;
//编译能通过,运行通不过
ClassA aa = new ClassA();
ClassB bb = (ClassB)aa;
}
}
class ClassB : ClassA
{
}
}
实际上:将子类强制转换为父类之后,在用这个父类强制转换为另外一个子类的时候编译和运行也都能通过;只是如果将父类直接转换为子类的时候运行会出错,请看如下代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FatherAndSon
{
class ClassC
{
static void Main(string[] args)
{
//先将子类强制转换为父类,然后用此父类强制转换为另外一个子类(通过)
ClassB b = new ClassB();
ClassA a = (ClassA)b;
ClassB bbbbb = (ClassB)a;
//(将父类直接强制转换为子类)编译能通过,运行通不过
ClassA aa = new ClassA();
ClassB bb = (ClassB)aa;
}
}
class ClassA
{
}
class ClassB : ClassA
{
}
}
另外一个值得非常注意的地方是:无论是子类强制转换赋给父类,还是父类(由子类强转得到)强制转换为子类;类对象是根据声明的类型(子类或父类)去调用其变量和函数的,与赋值无关。请看如下代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FatherAndSon
{
class ClassC
{
static void Main(string[] args)
{
//先将子类强制转换为父类,然后用此父类强制转换为另外子类(通过)
ClassB b = new ClassB();
b.PrintFunc();
Console.WriteLine(b.myStr);
ClassA a = (ClassA)b;
a.PrintFunc();
Console.WriteLine(a.myStr);
ClassB bbbbb = (ClassB)a;
bbbbb.PrintFunc();
Console.WriteLine(bbbbb.myStr);
//(将父类直接强制转换为子类)编译能通过,运行通不过
//ClassA aa = new ClassA();
//ClassB bb = (ClassB)aa;
//bb.PrintFunc();
}
}
class ClassA
{
public string myStr;
public ClassA()
{
myStr = "Hello, This is myStr in ClassA";
}
public void PrintFunc()
{
Console.WriteLine("This is the Print Function in ClassA");
}
}
class ClassB : ClassA
{
public string myStr;
public ClassB()
{
myStr = "Hello, This is myStr in ClassB";
}
public void PrintFunc()
{
Console.WriteLine("This is the Print Function in ClassB");
}
}
}
上面代码输出的结果为:
This is the Print Function in ClassB
Hello, This is myStr in ClassB
This is the Print Function in ClassA
Hello, This is myStr in ClassA
This is the Print Function in ClassB
Hello, This is myStr in ClassB
可以看出:类的实例对象在调用其变量和方法时,确实是以声明成为的类为依据的。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FatherAndSon
{
class ClassA
{
static void Main(string[] args)
{
//编译和运行都能通过
ClassB b = new ClassB();
ClassA a = (ClassA)b;
//编译能通过,运行通不过
ClassA aa = new ClassA();
ClassB bb = (ClassB)aa;
}
}
class ClassB : ClassA
{
}
}
实际上:将子类强制转换为父类之后,在用这个父类强制转换为另外一个子类的时候编译和运行也都能通过;只是如果将父类直接转换为子类的时候运行会出错,请看如下代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FatherAndSon
{
class ClassC
{
static void Main(string[] args)
{
//先将子类强制转换为父类,然后用此父类强制转换为另外一个子类(通过)
ClassB b = new ClassB();
ClassA a = (ClassA)b;
ClassB bbbbb = (ClassB)a;
//(将父类直接强制转换为子类)编译能通过,运行通不过
ClassA aa = new ClassA();
ClassB bb = (ClassB)aa;
}
}
class ClassA
{
}
class ClassB : ClassA
{
}
}
另外一个值得非常注意的地方是:无论是子类强制转换赋给父类,还是父类(由子类强转得到)强制转换为子类;类对象是根据声明的类型(子类或父类)去调用其变量和函数的,与赋值无关。请看如下代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FatherAndSon
{
class ClassC
{
static void Main(string[] args)
{
//先将子类强制转换为父类,然后用此父类强制转换为另外子类(通过)
ClassB b = new ClassB();
b.PrintFunc();
Console.WriteLine(b.myStr);
ClassA a = (ClassA)b;
a.PrintFunc();
Console.WriteLine(a.myStr);
ClassB bbbbb = (ClassB)a;
bbbbb.PrintFunc();
Console.WriteLine(bbbbb.myStr);
//(将父类直接强制转换为子类)编译能通过,运行通不过
//ClassA aa = new ClassA();
//ClassB bb = (ClassB)aa;
//bb.PrintFunc();
}
}
class ClassA
{
public string myStr;
public ClassA()
{
myStr = "Hello, This is myStr in ClassA";
}
public void PrintFunc()
{
Console.WriteLine("This is the Print Function in ClassA");
}
}
class ClassB : ClassA
{
public string myStr;
public ClassB()
{
myStr = "Hello, This is myStr in ClassB";
}
public void PrintFunc()
{
Console.WriteLine("This is the Print Function in ClassB");
}
}
}
上面代码输出的结果为:
This is the Print Function in ClassB
Hello, This is myStr in ClassB
This is the Print Function in ClassA
Hello, This is myStr in ClassA
This is the Print Function in ClassB
Hello, This is myStr in ClassB
可以看出:类的实例对象在调用其变量和方法时,确实是以声明成为的类为依据的。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询