关于继承类是否需要强制转换的为题

lightheartedfrog 2014-09-28 04:55:15
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
dog dog = new dog();
animal animal = (animal)dog;// 问题在此处:为何去掉(animal)后结果一样。是不是意味着基类引用指向继承实例时不需要强制转换?有隐式转换?
animal.eatfood();
}
}

class animal
{
virtual public void eatfood()
{
Console.WriteLine("this is animal eat food");
Console.ReadKey();

}
}

class dog : animal
{
override public void eatfood()
{
Console.WriteLine("this is dog eat food");
Console.ReadKey();
}
}
}
...全文
118 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
OenAuth.Core 2014-10-08
  • 打赏
  • 举报
回复
引用 5 楼 caozhy 的回复:
[quote=引用 1 楼 anglecloudy 的回复:] 其实这是C#一个很冷的语法--向上转型 语法规定的可以这样用。无需解释。但它不是强制转换,强制转换是向下转换
"向上转型"是什么?请说英文。 这么写还是有区别的:
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();            
        }
    }
}
[/quote] 向上转型:upcasting 所有面向对象语言的基础语法之一,忘了在哪本书上看的了
j911 2014-09-29
  • 打赏
  • 举报
回复
都在写类库么?继承这个东西用的有这么频繁么?惭愧写了 3年c++代码,没用过继承,没用过模板,没用过泛型
smthgdin_020 2014-09-28
  • 打赏
  • 举报
回复
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方法
smthgdin_020 2014-09-28
  • 打赏
  • 举报
回复
引用 楼主 lightheartedfrog 的回复:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { dog dog = new dog(); animal animal = (animal)dog;// 问题在此处:为何去掉(animal)后结果一样。是不是意味着基类引用指向继承实例时不需要强制转换?有隐式转换? animal.eatfood(); } } class animal { virtual public void eatfood() { Console.WriteLine("this is animal eat food"); Console.ReadKey(); } } class dog : animal { override public void eatfood() { Console.WriteLine("this is dog eat food"); Console.ReadKey(); } } }
子类转父类是安全的,是能够覆盖父类的,所以不需要强制转换。
  • 打赏
  • 举报
回复
这类代码没有什么瘾式转换,根本不需要运行时去转换,编译器根本不编译出那种代码,根本没有那种逻辑。
  • 打赏
  • 举报
回复
一个 Dog 本来就是一个 Animal,无需特意告知编译器,它自己知道。
threenewbee 2014-09-28
  • 打赏
  • 举报
回复
引用 1 楼 anglecloudy 的回复:
其实这是C#一个很冷的语法--向上转型 语法规定的可以这样用。无需解释。但它不是强制转换,强制转换是向下转换
"向上转型"是什么?请说英文。 这么写还是有区别的:
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();            
        }
    }
}
漫天雪飞 2014-09-28
  • 打赏
  • 举报
回复
不需要,因为子类肯定包含父类的一切东西
於黾 2014-09-28
  • 打赏
  • 举报
回复
int i = 12; double d = i; 就类似这样,低精度转高精度不需要强制转换,可以隐式转换 子类转基类也不需要强制转换 反过来就不行了
Wang-Yucai 2014-09-28
  • 打赏
  • 举报
回复
是的。不需要强制转换。
OenAuth.Core 2014-09-28
  • 打赏
  • 举报
回复
其实这是C#一个很冷的语法--向上转型 语法规定的可以这样用。无需解释。但它不是强制转换,强制转换是向下转换

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