面向对象-继承与多态
大约 2 分钟
面向对象-继承与多态
继承
在Java中,继承是一种重要的面向对象编程概念,它允许子类(派生类)从父类(基类)继承属性和方法。继承的主要目的是实现代码重用和建立类之间的关系,使代码更加模块化和易于维护。
在Oinone平台中,关于函数的继承涉及到了命名空间(namespace)的概念,这在Java中并不是直接的概念,但我们可以理解为类和包名的概念。
假设我们有一个父类 PetShop,它有一个 sayHello 的函数。子类可以继承 PetShop 并且自动拥有 sayHello 函数,这是继承的一种体现。子类继承了父类的方法,也可以添加自己的新方法。
子类在继承父类的方法时,会继承相同的函数名称和函数签名,因此在命名空间中保持一致。这样做的好处是保持了统一的接口,使得使用继承的代码更加清晰和易于维护。
多态
Oinone的多态,我们只提供覆盖功能,不提供重载,因为Oinone相同 name和fun的情况下不会去识别参数个数和类型。
多态实践
Step1 为PetShop新增hello函数
package pro.shushi.pamirs.demo.api.model;
…… //import
@Model.model(PetShop.MODEL_MODEL)
@Model(displayName = "宠物店铺",summary="宠物店铺",labelFields ={"shopName"} )
public class PetShop extends AbstractDemoIdModel {
public static final String MODEL_MODEL="demo.PetShop";
…… //省略其他代码
@Function(openLevel = FunctionOpenEnum.API)
@Function.Advanced(type= FunctionTypeEnum.QUERY)
public PetShop sayHello(PetShop shop){
PamirsSession.getMessageHub().info("Hello:"+shop.getShopName());
return shop;
}
@Function(name = "sayHello2",openLevel = FunctionOpenEnum.API)
@Function.Advanced(type= FunctionTypeEnum.QUERY)
@Function.fun("sayHello2")
public PetShop sayHello(PetShop shop, String s) {
PamirsSession.getMessageHub().info("Hello:"+shop.getShopName()+",s:"+s);
return shop;
}
@Function(openLevel = FunctionOpenEnum.API)
@Function.Advanced(type= FunctionTypeEnum.QUERY)
public PetShop hello(PetShop shop){
PamirsSession.getMessageHub().info("Hello:"+shop.getShopName());
return shop;
}
}
Step2 为PetShopProxyB新增对应的三个函数
其中PetShopProxyB新增的hello函数,在java中是重载了hello,在代码中new PetShopProxyB()是可以调用父类的sayHello单参方法,也可以调用本类的双参方法。但在oinone的体系中对于PetShopProxyB只有一个可识别的Fucation就是双参的sayHello
package pro.shushi.pamirs.demo.api.proxy;
…… //import
@Model.model(PetShopProxyB.MODEL_MODEL)
@Model.Advanced(type = ModelTypeEnum.PROXY,inherited ={PetShopProxy.MODEL_MODEL,PetShopProxyA.MODEL_MODEL} )
@Model(displayName = "宠物店铺代理模型B",summary="宠物店铺代理模型B")
public class PetShopProxyB extends PetShop {
public static final String MODEL_MODEL="demo.PetShopProxyB";
@Field.one2many
@Field(displayName = "萌猫商品列表")
@Field.Relation(relationFields = {"id"},referenceFields = {"shopId"})
private List<PetCatItem> catItems;
@Function(openLevel = FunctionOpenEnum.API)
@Function.Advanced(type= FunctionTypeEnum.QUERY)
public PetShop sayHello(PetShop shop){
PamirsSession.getMessageHub().info("PetShopProxyB Hello:"+shop.getShopName());
return shop;
}
@Function(name = "sayHello2",openLevel = FunctionOpenEnum.API)
@Function.Advanced(type= FunctionTypeEnum.QUERY)
@Function.fun("sayHello2")
public PetShop sayHello(PetShop shop,String hello){
PamirsSession.getMessageHub().info("PetShopProxyB say:"+hello+","+shop.getShopName());
return shop;
}
@Function(openLevel = FunctionOpenEnum.API)
@Function.Advanced(type= FunctionTypeEnum.QUERY)
public PetShop hello(PetShop shop,String hello){
PamirsSession.getMessageHub().info("PetShopProxyB hello:"+hello+","+shop.getShopName());
return shop;
}
}
Step3 重启看效果
- 查看petShopQuery的sayHello、sayHello2、hello三个函数,结果正常
- 查看petShopProxyBQuery的sayHello、sayHello2、hello三个函数结果都被重载了,而且hello函数传不传参数hello,都是调用的本类的双参函数
query{
petShopQuery{
sayHello(shop:{shopName:"cpc"}){
shopName
}
sayHello2(shop:{shopName:"cpc"},s:"sss"){
shopName
}
hello(shop:{shopName:"cpc"}){
shopName
}
}
petShopProxyBQuery{
sayHello(shop:{shopName:"cpc"}){
shopName
}
sayHello2(shop:{shopName:"cpc"},hello:"sss"){
shopName
}
hello(shop:{shopName:"cpc"},hello:"hello"){
shopName
}
}
}