C#赋值-类层次结构
所以我有这个代码:C#赋值-类层次结构,c#,class,inheritance,variable-assignment,hierarchy,C#,Class,Inheritance,Variable Assignment,Hierarchy,所以我有这个代码: class Program { static void Main(string[] args) { B b = new B(); A a = new A(); A c = new C(); I i = new I(); I k = new D(); J j = new E(); J d = new D(); Cons
class Program
{
static void Main(string[] args)
{
B b = new B();
A a = new A();
A c = new C();
I i = new I();
I k = new D();
J j = new E();
J d = new D();
Console.WriteLine(c is B); //this should be true
Console.WriteLine(i is J); //this should be false
Console.WriteLine(b is A); //this should be true
Console.WriteLine(d is A); //this should be false
Console.WriteLine(d is E); //this should be true
Console.WriteLine(k is E); //this should be true
Console.WriteLine(c is I); //this should be false
Console.ReadKey();
}
}
我需要建立一个适当的类层次结构,这样它才能工作,但我真的不知道如何>>我知道它是关于继承的,但我无法编译它。完成:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace UnitTestProject2
{
class A
{
}
class B : A
{
}
class C : B
{
}
class D : E
{
}
class E : J
{
}
class I
{
}
class J : I
{
}
[TestClass]
public class test
{
[TestMethod]
public void t()
{
B b = new B();
A a = new A();
A c = new C();
I i = new I();
I k = (I)new D();
J d = (J)new D();
J j = (J)new E();
Assert.IsTrue(c is B); //this should be true
Assert.IsFalse(i is J); //this should be false
Assert.IsTrue(b is A); //this should be true
Assert.IsFalse(d is A); //this should be false
Assert.IsTrue(d is E); //this should be true
Assert.IsTrue(k is E); //this should be true
Assert.IsFalse(c is I); //this should be false
}
}
}
你能给我们看看你的类的代码吗?还有stackoverflow。到目前为止你试过什么?到目前为止,我有:A类{}类B:A{}类C:B{}类D:I{}类E:J{}类I:J{}类J{}