我们可以为抽象类创建一个对象 [英] can we create an object for abstract class

查看:88
本文介绍了我们可以为抽象类创建一个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





i am princes



请清除我的怀疑

解决方案

抽象类的重点在于它不是要实例化的 - 你不能创建那种类型的对象。相反,它旨在提供一个框架,可以在其上构建实际实例。



例如,想想汽车:汽车是一种抽象类型:你从不买A car - 你买的是A Ford Fiesta或A Ferrari 458,但它们都来自基本的Car级别,它们有Car属性和方法的共同点:四个轮子,某个地方为司机坐,转向,引擎等。你可以跳进任何Car派生的物体,并期望能够驾驶它(至少在停车场附近)。

所以抽象的Car类可能有:

< pre lang =c#> public abstract Car
{
public abstract void StartEngine();

public 覆盖 TurnLeft(浮动度)
{
转(度);
}
public 覆盖 TurnRight(浮动度)
{
转(-degrees);
}
}

这需要您创建一个SatrtEngine方法,但您可以选择创建自己的TurnLeft和TurnRight方法。



有很多关于抽象类的想法,所以首先看一下MSDN文档: http://msdn.microsoft.com/en-gb/library/sf985hc5.aspx [ ^ ]


不,抽象类就是:abstract。它无法创建,因为它的某些部分尚未完全定义。抽象类只能用作实际或具体类的基础。


抽象类无法实例化但我们可以继承抽象类。

有关详细信息,请参阅 http://www.dotnet-tricks.com/教程/ csharp / E0K2011113-A-Deep-Dive-into-C [ ^ ]


hi,

i am princes

please clear my doubt

解决方案

The whole point of an abstract class is that it is not intended to be instantiated - that you can't create an object of that type. Instead, it is intended to provide a "framework" on which actual instances can be built.

For an example, think about cars: A Car is an abstract type: you never buy "A car" - you buy "A Ford Fiesta" or "A Ferrari 458", but they both derive from the base "Car" class, and they have "Car" properties and methods in common: four wheels, somewhere for the driver to sit, steering, engine, and so on. You can jump into any Car derived object and expect to be able to steer it (at least around a car park).
So the abstract Car class might have:

public abstract Car
   {
   public abstract void StartEngine();

   public override TurnLeft(float degrees)
      {
      Turn(degrees);
      }
   public override TurnRight(float degrees)
      {
      Turn(-degrees);
      }
   }

Which would require you to create a SatrtEngine method, but you can optionally cretae your own TurnLeft and TurnRight methods.

There is a lot to think about with abstract classes, so try having a look at the MSDN documentation first: http://msdn.microsoft.com/en-gb/library/sf985hc5.aspx[^]


No, an abstract class is just that: abstract. It cannot be created since there are parts of it that are not fully defined. An abstract class may only be used as the base for a real or concrete class.


Abstract Class can't be instantiated but we can inherit Abstract class.
For more information see http://www.dotnet-tricks.com/Tutorial/csharp/E0K2011113-A-Deep-Dive-into-C[^]


这篇关于我们可以为抽象类创建一个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