您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

使用java多态的好处有哪些

2025/3/22 14:50:33发布18次查看
好处
1、类调用者对类的使用成本进一步降低。多态只需要知道对象具有某个方法。
封装是让类的调用者不需要知道类的实现细节。
多态能让类的调用者连这个类的类型是什么都不必知道, 只需要知道这个对象具有某个方法即可。
2、可扩展能力更强,如果要新增一种新的形状,使用多态的方式代码改动成本也比较低。
对于类的调用者来说(drawshapes方法), 只要创建一个新类的实例就可以了, 改动成本很低。
实例
class cycle {private string name = cycle; public static void travel(cycle c) {system.out.println(cycle.ride() + c);} public string tostring() {return this.name;}} class unicycle extends cycle {private string name = unicycle;public string tostring() {return this.name;}} class bicycle extends cycle {private string name = bicycle;public string tostring() {return this.name;}} class tricycle extends cycle {private string name = tricycle;public string tostring() {return this.name;}} public class demo1 {public static void ride(cycle c) {c.travel(c);}public static void main(string[] args) {unicycle unicycle = new unicycle();bicycle bicycle = new bicycle();tricycle tricycle = new tricycle();ride(unicycle);ride(bicycle);ride(tricycle);}}
以上就是使用java多态的好处有哪些的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product