zoukankan      html  css  js  c++  java
  • 14_Xml继承

    【工程截图】

    【Person.java】

    package com.HigginCui;
    
    public class Person {
        private String name;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
    }

    【Studnet.java】

    package com.HigginCui;
    
    public class Student extends Person{
        public void studentSay(){
            System.out.println(this.getName());
        }
    }

    【applicationContext.xml】

    <?xml version= "1.0" encoding ="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
            xmlns:context="http://www.springframework.org/schema/context"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           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">
    
        <bean id="person" class="com.HigginCui.Person">
            <property name="name">
                <value>HigginCui</value>
            </property>
        </bean>
        <!-- parent属性说明了student这个bean继承于person这个bean中的属性的值
         若不加parent="person"这句,testStudent中的输出值为null
    --> <bean id="student" class="com.HigginCui.Student" parent="person"> </bean> </beans>

    【testStudent.java】

    package com.HigginCui.test;
    
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import com.HigginCui.Student;
    
    public class testStudent {
        @Test
        public void test(){
            ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
            Student student=(Student) context.getBean("student");
            student.studentSay();
        }
    }

    【运行结果】

    HigginCui
  • 相关阅读:
    反射学习笔记
    路由机制
    缓存笔记
    进程和线程
    垃圾回收机制
    堆和栈
    值类型和引用类型及参数传递
    招到一个程序员很难吗?
    全面讲解:委托、事件
    struts2入门实例
  • 原文地址:https://www.cnblogs.com/HigginCui/p/5579306.html
Copyright © 2011-2022 走看看