๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Java

[Java-๊ธฐ์ดˆ] Class ํด๋ž˜์Šค

by ์ฝ”๋”ฉํ•˜๋Š” ๋ถ•์–ด 2021. 2. 21.
๋ฐ˜์‘ํ˜•

โ–ถ Class ํด๋ž˜์Šค

๋ชจ๋ฅด๋Š” ํด๋ž˜์Šค์˜ ์ •๋ณด๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ์— ํด๋ž˜์Šค ์ •๋ณด๋ฅผ ์ง์ ‘ ์ฐพ์•„์•ผํ•˜๋Š”๋ฐ ์ด๋•Œ Class ํด๋ž˜์Šค๋ฅผ ํ™œ์šฉํ•œ๋‹ค.

Class ํด๋ž˜์Šค๋ฅผ ์„ ์–ธํ•˜๊ณ  ํด๋ž˜์Šค ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ธ ๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค.

โ€‹

1. Object ํด๋ž˜์Šค์˜ getClass( ) ๋ฉ”์„œ๋“œ ์‚ฌ์šฉํ•˜๊ธฐ

2. ํด๋ž˜์Šค ํŒŒ์ผ ์ด๋ฆ„์„ Class ๋ณ€์ˆ˜์— ์ง์ ‘ ๋Œ€์ž…ํ•˜๊ธฐ

3. Class.forName("ํด๋ž˜์Šค ์ด๋ฆ„") ๋ฉ”์„œ๋“œ ์‚ฌ์šฉํ•˜๊ธฐ

-1๋ฒˆ์˜ ๊ฒฝ์šฐ Object์— ์„ ์–ธํ•œ getClass( ) ๋ฉ”์„œ๋“œ๋Š” ๋ชจ๋“  ํด๋ž˜์Šค๊ฐ€ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉ”์„œ๋“œ์ด๋‹ค. ์ด ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์ด๋ฏธ ์ƒ์„ฑ๋œ ์ธ์Šคํ„ด์Šค๊ฐ€ ์žˆ์–ด์•ผ ํ•œ๋‹ค. 2, 3๋ฒˆ์˜ ๊ฒฝ์šฐ์—๋Š” ์ปดํŒŒ์ผ๋œ ํด๋ž˜์Šค ํŒŒ์ผ์ด ์žˆ๋‹ค๋ฉด ํด๋ž˜์Šค ์ด๋ฆ„๋งŒ์œผ๋กœ Class ํด๋ž˜์Šค๋ฅผ ๋ฐ˜ํ™˜๋ฐ›๋Š”๋‹ค.

 

package classex;

public class Person {
	private String name;
	private int age;

	public Person() {  // ๋””ํดํŠธ ์ƒ์„ฑ์ž
	}

	public Person(String name) {  // ์ด๋ฆ„๋งŒ ์ž…๋ ฅ๋ฐ›๋Š” ์ƒ์„ฑ์ž
		this.name = name;
	}

	public Person(String name, int age) {  // ์ด๋ฆ„๊ณผ ๋‚˜์ด๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š” ์ƒ์„ฑ์ž
		this.name = name;
		this.age = 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;
	}
    
}

 

package classex;

public class ClassTest {
	public static void main(String[] args) throws ClassNotFoundException {  // forName() ๋ฉ”์„œ๋“œ์—์„œ ๋ฐœ์ƒํ•˜๋Š” ์˜ˆ์™ธ๋ฅผ ์ฒ˜๋ฆฌํ•จ. ์ด๋ฆ„๊ณผ ์ผ์น˜ํ•˜๋Š” ํด๋ž˜์Šค๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ ClassNotFoundException ๋ฐœ์ƒ
		Person person = new Person();
		Class pClass1 = person.getClass();  // Object์˜ getClass() ๋ฉ”์„œ๋“œ ์‚ฌ์šฉํ•˜๊ธฐ
		System.out.println(pClass1.getName());

		Class pClass2 = Person.class;  // ์ง์ ‘ class ํŒŒ์ผ ๋Œ€์ž…ํ•˜๊ธฐ
		System.out.println(pClass2.getName());

		Class pClass3 = Class.forName("classex.Person");  // ํด๋ž˜์Šค ์ด๋ฆ„ ๊ฐ€์ ธ์˜ค๊ธฐ
		System.out.println(pClass3.getName());
	}
}

