抽象方法和虚拟方法有什么区别?在哪些情况下,建议使用抽象方法或虚拟方法?哪一种是最好的方法?
当前回答
抽象函数或方法是由类公开的公共“操作名称”,其目的与抽象类一起,主要是在对象设计中针对对象必须实现的结构提供一种形式的约束。
事实上,从其抽象类继承的类必须为该方法提供一个实现,通常编译器在不实现时会引发错误。
使用抽象类和方法非常重要,主要是为了避免在设计类时关注实现细节,从而导致类结构与实现过于相关,从而在它们之间协作的类之间创建依赖关系和耦合。
虚拟函数或方法只是一种模拟类的公共行为的方法,但我们可以在继承链中自由修改它,因为我们认为子类可能需要为该行为实现一些特定的扩展。
它们都代表了面向对象范式中的一种多元化。
我们可以一起使用抽象方法和虚拟函数来支持一个好的继承模型。
我们为解决方案的主要对象设计了一个很好的抽象结构,然后通过定位那些更易于进一步专门化的对象来创建基本实现,我们将这些对象作为虚拟对象,最后我们专门化我们的基本实现,最终“覆盖”继承的虚拟对象。
其他回答
图.-命题的传统三重分类。
在道义逻辑(义务和许可的研究)中,每个命题都是强制性的(“必须”运算符)、可选的(“可能”运算符)或不允许的(“不得”运算符),任何命题都不属于这三类中的一类。
此外,允许(“可能”运算符)命题是强制性或可选的命题,不允许(“未必”运算符)的命题是不允许或可选的,非可选(“必须或不得”运算符)是强制性或不允许的命题。
特别是,强制性命题是允许的,而不允许的命题是不允许的。
将这些运算符应用于命题“方法被覆盖”会产生以下命题:
抽象(纯)/具体方法:该方法必须被重写/不能被重写;virtual/real(final)方法:该方法可以被重写/不能被重写。
特别是,抽象方法是虚拟的,而真实方法是具体的。
只有抽象类才能有抽象成员。从抽象类继承的非抽象类必须重写其抽象成员。抽象成员是隐式虚拟的。抽象成员不能提供任何实现(抽象在某些语言中称为纯虚拟)。
据我所知:
抽象方法:
只有抽象类才能保存抽象方法。派生类也需要实现该方法,并且类中没有提供任何实现。
虚拟方法:
类可以声明这些并提供其实现。派生类还需要实现方法来重写它。
我在一些地方看到抽象方法的定义如下**
“必须在子类中实现抽象方法”
**我觉得是这样。
如果子类也是抽象的,则不必在子类中实现抽象方法。。
1) 抽象方法不能是私有方法。2) 抽象方法不能在同一抽象类中实现。
我会说。。如果我们要实现一个抽象类,您必须重写基础抽象类中的抽象方法。因为使用重写关键字实现抽象方法。类似于虚拟方法。
虚拟方法不必在继承类中实现。
----------CODE--------------
public abstract class BaseClass
{
public int MyProperty { get; set; }
protected abstract void MyAbstractMethod();
public virtual void MyVirtualMethod()
{
var x = 3 + 4;
}
}
public abstract class myClassA : BaseClass
{
public int MyProperty { get; set; }
//not necessary to implement an abstract method if the child class is also abstract.
protected override void MyAbstractMethod()
{
throw new NotImplementedException();
}
}
public class myClassB : BaseClass
{
public int MyProperty { get; set; }
//You must have to implement the abstract method since this class is not an abstract class.
protected override void MyAbstractMethod()
{
throw new NotImplementedException();
}
}
在这里,我写了一些示例代码,希望这可能是一个非常实际的例子,可以从非常基本的层面上看到接口、抽象类和普通类的行为。如果您想将其用作演示,也可以在github中找到此代码作为项目:https://github.com/usavas/JavaAbstractAndInterfaceDemo
public interface ExampleInterface {
// public void MethodBodyInInterfaceNotPossible(){
// }
void MethodInInterface();
}
public abstract class AbstractClass {
public abstract void AbstractMethod();
// public abstract void AbstractMethodWithBodyNotPossible(){
//
// };
//Standard Method CAN be declared in AbstractClass
public void StandardMethod(){
System.out.println("Standard Method in AbstractClass (super) runs");
}
}
public class ConcreteClass
extends AbstractClass
implements ExampleInterface{
//Abstract Method HAS TO be IMPLEMENTED in child class. Implemented by ConcreteClass
@Override
public void AbstractMethod() {
System.out.println("AbstractMethod overridden runs");
}
//Standard Method CAN be OVERRIDDEN.
@Override
public void StandardMethod() {
super.StandardMethod();
System.out.println("StandardMethod overridden in ConcreteClass runs");
}
public void ConcreteMethod(){
System.out.println("Concrete method runs");
}
//A method in interface HAS TO be IMPLEMENTED in implementer class.
@Override
public void MethodInInterface() {
System.out.println("MethodInInterface Implemented by ConcreteClass runs");
// Cannot declare abstract method in a concrete class
// public abstract void AbstractMethodDeclarationInConcreteClassNotPossible(){
//
// }
}
}
推荐文章
- 在支持循环和函数的语言中,是否存在“goto”的合法用例?
- 为什么不使用异常作为常规的控制流呢?
- 如何在方法中访问“静态”类变量?
- 什么是序列化?
- 为什么c#不提供c++风格的'friend'关键字?
- String, StringBuffer和StringBuilder
- 存储库和服务层的区别?
- 每个递归都可以转换成迭代吗?
- DDD -实体不能直接访问存储库的规则
- 为什么STL如此严重地基于模板而不是继承?
- 如何在Objective-C中声明类级属性?
- 面向方面编程与面向对象编程
- 什么是ORM,它是如何工作的,我应该如何使用它?
- 我能在服务器端应用程序(PHP、Ruby、Python等)上读取URL的哈希部分吗?
- c++中类似于java的instanceof