Ⅷ.spring的点点滴滴--抽象对象和子对象
承接上文
抽象对象和子对象
.net篇(环境为vs2012+Spring.Core.dll v1.31)
public class parent { public string Name { get; set; } public int Age { get;set; } } public class chlid { public string Name { get; set; } public int Age { get; set; } }
<object id="p" type="SpringBase.parent,SpringBase"> <property name="Name" value="cnljli-p0" /> <property name="Age" value="1" /> </object> <object id="c" parent="p" type="SpringBase.chlid,SpringBase"> <property name="Age" value="2" /> </object> <object id="p1"> <property name="Name" value="cnljli-p1" /> <property name="Age" value="2" /> </object> <object id="c1" parent="p1" type="SpringBase.chlid,SpringBase"> <property name="Age" value="3" /> </object> <object id="p2" type="SpringBase.chlid,SpringBase" abstract="true"> <property name="Name" value="cnljli-p2" /> <property name="Age" value="3" /> </object> <object id="c2" parent="p2" type="SpringBase.chlid,SpringBase"> <property name="Age" value="4" /> </object>
- 可以在不声明一个类的情况下直接写一个Object来做模板
java篇(环境为Maven+Jdk1.7+IntelliJ IDEA 12.1.4)
package springdemo; public class Parent { private String name; private Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } } class Chlid{ private String name; private Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
<bean id="p" class="springdemo.Parent"> <property name="name" value="cnljli-p0" /> <property name="age" value="1" /> </bean> <bean id="c" parent="p" class="springdemo.Chlid"> <property name="age" value="2" /> </bean> <bean id="p1" class="springdemo.Parent" abstract="true"> <property name="name" value="cnljli-p2" /> <property name="age" value="3" /> </bean> <bean id="c1" parent="p1" class="springdemo.Chlid"> <property name="age" value="4" /> </bean>
java和Csharp的共同点
- 用其他类做父类,子类不一定继承父类,但是必须要包含父类的属性
- 可以用自己本身做为模板,当设置参数abstract为true的时候这个对象通过id调用的 时候返回会报错
- 子类会覆盖父类中某些属性的方法,不过不会覆盖属性为 depends-on(依赖)、dependency-check(依赖检查)、autowire(自动装配模式)、 singleton(单例)和lazy-init(延迟加载)
作者:cnljli
欢迎转载,但还请尊重劳动果实,保留此段声明并注明原文链接。