<์‹คํ–‰ ๊ฒฐ๊ณผ>

โ€‹

 

 

*Class ํด๋ž˜์Šค๋ฅผ ํ™œ์šฉํ•ด ํด๋ž˜์Šค ์ •๋ณด ์•Œ์•„๋ณด๊ธฐ

ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ํ•˜๋‹ค ๋ณด๋ฉด ๋‚ด๊ฐ€ ์‚ฌ์šฉํ•  ํด๋ž˜์Šค์˜ ์ž๋ฃŒํ˜•์„ ๋ชจ๋ฅด๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋‚ด ์ปดํ“จํ„ฐ์— ์ €์žฅ๋˜์–ด ์žˆ์ง€ ์•Š์€ ๊ฐ์ฒด๋ฅผ ๋ฉ”๋ชจ๋ฆฌ์— ๋กœ๋“œํ•˜๊ณ  ์ƒ์„ฑํ•˜๋Š” ๊ฒฝ์šฐ ๊ทธ ๊ฐ์ฒด์˜ ์ •๋ณด๋ฅผ ์•Œ ์ˆ˜ ์—†๋‹ค. ์ด ๋•Œ Class ํด๋ž˜์Šค๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค๋ฉด ํ•ด๋‹น ํด๋ž˜์Šค ์ •๋ณด, ์ฆ‰ ์ƒ์„ฑ์ž·๋ฉ”์„œ๋“œ·๋ฉค๋ฒ„ ๋ณ€์ˆ˜ ์ •๋ณด๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์‚ฌ์šฉํ•˜๋ ค๋Š” ํด๋ž˜์Šค์˜ ์ž๋ฃŒํ˜•์„ ๋ชจ๋ฅด๋Š” ์ƒํƒœ์—์„œ Class ํด๋ž˜์Šค๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๊ทธ ํด๋ž˜์Šค์˜ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ค๊ณ , ์ด ์ •๋ณด๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ฑฐ๋‚˜ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ์‹์„ '๋ฆฌํ”Œ๋ ‰์…˜'์ด๋ผ๊ณ  ํ•œ๋‹ค.

โ€‹

package classex;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Field;

public class StringClassTest {
	public static void main(String[] args) throws ClassNotFoundException {
		Class strClass = Class.forName("java.lang.String");  // ํด๋ž˜์Šค ์ด๋ฆ„ ๊ฐ€์ ธ์˜ค๊ธฐ

		Constructor[] cons = strClass.getConstructors();  // ๋ชจ๋“  ์ƒ์„ฑ์ž ๊ฐ€์ ธ์˜ค๊ธฐ
		for (Constructor c : cons) {
			System.out.println(c);
		}

		System.out.println();
		Field[] fields = strClass.getFields();  // ๋ชจ๋“  ๋ฉค๋ฒ„๋ณ€์ˆ˜ ๊ฐ€์ ธ์˜ค๊ธฐ
		for (Field f : fields) {
			System.out.println(f);
		}
		System.out.println();
		Method[] methods = strClass.getMethods();  // ๋ชจ๋“  ๋ฉ”์„œ๋“œ ๊ฐ€์ ธ์˜ค๊ธฐ
		for (Method m : methods) {
			System.out.println(m);
		}
        
	}
}

<์‹คํ–‰ ๊ฒฐ๊ณผ>

โ€‹-String ํด๋ž˜์Šค์˜ ์ •๋ณด๋ฅผ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

โ€‹

 

 

๋‚˜ ํ˜ผ์ž ์ฝ”๋”ฉ) Person ํด๋ž˜์Šค ์ •๋ณด ๊ฐ€์ ธ์˜ค๊ธฐ

