非抽象类可以有抽象方法吗?如果是,那么为什么,如果没有那么为什么呢? [英] Can a non abstract class have abstract method? If yes then why and if no then also why?

查看:364
本文介绍了非抽象类可以有抽象方法吗?如果是,那么为什么,如果没有那么为什么呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友们,



我正在读一篇关于抽象课程中是否有非抽象方法的文章。



然后在阅读一个问题突然出现在我的脑海中时,副词也可能是真的,即,如果有人愿意在非抽象类中制作一个抽象方法,那么它会有用吗还是它会给出一个编译时错误。?



这个问题对某些人来说可能是新手,但理论上我的OOP概念有点低。



谢谢



Varun

Dear Friends,

I was reading an article on whether there could be non abstract methods in abstract classes or not.

Then while reading a question pops up into my mind that is vice-verse can also be true i.e., If some one is willing to make a abstract method in a non abstract class then will it be helpful or it will give a compile time error.?

This question might be novice for some but i am a little low in OOPs concepts theoretically.

Thanks

Varun

推荐答案

没有。您可以通过编写代码并尝试编译来轻松检查它。使用关键字abstract来表示类是抽象的指示是一种傻瓜式的功能。它几乎就像没有未声明的变量一样。



您可以定义一个没有抽象方法/属性的抽象类。它是安全的并且可能有意义,因为它表明该类旨在成为某些非抽象类的基类,并用作公共接口。它不需要抽象类甚至后期绑定。



另一方面,如果至少有一个没有抽象方法/属性,它需要类是抽象。如果类不是抽象的(允许实例化),那将是不安全的,因为那么对抽象方法/属性的调用将是一场灾难。



关键字摘要应用于类别不如上面解释的规则重要,因为抽象性只能基于成员来确定。不过,这是额外的方便和更简单的语法,也更具可读性。



-SA
No. And you can easily check it up by writing code and trying to compile it. The indication that a class is abstract with the use of the keyword "abstract" is a sort of a fool-proof feature. It''s pretty much the like the absence of non-declared variables.

You can define an abstract class with no abstract methods/properties. It is safe and may make sense, as it indicates that the class is intended to be a base class for some non-abstract classes and serve as a common interface. It does not require abstract classes and even late binding.

From the other hand, if there is at least one no abstract method/property, it requires the class to be abstract. If the class was not abstract (allowed instantiation), it would be unsafe, because then the call to the abstract method/property would be a disaster.

The keyword "abstract" applied to the class is less important than the rule explained above, as the "abstractness" could be determined only based on the members. Nevertheless, this is extra convenience and more fool-proof syntax, also more readable.

—SA


这篇关于非抽象类可以有抽象方法吗?如果是,那么为什么,如果没有那么为什么呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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