Posts
Q&A
itemService.saveItem(book) ์ง๋ฌธ ๋๋ฆฝ๋๋ค!
์ง๋ฌธ์ ๋ํด์ ์๊ฐํด๋ณด๊ณ ์ฝ๋๋ฅผ ์์ฑํด๋ดค๋๋ฐ ์ด๋ฐ ๊ฐ๋ ์ด ๋ง์๊น์??.. import java.lang.reflect.Method; public class ParentTest { public ParentTest(String str, int cnt) { this.str = str; this.cnt = cnt; } public String str; public int cnt; public static void main(String[] args) { Child child = new Child("Parent1" , 1 , "Child1" , 1000); ParentTest parentTest = child; //parentTest.childStr = ์ ์บ์คํ ์ ์์์ ๋ฉค๋ฒ์ ์ ๊ทผ๋ถ๊ฐ ๋ณธ์ธ (ParentTest)์๋ง ์ ๊ทผ๊ฐ๋ฅ //entityManager์ ๋งค๊ฐ๋ณ์๋ Object์ EntityTest entityTest = new EntityTest(); entityTest.persist(parentTest); //๋ง์ฝ ๋ด๋ถ ๋ก์ง์ด ์ด๋ ๊ฒ ํ๋ณํ์ ์งํํ๋ค๊ณ ๊ฐ์ ํ๋ฉด // Child convertChild = (Child) testObject; // System.out.println(convertChild.childCnt); // System.out.println(convertChild.childStr); } } class Child extends ParentTest{ public Child(String str, int cnt , String childStr , int childCnt){ super(str,cnt); this.childStr = childStr; this.childCnt = childCnt; } public String childStr; public int childCnt; public String getChildStr() { return childStr; } public int getChildCnt() { return childCnt; } } class EntityTest{ public void persist(Object obj){ Class testClass = null; try{ // ๋ฆฌํ๋ ์ ํ์ฉ Class ์ด๊ธฐํ testClass = Class.forName(obj.getClass().getName()); //obj ํด๋์ค ์บ์คํ testClass.cast(obj); //ํด๋น ํด๋์ค์ ๋ฉ์๋๋ค ์ ๋ถ ๊ฐ์ ธ์ค๊ธฐ Method[] methods = testClass.getDeclaredMethods(); //๋ฐ๋ณต for (Method o : methods) { //getXXX๋ก ์์ํ๋ค๋ฉด if(o.getName().startsWith("get")){ //๋ฉ์๋ ์คํ System.out.println(o.invoke(obj)); } } }catch (Exception e){ } } }
- 0
- 2
- 405
Q&A
๋ค์ด์บ์คํ ์ ๋ํด์ ์ง๋ฌธ์ด ์์ต๋๋ค!
๋ฌด์งํ ์ง๋ฌธ์ ์น์ ํ ๋ต๋ณ ์ ๋ง ๊ฐ์ฌํฉ๋๋ค! ๊ณ์ ์ฐพ์๋ณด๋ ๋ด์ฉ์ด์๋๋ฐ ใ ใ ... ์๋ ์์ ์ฝ๋๋ ์ ๋ง ๊ฐ์ฌํฉ๋๋ค!!
- 2
- 2
- 806