379์ชฝ์—์„œ ๋งŒ๋“  Person ํด๋ž˜์Šค์˜ Class ํด๋ž˜์Šค๋ฅผ ๊ฐ€์ ธ์™€์„œ getConstructor( ), getMethods( ), getFields( ) ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ์ƒ์„ฑ์ž, ๋ฉ”์„œ๋“œ, ๋ฉค๋ฒ„ ๋ณ€์ˆ˜(ํ•„๋“œ) ์ •๋ณด๋ฅผ ์ถœ๋ ฅํ•ด ๋ณด๋ผ.

package classex;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class PersonClassTest {
	public static void main(String[] args) {
		Class strClass = Person.class;

		Constructor[] cons = strClass.getConstructors();
		for (Constructor c : cons) {
			System.out.println(c);
		}

		System.out.println();
		Field[] fields = strClass.getFields();
		for (Field f : fields) {
			System.err.println(f);
		}
		System.out.println();
		Method[] methods = strClass.getMethods();
		for (Method m : methods) {
			System.out.println(m);
		}
        
	}
}

<์‹คํ–‰ ๊ฒฐ๊ณผ>

 

 

 

*newInstance( )๋ฅผ ์‚ฌ์šฉํ•ด ํด๋ž˜์Šค ์ƒ์„ฑํ•˜๊ธฐ

newInstance( ) ๋ฉ”์„œ๋“œ๋Š” ํ•ญ์ƒ Object๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฏ€๋กœ ์ƒ์„ฑ๋œ ๊ฐ์ฒดํ˜•์œผ๋กœ ํ˜• ๋ณ€ํ™˜ํ•ด์•ผ ํ•œ๋‹ค.

package classex;

public class NewInstanceTest {
	public static void main(String[] args)
			throws ClassNotFoundException, InstantiationException, IllegalAccessException {
		Person person1 = new Person();  // ์ƒ์„ฑ์ž๋กœ ์ƒ์„ฑํ•˜๊ธฐ
		System.out.println(person1);

		Class pClass = Class.forName("classex.Person");
		Person person 2 = (Person) pClass.newInstance();  // Class ํด๋ž˜์Šค์˜ newInstance() ๋ฉ”์„œ๋“œ๋กœ ์ƒ์„ฑํ•˜๊ธฐ
		System.out.println(person2);
	}
}

 

๋”๋ณด๊ธฐ

<์ฑ… ๋‚ด์šฉ์„ ์„ ์ƒ๋‹˜์ด ์ˆ˜์ •ํ•˜์‹  ๊ฒƒ>

package classex;

public class NewInstanceTest {
	public static void main(String[] args)
			throws ClassNotFoundException, InstantiationException, IllegalAccessException {
		Person person1 = new Person();
		System.out.println(person1);

		Class pClass = Class.forName("classex.Person");
		Object obj = pClass.newInstance();
		Person person2 = null;
		if (obj instanceof Person) {
			person2 = (Person) obj;
		}
		System.out.println(person2);
	}
}

-๋‹ค์šด์บ์ŠคํŒ… ํ•ด์ฃผ๋ฉด ์ข€ ๋” ์ข‹๋‹ค.

 

<์‹คํ–‰ ๊ฒฐ๊ณผ>

โ€‹

 

 

 

์—ฐ์Šต๋ฌธ์ œ Q4) ๋‹ค์Œ ์ฝ”๋“œ์˜ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๊ฐ€ '์ง„๋—๊ฐœ ๋ฉ๋ฉ์ด' ๊ฐ€ ๋˜๋„๋ก MyDog ํด๋ž˜์Šค๋ฅผ ์ˆ˜์ •ํ•˜๋ผ.

์ด๊ฑธ ๋ฐ”ํƒ•์œผ๋กœ ์ˆ˜์ •ํ•  ๊ฒƒ

 

<๋‚ด๊ฐ€ ํ•œ ๊ฒƒ>

package classex;

class MyDog {
	String name;
	String type;

	public MyDog(String name, String type) {
		this.name = name;
		this.type = type;
	}

	public String toString() {
		return name + ", " + type;
	}
}

public class Q4 {
	public static void main(String[] args) {
		MyDog dog = new MyDog("๋ฉ๋ฉ์ด", "์ง„๋—๊ฐœ");
		System.out.println(dog);
	}
}

<์‹คํ–‰ ๊ฒฐ๊ณผ>

โ€‹

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€