在派生类上禁用继承的方法

在派生类上禁用继承的方法,第1张

派生类上禁用继承的方法

我认为不可能。但是,您可以通过从其规范中 删除Rotate()* 方法来进一步优化 Shape 类,而定义另一个名为
RotatableShape 的Shape 子类 ,并让 CircleShape 派生 ,并从
RotatableShape 派生 所有其他 Rotatable类

***


例如:

public class Shape{ //all the generic methods except rotate()}public class RotatableShape extends Shape{ public void rotate(){    //Some Code here... }}public class Circle extends Shape{ //Your implementation specific to Circle}public class Rectangle extends RotatableShape{ //Your implementation specific to Rectangle}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5586482.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-14
下一篇 2022-12-14

发表评论

登录后才能评论

评论列表(0条)

保存