1. 반사는 무엇입니까?
클래스에는 멤버 변수, 메소드, 생성자 등과 같은 여러 구성 요소가 있습니다.
반사는 클래스를로드하고 클래스의 다양한 구성 요소를 해부하는 것입니다.
2. 클래스로드
Java에는 특정 클래스의 바이트 코드를 나타내는 클래스 클래스가 있습니다.
클래스 클래스는 특정 클래스의 바이트 코드를 나타내므로 특정 클래스의로드를 제공해야합니다.
바이트 코드 : Forname ()은 특정 클래스의 바이트 코드를로드하는 데 사용됩니다.
클래스 객체를 사용하여 메모리에 캡슐화하십시오.
클래스 객체를 얻는 또 다른 두 가지 방법 :
클래스 이름 .class
Object.getClass ()
먼저 간단한 사람 수업을 만듭니다
코드 사본은 다음과 같습니다.
공개 클래스 반사 분해 {
public static void main (String args [])은 예외를 던집니다
{// 1.
클래스 Clazz = class.forname ( "dsa.person");
// 2.
클래스 clazz1 = new Person (). getClass ();
// 3.
classclazz2 = person.class;
}
}
3. 반사 구성 방법
개인 수업 :
코드 사본은 다음과 같습니다.
/**
*건축 방법
*/
공개인 () {
System.out.println ( "null");
}
공개 (StringName) {
System.out.println (이름);
}
공개 (StringName, intpwd) {
System.out.println (name+"+"+pwd);
}
민간인 (ListList) {
System.out.println ( "목록");
}
테스트 클래스에서 :
코드 사본은 다음과 같습니다.
// 반사 공개 공개 ()
@시험
publicVoidTest1 () ThrowSexection {
classclazz = class.forname ( "rflectordemo.person");
constructorcr = clazz.getConstructor (null); // 생성자 객체를 가져옵니다
personp = (person) cr.newinstance (null); // 생성자를 통해 객체를 인스턴스화합니다
//system.out.println(p.name);
}
// 반사 공개 (StringName)
@시험
publicVoidTest2 () ThrowSexection {
classclazz = class.forname ( "rflectordemo.person");
constructorcr = clazz.getConstructor (String.class);
personp = (person) cr.newinstance ( "haha");
System.out.println (p.name);
}
// 반사 공개 (StringName, intpwd)
@시험
publicVoidTest3 () ThrowSexection {
classclazz = class.forname ( "rflectordemo.person");
constructorcr = clazz.getConstructor (String.class, int.class);
personp = (person) cr.newinstance ( "haha", 1);
//system.out.println(p.name);
}
// 반사 공개 (ListList)
@시험
publicVoidTest4 () ThrowSexection {
classclazz = class.forname ( "rflectordemo.person");
constructorcr = clazz.getDeclaredConstructor (list.class);
cr.setAccessible (true); // 무차별 력 크래킹
personp = (person) cr.newinstance (newArrayList ());
System.out.println (p.name);
}
// 객체를 생성하는 또 다른 방법은 매개 변수없이 메소드를 구성하는 데만 적용됩니다.
@시험
publicVoidTest5 () ThrowSexection {
classclazz = class.forname ( "rflectordemo.person");
personp = (person) clazz.newinstance ();
System.out.println (p.name);
}
시공 방법이 비공개 일 때, 우리는 균열을 무너 뜨릴 것입니다! ! !
4. 기존의 반사 방법
개인 수업 :
코드 사본은 다음과 같습니다.
/**
*방법
*/
publicvoidjf ()
{
}
publicvoidjf (StringName, intpwd)
{
}
publicclass [] jf (StringName, int [] pwd)
{
returnNewClass [] {string.class, int.class};
}
privatevoidjf (inputstreamin)
{
System.out.println (in);
}
publicstaticvoidjf (intnum)
{
System.out.println (num);
}
PublicStaticVoidMain (StringArgs [])
{
System.out.println ( "Main !!!");
}
테스트 클래스에서 :
코드 사본은 다음과 같습니다.
/**
*반사 방법
*
*@Authortanlvxu
*
*/
publicclassdemo2 {
// 반사 클래스 방법 : publicVoidjf ()
@시험
publicVoidTest1 () ThrowSexection {
personp = newperson ();
classclazz = class.forname ( "rflectordemo.person");
MethodMethod = clazz.getMethod ( "jf", null);
Method.invoke (p, null);
}
// 반사 클래스 방법 : publicVoidjf (StringName, intpwd)
@시험
publicVoidTest2 () ThrowSexection {
personp = newperson ();
classclazz = class.forname ( "rflectordemo.person");
MethodMethod = clazz.getMethod ( "jf", String.class, int.class);
Method.invoke (P, "DSA", 30);
}
// 반사 클래스 방법 : publicclass [] jf (StringName, int [] pwd)
@시험
publicVoidTest3 () ThrowSexection {
personp = newperson ();
classclazz = class.forname ( "rflectordemo.person");
MethodMethod = clazz.getMethod ( "jf", String.class, int []. class);
classcs [] = (class []) method.invoke (p, "aads", newint [] {1,2,3});
System.out.println (CS [0]);
}
// 반사 클래스 방법 : privateVoidjf (inputStreamin)
@시험
publicVoidTest4 () ThrowSexection {
personp = newperson ();
classclazz = class.forname ( "rflectordemo.person");
MethodMethod = clazz.getDeclaredMethod ( "jf", inputStream.class);
method.setAccessible (true);
method.invoke (p, newfileInputStream ( "d : //qqclient.txt"));
}
// 반사 클래스 방법 : publicstaticVoidjf (intnum)
@시험
publicVoidTest5 () ThrowSexection {
classclazz = class.forname ( "rflectordemo.person");
MethodMethod = clazz.getMethod ( "jf", int.class);
Method.invoke (Null, 30);
}
// 반사 클래스 방법 : publicstaticVoidm (intnum)
@시험
publicVoidTest6 () ThrowSexection {
classclazz = class.forname ( "rflectordemo.person");
MethodMethod = clazz.getMethod ( "main", String []. class);
//method.invoke(null ,(Object)newstring] {"d","das "});
method.invoke (null, newObject [] {NewString [] { "ds", "das"}});
}
5. 반사장
개인 수업 :
코드 사본은 다음과 같습니다.
/**
*필드
*/
publictringname = "swr";
privateintpassword = 45;
privatestaticintage = 35;
테스트 클래스에서 :
자바 코드
/**
*반사 클래스 필드
*@Authortanlvxu
*
*/
publicclassdemo3 {
/**
*반사 필드 대중 문자열 = "SWR";
*@ThrowSexception
*/
@시험
publicVoidTest1 () ThrowSexection
{
personp = newperson ();
classclazz = class.forname ( "rflectordemo.person");
fieldf = clazz.getfield ( "이름");
// 필드의 값을 얻습니다
ObjectValue = f.get (p);
// 필드 유형을 가져옵니다
classtype = f.gettype ();
if (type.equals (string.class)) {
StringName = (string) f.get (p);
System.out.println (이름);
}
// 필드의 값을 설정합니다
F. 세트 (p, "dfafa");
System.out.println (p.name);
}
/**
*반사 필드 PrivateIntPassword;
*@ThrowSexception
*/
@시험
PublicVoidTest2 () ThrowSexception
{
personp = newperson ();
classclazz = class.forname ( "rflectordemo.person");
fieldf = clazz.getDeclaredfield ( "비밀번호");
f.setAccessible (true);
F. 세트 (p, 36);
System.out.println (f.get (p));
}
/**
*반사 필드 privatestaticIntage = 35;
*@ThrowSexception
*/
@시험
PublicVoidTest3 () ThrowSexection
{
classclazz = class.forname ( "rflectordemo.person");
fieldf = clazz.getDeclaredfield ( "Age");
f.setAccessible (true);
F. 세트 (NULL, 24);
System.out.println (f.get (null));
}
실제로, 반사 구성 방법이나 필드에 관계없이, 우리는 그것으로부터 하나씩 배울 수 있습니다!