什么是脆弱的基类问题? [英] What is the fragile base class problem?

查看:435
本文介绍了什么是脆弱的基类问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

java中脆弱的基类问题是什么?

What is the fragile base class problem in java?

推荐答案

脆弱的基类是继承的常见问题,适用于Java和支持继承的任何其他语言。

A fragile base class is a common problem with inheritance, which applies to Java and any other language which supports inheritance.

简而言之,基类是您继承的类,它通常被称为脆弱的,因为更改为这个类在从它继承的类中会有意想不到的结果。

In a nutshell, the base class is the class you are inheriting from, and it is often called fragile because changes to this class can have unexpected results in the classes that inherit from it.

减轻这种情况的方法很少;但是在使用继承时没有直接的方法可以完全避免它。您可以通过在Java中将类声明标记为 final 来阻止从类继承的其他类。

There are few methods of mitigating this; but no straightforward method to entirely avoid it while still using inheritance. You can prevent other classes inheriting from a class by labelling the class declaration as final in Java.

最佳实践避免这些问题中最糟糕的是将所有类标记为final,除非您特意打算从它们继承。对于那些打算继承的人来说,就像设计一个API一样设计它们:隐藏所有的实现细节;要严格对待你所发出的事情并注意你接受的内容,并详细记录该课程的预期行为。

A best practice to avoid the worst of these problems is to label all classes as final unless you are specifically intending to inherit from them. For those to intend to inherit from, design them as if you were designing an API: hide all the implementation details; be strict about what you emit and careful about what you accept, and document the expected behaviour of the class in detail.

这篇关于什么是脆弱的基类问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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