您如何知道是创建类,子类,抽象类还是接口? [英] How do you know whether to make a class, a subclass, an abstract class, or an interface?

查看:74
本文介绍了您如何知道是创建类,子类,抽象类还是接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常在编写代码时编写代码,但是我注意到从长远来看这需要更多的时间,特别是当我忘记了代码在哪里做什么时,我想通过制作UML类图和模型来开始规划我的代码然而,就像标题所说,你怎么知道是否要创建一个类,一个子类,一个抽象类或一个接口是否有一些大多数程序员遵循的指导或经验法则,如果有人能够向我解释这个问题以上简单来说将是一个很大的帮助,



我尝试过:



搜索谷歌,但答案不简单或没有解释,

I usually write code as I go along, but I noticed this takes more time in the long run especially when I lose track of what codes does what where, but I wanted to start planning my code by making UML class diagrams and mockups however, like the title says, how do you know whether to make a class, a subclass, an abstract class, or an interface is there some guide line or rule of thumb that most programmers follow, if someone can please explain to me the question above in simple terms that will be a great help,

What I have tried:

Searched google but the answers are no simple or not explained to well,

推荐答案

Lesson:类和对象(Java™教程>学习Java语言) [ ^ ]。


是的,有指导方针。



Yes, there are guidelines.

  • You create a class hierarchy (abstract/base class, concrete class) as a result of refactoring[^].

  • You create (and implement) interfaces to facilitate separation of concerns[^] to increase code maintainability, and dependency injection[^] to make your code easier to test and make it more flexible by enabling run-time resolution of a dependency (e.g. a service).


这篇关于您如何知道是创建类,子类,抽象类还是接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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