- 다음을 프로그래밍 하시오.(금일 Pencil 소스코드 참고)
- interface Shape 작성
- 클래스 Rectangle ,Triangle ,Circle 작성
- 스프링 생성후 Bean 생성시 위의 3가지 도형 설정에 따른 넓이가 나오도록 하시오.
ctx = new GenericXmlApplicationContext("classpath:appCTX6.xml"); Shape pencil = (Shape) ctx.getBean("Shape"); Shape.getArea();
- 아래의 메모리를 그리시오.(칠판 참고, xml은 수업시 했던 내용 참고 )
ctx = new GenericXmlApplicationContext("classpath:appCTX4.xml","classpath:appCTX5.xml"); Student student1 = (Student) ctx.getBean("student1"); System.out.println(student1.getName()); System.out.println(student1.getHobbys()); StudentInfo studentInfo = (StudentInfo) ctx.getBean("studentInfo1"); Student student2 = studentInfo.getStudent(); if( student1 == student2) { System.out.println(" 같습니다. "); } Student student3 =(Student) ctx.getBean("student2"); if( student2 == student3) { System.out.println(" 같습니다. "); }else { System.out.println("안 같습니다. "); }
appCTX6.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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.xsd">
<bean id="Shape" class="edu.sejong.ex.Shape"></bean>
<!-- edu.sejong.ex.Rectangle -->
<!-- edu.sejong.ex.Triangle -->
<!-- edu.sejong.ex.Circle -->
</beans>
Shape.java
package edu.sejong.ex;
public interface Shape{
public void getArea();
}
Rectangle.java
package edu.sejong.ex;
public class Rectangle implements Shape {
private double width = 6;
private double height = 10;
public void getArea() {
System.out.println("사각형의 넓이는 " + (width*height) + "입니다.");
}
}
Triangle.java
package edu.sejong.ex;
public class Triangle implements Shape {
private double width = 6;
private double height = 10;
public void getArea() {
System.out.println("삼각형의 넓이는 " + (width*height/2) + "입니다.");
}
}
Circle.java
package edu.sejong.ex;
public class Circle implements Shape {
private double radius = 10;
public void getArea() {
System.out.println("원의 넓이는 " + (Math.PI * Math.pow(radius,2)) + "입니다.");
}
}
