在 C#/Java 中使用抽象类的技术原因

发布于 2024-07-29 07:45:58 字数 491 浏览 1 评论 0原文

根据OOP,需要抽象类来建模那些在现实世界中不存在但作为多个现实世界对象的基类的对象。

例如:

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

这里 BankAccount 应建模为抽象类。

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

例如:

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

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

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.

For Example:

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

Here BankAccount should be modeled as an Abstract Class.

But what is the technical reason for using Abstract Classes in C#/Java?link text

For example:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

反目相谮 2024-08-05 07:45:58

如果可能的话,抽象类可以有默认行为; 接口不能。

抽象类可以为所有方法提供默认行为,也可以不提供任何方法; 开发商的选择; 接口不能。

抽象类可以具有与所有子类共享的状态; 接口不指定状态。

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

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.

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

顾冷 2024-08-05 07:45:58

我不确定我是否理解你的问题,但是,抽象类无法实例化,因此它仅用于此目的。

Im not sure I understand your question but, an abstract class cannot be instantiated so it only serves this purpose.

ぶ宁プ宁ぶ 2024-08-05 07:45:58

我将举一个现实世界的例子。 我有一个名为 cSourceControl 的抽象类。 然后我有一个用于 Visual Source Safe 的类(称为 cVSS)和一个用于 Subversion 的类(称为 cSVN),两者都继承自 cSourceControl。

现在,抽象类有一堆属性和方法(是的,带有代码),继承类可以使用它们。 抽象类还定义了继承类必须实现的一系列抽象方法:

public abstract DataTable getFiles(string path, string filter, DateTime since, bool filterAuthorByLastCheckin, bool expandAll, bool onlySinceBranched);
public abstract DataTable getFiles(string path, string filter, DateTime since, string lastUser, bool filterAuthorByLastCheckin, bool expandAll, bool onlySinceBranched);
public abstract long getFile(string sFileName, string sVersion, string sLocalFileName);
public abstract DataTable getFileVersions(string sFileName);
public abstract DataTable getDirectories(string path, bool expandAll);
public abstract DataTable getChangedFiles(string path);
public abstract DataTable GetFileLogRevision(string path, string revision);
public abstract DateTime getBranchStartDateTime(string sBranch);

从这里您可以看出,SourceControl 类将需要其中的每一个,但它在 cVSS 中的外观与在 cSVN 中的外观非常不同。 另一个好处是在运行时我可以选择 VSS 或 SVN。 一个简化的代码片段可能是:

cSourceControl sc;

if(usingVSS)
   sc = new cVSS();
else
   sc = new cSVN();

DataTable dtFiles = sc.getChangedFiles("myproject/branches/5.1/");
etc.

现在,当我的客户开始要求使用 Git 或 SourceGear 时,我只需要创建这些类,并且几乎不需要更改其他内容。

I'll give a real world example. I have an abstract class call cSourceControl. Then I have a class for Visual Source Safe call cVSS and one for Subversion called cSVN, both inherited from cSourceControl.

Now the abstract class has a bunch properties and methods (yes, with code) that the inheriting classes can just use. The abstract class also defines a bunch of abstract methods that an inherited class must implement:

public abstract DataTable getFiles(string path, string filter, DateTime since, bool filterAuthorByLastCheckin, bool expandAll, bool onlySinceBranched);
public abstract DataTable getFiles(string path, string filter, DateTime since, string lastUser, bool filterAuthorByLastCheckin, bool expandAll, bool onlySinceBranched);
public abstract long getFile(string sFileName, string sVersion, string sLocalFileName);
public abstract DataTable getFileVersions(string sFileName);
public abstract DataTable getDirectories(string path, bool expandAll);
public abstract DataTable getChangedFiles(string path);
public abstract DataTable GetFileLogRevision(string path, string revision);
public abstract DateTime getBranchStartDateTime(string sBranch);

From this you can tell that a SourceControl class will need each of these, but the way it looks in cVSS is very different than how it looks in cSVN. Another payoff is that at runtime I can select VSS or SVN. A simplified code snip might be:

cSourceControl sc;

if(usingVSS)
   sc = new cVSS();
else
   sc = new cSVN();

DataTable dtFiles = sc.getChangedFiles("myproject/branches/5.1/");
etc.

Now when my customers start asking for Git or SourceGear, I just have to create those classes and change very little else.

假装爱人 2024-08-05 07:45:58

抽象类无法实例化,因为它们缺少方法定义。

您可以在超类中定义一个垃圾(不执行任何操作)方法函数。 但这很愚蠢。 不要定义任何事情,而不是定义“什么都不做”。

由于缺少主体,该方法是抽象的。 这使得整个类变得抽象。


为什么?

因为您不想指定“不执行任何操作”的方法主体。 您希望完全省略方法主体,留下一个占位符由子类填充。

您创建超类来包含子类之间的常见行为。 在某些情况下,超类不能是完整的类,因为每个子类必须提供重写或扩展。

我们提供一个抽象超类,因为我们需要子类来提供缺少的功能。

Abstract classes can't be instantiated because they lack method definitions.

You could define a junk -- do nothing -- method function in the superclass. But that's silly. Rather than define a "do nothing", don't define anything.

With a body missing, the method is abstract. That makes the class as a whole abstract.


Why?

Because you don't want to specify a "do nothing" method body. You want to omit the method body entirely, leaving a place-holder to be filled by a subclass.

You create superclasses to contain common behavior among subclasses. In some cases, the superclass cannot be a complete class because each subclass must provide an override or extension.

We provide a abstract superclass because we need subclasses to provide the missing features.

挽你眉间 2024-08-05 07:45:58

抽象类适用于外部利益相关者。 经理们制定了一项程序,因为公司将于上午 9 点开门,摘要对于利益相关者来说很容易看到。 但对软件开发人员实施一项额外规则,即他们可以上午 10 点来。 因此管理者及下属制定的规则都是抽象的、易于可见的。 下午六点从公司出来。 你可以看到抽象的方法,可以了解公司的文化。

Abstract classes are for outside stakeholders. One procedure is set by managers as abstract is easily visible to stakeholders is company will open on 9am. But implement one extra rule for software developers that they may come 10am. So rules set by managers and below are implemented by abstract and easily visible. 6pm is exit from the company. You can see abstract methods and can know the culture of company.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文