예제 #1
0
        public void AsIsTest()
        {
            Person p = new Person();
            Console.Write("Person: ");
            Console.WriteLine(p.getName());

            Object j = new John();
            Object a = new Anne();
            Object n = new Person();

            Console.WriteLine("Cast to super class");
            if (j is Person)
            {
                Person _j = j as Person;
                Console.WriteLine(_j.getName());
            }
            if (a is Person)
            {
                Person _a = a as Person;
                Console.WriteLine(_a.getName());
            }
            Console.WriteLine("Cast to specific class");
            if (j is John)
            {
                John _j = j as John;
                Console.WriteLine(_j.getName());
            }
            if (a is Anne)
            {
                Anne _a = a as Anne;
                Console.WriteLine(_a.getName());
            }
            if (n is John)
            {
                Console.WriteLine("error");
            }
        }