C#赋值 - 类层次结构

时间:2015-04-20 11:18:57

标签: 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();



        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();
    }
}

我需要制作一个合适的类层次结构,以便它可以工作,但我真的不知道如何>。>我知道它是关于继承的,但是我无法进行编译。

1 个答案:

答案 0 :(得分:0)

完成的:

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
        }
    }
}