在C#/ Java中使用抽象类的技术原因 [英] Technical reason for using Abstract Classes in C#/Java

查看:82
本文介绍了在C#/ Java中使用抽象类的技术原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据OOP,需要使用抽象类对那些在现实世界中不存在但充当多个现实世界对象的基类的对象进行建模。

According to OOP, Abstract Classes are needed to model those objects that have no existence in the real-world but serves as a base class for several real-world objects.

例如:

   BankAccount            
       /\
      /  \
     /    \
    /      \
Current     Savings
Account     Account

此处应该将BankAccount建模为抽象类。

Here BankAccount should be modeled as an Abstract Class.

但是在C#/ Java中使用抽象类的技术原因是什么? 链接文本

例如:

使用接口的OOP原因是对行为继承进行建模(没有真正层次关系的继承)。

The OOP reason for using Interfaces is to model Behavioral inheritance (Inheritance with no real hierarchical relationship).

在C#/ Java中使用接口的技术原因是为了解决多重继承的问题(如果我没记错!)。

The technical reason for using Interfaces in C#/Java is to solve the problem of multiple inheritance (If I am not wrong!).

推荐答案

如果可能的话,抽象类可以具有默认行为。

Abstract classes can have default behavior if something sensible is possible; interfaces cannot.

抽象类可以为所有方法或没有方法提供默认行为;开发人员的选择;

An abstract class can provide default behavior for ALL methods or no methods; developer's choice; interfaces cannot.

抽象类可以具有与所有子类共享的状态。界面不会指定状态。

Abstract classes can have state that's shared with all subclasses; interfaces don't specify state.

因此,您的抽象BankAccount可以具有一个余额属性,可以授予储蓄和支票访问权限。

So your abstract BankAccount can have a balance attribute that Savings and Checking can be granted access.

这篇关于在C#/ Java中使用抽象类的技术原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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