为什么我们不能创建Abstract类的对象? [英] Why we can't create object of Abstract class?

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

问题描述

//抽象类可能有公共构造函数但我们无法创建其对象。



公共抽象类MyAbstract

{

private int x;

public MyAbstract(int p)

{

this.x = p;

}

}



公共类MyClass:MyAbstract

{

public MyClass(int p):base(8)

{

p = 8;

}

}



公共舱你好

{

MyAbstract abss = new MyClass(3);

MyAbstract abss = new MyAbstract(4);

MyClass cls = new MyAbstract(5);

}



在上面的例子中,最后两行是不允许的。

//An Abstract class may have public constructor but we can not able to create its object.

public abstract class MyAbstract
{
private int x;
public MyAbstract(int p)
{
this.x = p;
}
}

public class MyClass : MyAbstract
{
public MyClass(int p) : base(8)
{
p = 8;
}
}

public class Hello
{
MyAbstract abss = new MyClass(3);
MyAbstract abss = new MyAbstract(4);
MyClass cls = new MyAbstract(5);
}

In the above example last two line is not permissible.

推荐答案

它无法完成,没有任何意义。对于类型,关键字 abstract 的整个目的是防止其实例化。注意,我说关键字的目的,而不是抽象类。然后它给我们带来了第二个问题:为什么要有抽象类?

仅用于一个目的:用作其他类的基类;但最终,其中一些课程应该是非抽象的,否则整个活动都会缺乏目的。



这样的抽象类可以用作用于访问某些多态集的编译时类型。要理解这一点,您需要了解OOP的核心:虚拟方法和后期绑定。它包括对编译时类型运行时类型的理解。当你有一个类实例时,可以通过某个变量或某些编译时类型的成员访问它,它可以是抽象的,但它的运行时类型总是非抽象的,否则你就没有实例。这些抽象类型用作集合的所有成员的公共接口,具有不同的运行时类型。如果这种类型不是抽象的,那么任何功能都不会改变:抽象是一种万无一失的措施,只是为了防止一些可能不了解这种类型目的的开发人员进行不必要的实例化。



-SA
It cannot be done and makes no sense. The whole purpose of the keyword abstract, for a type, is to prevent its instantiation. Note, I say "purpose of the keyword", not abstract classes. Then it brings us the the second question: why having abstract classes at all?
Only for one purpose: to be used as a base classes for other classes; but ultimately, some of those classes should be made non-abstract, otherwise the whole activity would lack the purpose.

Such abstract classes can be used as the compile-time types used for access to some polymorphic sets. To understand that, you need to understand the heart of OOP: virtual methods and late binding. It includes understanding of compile-time types vs. run-time types. When you have some class instance, it can be accessible through some variable or a member of some compile-time type, which can be abstract, but its runtime type is always non-abstract, otherwise you could not have an instance. Such abstract types are used as a common interface for all members of the set, having different runtime types. And if this type is not abstract, no functionality will change: abstract is a kind of fool-proof measure, just to prevent unwanted instantiation by some developers who might not understand the purpose of this type.

—SA


受保护的可能是框架强制执行机制的方式。但是框架阻止你做一些没有意义的事情。



抽象类是一个包含一些尚未实现的方法的类。它们必须由派生的具体类实现。因此,我们可以说抽象类是不完整的。如果你要实例化一个,那么这些方法就会有漏洞。该框架阻止您构建一个带有漏洞的类。因此,您只能实例化一个具体的类。
Protected might be the way that the framework enforces the mechanism. But the framework is preventing you from doing something that doesn't make sense.

An abstract class is a class that contains some methods that are not yet implemented. They must be implemented by a derived 'concrete' class. As such, we can say that an abstract class is 'incomplete'. If you were to instantiate one, it will have 'holes' where those methods are. The framework is preventing you from building a class with holes in it. Hence you can only instantiate a concrete class.


这篇关于为什么我们不能创建Abstract类的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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