标签:spring
一:案例截图
二:基本代码
Student.java
<span style="font-size:14px;">package com.cloud.inherit; public class Student { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } </span>Gradate.java
<span style="font-size:14px;">package com.cloud.inherit; public class Gradate extends Student{ private String degree; public String getDegree() { return degree; } public void setDegree(String degree) { this.degree = degree; } } </span>
三:配置代码
<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- 配置一个学生对象 --> <bean id="student" class="com.cloud.inherit.Student"> <property name="name" value="Spring"/> <property name="age" value="22"/> </bean> <!-- 配置Grdate对象 --> <bean id="gradate" parent="student" class="com.cloud.inherit.Gradate"> <property name="name" value="李四"/> <property name="degree" value="学士"/> </bean> </beans></span>
<span style="font-size:14px;">package com.cloud.inherit; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { ApplicationContext ac=new ClassPathXmlApplicationContext("com/cloud/inherit/beans.xml"); Gradate ge=(Gradate) ac.getBean("gradate"); System.out.println(ge.getName()+" "+ge.getAge()+" "+ge.getDegree()); } }</span>
五:测试结果
李四 22 学士
版权声明:博主原创文章,转载请说明出处。http://blog.csdn.net/dzy21
标签:spring
原文地址:http://blog.csdn.net/dzy21/article/details/48052849