C# 赋值 - class 层次结构
C# assignment - class 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();
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();
}
}
我需要创建一个适当的 class 层次结构,这样它才能工作,但我真的不知道怎么做 >.> 我知道这是关于继承的,但我无法编译它。
完成:
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
}
}
}
所以我有这个代码:
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();
}
}
我需要创建一个适当的 class 层次结构,这样它才能工作,但我真的不知道怎么做 >.> 我知道这是关于继承的,但我无法编译它。
完成:
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
}
}
}