【问题标题】:C# assignment - class hierarchy [closed]C# 赋值 - 类层次结构
【发布时间】:2015-04-20 11:18:56
【问题描述】:

所以我有这个代码:

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

我需要建立一个适当的类层次结构,这样它才能工作,但我真的不知道如何>。>我知道它是关于继承的,但我无法编译它。

【问题讨论】:

  • 你能告诉我们你的课程的代码吗?
  • 欢迎来到stackoverflow。你试过什么了?阅读How to Ask
  • 我到目前为止:A类{}B类:A{}C类:B{}D类:I{}E类:J{}I类:J{}J类{ }

标签: c# class inheritance variable-assignment hierarchy


【解决方案1】:

完成:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-19
    • 1970-01-01
    • 1970-01-01
    • 2018-08-09
    • 2011-01-21
    • 1970-01-01
    相关资源
    最近更新 更多