且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Java中更改重写方法的访问修饰符?

更新时间:2023-12-04 20:22:46

Java不允许您使访问修饰符更具限制性,因为这会违反规则子类实例应该可用于代替超类实例。但是当谈到使访问 less 限制性......好吧,也许超类是由不同的人编写的,并且他们没有预料到你想要使用他们的类的方式。

Java doesn't let you make the access modifier more restrictive, because that would violate the rule that a subclass instance should be useable in place of a superclass instance. But when it comes to making the access less restrictive... well, perhaps the superclass was written by a different person, and they didn't anticipate the way you want to use their class.

人们编写的程序和编程时出现的情况多种多样,语言设计者***不要猜测程序员可能想要做什么他们的语言。如果没有充分的理由为什么程序员能够使访问说明符在子类中的限制性较小(例如),那么***将该决定留给程序员。他们知道他们个人情况的具体情况,但语言设计师却不知道。所以我认为这是Java设计师的一个很好的调用。

The programs people write and the situations which arise when programming are so varied, that it's better for language designers not to "second-guess" what programmers might want to do with their language. If there is no good reason why a programmer should not be able to make access specifiers less restrictive in a subclass (for example), then it's better to leave that decision to the programmer. They know the specifics of their individual situation, but the language designer does not. So I think this was a good call by the designers of Java.