public class Dog extends Animal{
private String myName = "뽀삐";
public String myCity = "서울";
public Dog() {
}
private Dog(String myName) {
this.myName = myName;
}
private void myName(String name){
System.out.println("myName : " + name);
}
private void myCity(String city){
System.out.println("myCity : " + city);
}
private void hello(){
System.out.println("hello~");
}
}
Dog.class
처럼 클래스 정보를 할당할 수 있습니다.getName()
은 클래스의 이름을 리턴합니다.public class Example {
public static void main(String[] args) throws Exception{
Class cls = Dog.class;
System.out.println("Class Name : " + cls.getName());
// Class Name : test.Dog 출력
}
}
public class Example {
public static void main(String[] args) throws Exception{
Class cls = Class.forName("Dog");
System.out.println("Class Name : " + cls.getName());
// Class Name : Dog 출력
}
}
getDeclaredConstructor()
메서드에 아무런 내용을 작성하지 않으면 인자가 없는 기본 생성자를 가져올 수 있습니다.java.lang.NoSuchMethodException
예외를 발생시킵니다.public class Example {
public static void main(String[] args) throws Exception{
Class cls = Class.forName("Dog");
Constructor constructor = cls.getDeclaredConstructor();
System.out.println("Constructor : " + constructor.getName());
// Constructor : Dog 출력
}
}
getDeclaredConstructor(Param)
에 인자를 넣으면 해당 타입과 일치하는 생성자를 찾습니다.public class Example {
public static void main(String[] args) throws Exception{
Class cls = Class.forName("Dog");
Constructor constructor = cls.getDeclaredConstructor(String.class);
System.out.println("Constructor : " + constructor.getName());
// Constructor : Dog 출력
}
}
getDeclaredConstructor()
메서드를 사용하면 클래스의 private
, public
등의 모든 생성자를 리턴합니다.public class Example {
public static void main(String[] args) throws Exception{
Class cls = Class.forName("Dog");
Constructor constructors[] = cls.getDeclaredConstructors();
for (Constructor item : constructors){
System.out.println("Get constructors : " + item);
// Get constructors : public test.Dog()
// Get constructors : public test.Dog(java.lang.String)
}
}
}
public
생성자만 가져오기getConstructor()
메서드를 사용하면 클래스의 public
생성자를 리턴할 수 있습니다.public class Example {
public static void main(String[] args) throws Exception{
Class cls = Class.forName("Dog");
**Constructor constructors[] = cls.getConstructors();**
for (Constructor item : constructors){
System.out.println("Get public constructors : " + item);
// Get public constructors : public Dog()
}
}
}
import java.lang.reflect.Method;
public class Example {
public static void main(String[] args) throws Exception{
Class cls = Class.forName("Dog");
**Method method = cls.getDeclaredMethod("hello", null);**
System.out.println("Method : " + method);
// Method : Method : private void Dog.hello()
}
}
import java.lang.reflect.Method;
public class Example {
public static void main(String[] args) throws Exception{
Class cls = Class.forName("Dog");
**Method method = cls.getDeclaredMethod("myName", String.class);**
System.out.println("Method : " + method);
// Method : private void Dog.myName(java.lang.String)
}
}
import java.lang.reflect.Method;
public class Example {
public static void main(String[] args) throws Exception{
Class cls = Class.forName("Dog");
**Method methods[] = cls.getDeclaredMethods();**
for (Method item : methods) {
System.out.println("Method : " + item);
// Method : private void test.Dog.hello()
// Method : private void test.Dog.myName(java.lang.String)
// Method : private void test.Dog.myCity(java.lang.String)
}
}
}
public
메서드만 가져오는 방법import java.lang.reflect.Method;
public class Example {
public static void main(String[] args) throws Exception{
Class cls = Class.forName("Dog");
**Method methods[] = cls.getMethods();**
for (Method item : methods) {
System.out.println("Method : " + item);
}
}
}
public class Example {
public static void main(String[] args) throws Exception{
Class cls = Class.forName("Dog");
Field field = cls.getDeclaredField("myName");
System.out.println(field);
// private java.lang.String Dog.myName
}
}
public class Example {
public static void main(String[] args) throws Exception{
Class cls = Class.forName("Dog");
**Field fields[] = cls.getDeclaredFields();**
for (Field item : fields) {
System.out.println(item);
// private java.lang.String Dog.myName
// public java.lang.String Dog.myCity
}
}
}
참고 자료