int i = 12;
            double d = i;
就类似这样,低精度转高精度不需要强制转换,可以隐式转换
子类转基类也不需要强制转换
反过来就不行了

解决方案 »

  1.   


    "向上转型"是什么?请说英文。这么写还是有区别的:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;namespace ConsoleApplication1
    {
        class Animal
        {
            public void A() { Console.WriteLine("A"); }
        }    class Dog : Animal
        {
            public new void A() { Console.WriteLine("D"); }
        }    class Program
        {
            static void Main(string[] args)
            {
                Dog dog = new Dog();
                dog.A();
                ((Animal)dog).A();            
            }
        }
    }
      

  2.   

    一个 Dog 本来就是一个 Animal,无需特意告知编译器,它自己知道。
      

  3.   

     class Program
        {
            static void Main(string[] args)
            {
                Dog dog = new Dog();
                dog.A();
                ((Animal)dog).A();            
            }
        } dog.A();                     //直接使用子类的A方法,子类用一个A方法替换基类的A方法,关键字new
    ((Animal)dog).A();       //使用父类的A方法
          
      

  4.   


    "向上转型"是什么?请说英文。这么写还是有区别的:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;namespace ConsoleApplication1
    {
        class Animal
        {
            public void A() { Console.WriteLine("A"); }
        }    class Dog : Animal
        {
            public new void A() { Console.WriteLine("D"); }
        }    class Program
        {
            static void Main(string[] args)
            {
                Dog dog = new Dog();
                dog.A();
                ((Animal)dog).A();            
            }
        }
    }向上转型:upcasting 所有面向对象语言的基础语法之一,忘了在哪本书上看的了