[企业管理] [零基础学JAVA]Java SE应用部分-36.反射机制与工厂设计模式

[复制链接]
发表于 2022-9-23 11:23:38
本季方案
Java的反射机制
工厂模式综合解说
1、啥叫反射
Java.lang.reflect包下
正常状况下我们可以通过类实例化一个目标,那么通过反射实际上就可以通过一个目标得到此类完整的包.类称号。
packageorg.michael;
classPerson{
privateStringname;
privateintage;
publicvoidsetName(Stringname){
this.name=name;
}
publicvoidsetAge(intage){
this.age=age;
}
publicStringgetName(){
returnthis.name;
}
publicintgetAge(){
returnthis.age;
}
}
publicclassDemo01{
publicstaticvoidmain(Stringargs){
Personp=newPerson();
//假定如今不认识p是那个类的目标,则可以通过反射机制找到
ClaS/SC=null;
c=p.getClass();
System.out.println(c.getName());
}
}
看下效果:
除了可以找到目标地点的包.类称号,实际上也可以把所有的方法称号列出来。
packageorg.michael;
importjava.lang.reflect.
;
classPerson{
privateStringname;
privateintage;
publicvoidsetName(Stringname){
this.name=name;
}
publicvoidsetAge(intage){
this.age=age;
}
publicStringgetName(){
returnthis.name;
}
publicintgetAge(){
returnthis.age;
}
}
publicclassDemo02{
publicstaticvoidmain(Stringargs){
Personp=newPerson();
//假定如今不认识p是那个类的目标,则可以通过反射机制找到
ClaS/SC=null;
c=p.getClass();
Methodm=c.getMethods();
for(inti=0;ilt;m.length;i++){
System.out.println(m);
}
}
}
2、研究Class类
Class类的结构方法被私有化了,外部无法直接看见,所以其内部必定有一个方法可以获得Class实例。
publicstaticClasslt;?gt;forName(StringclassName)throwsClassNotFoundException
此方法可以回来Class类的实例,此方法接收一个完整的包.类称号。
通过newInstance方法,可以将传入的完整的字符串(包.类称号)实例化。
packageorg.michael;
importjava.lang.reflect.
;
classPerson{
privateStringname;
privateintage;
publicvoidsetName(Stringname){
this.name=name;
}
publicvoidsetAge(intage){
this.age=age;
}
publicStringgetName(){
returnthis.name;
}
publicintgetAge(){
returnthis.age;
}
}
publicclassDemo03{
publicstaticvoidmain(Stringargs){
Personp=null;
ClaS/SC=null;
try{
c=Class.forName(org.michael.Person);
}catch(Exceptione){}
try{
p=(Person)c.newInstance();
}catch(Exceptione){}
//上面两行代码也可以使用下面一行代码替代哈~
//p=(Person)Class.forName(org.michael.Person).newInstance();
p.setName(Michael);
p.setAge(30);
System.out.println(p.getName()+---gt;+p.getAge());
}
}
假如要使用以上的代码去实例化一个目标,则必须有一个前途条件:在目标地点的类中必须有一个无参结构方法,假如没有此无参结构,则肯定会呈现错误。
packageorg.michael;
importjava.lang.reflect.
;
classPerson{
privateStringname;
privateintage;
publicPerson(Stringname,intage){
this.name=name;
this.age=age;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicvoidsetAge(intage){
this.age=age;
}
publicStringgetName(){
returnthis.name;
}
publicintgetAge(){
returnthis.age;
}
}
publicclassDemo04{
publicstaticvoidmain(Stringargs){
Personp=null;
ClaS/SC=null;
try{
c=Class.forName(org.michael.Person);
p=(Person)c.newInstance();
}catch(Exceptione){
System.out.println(e);
}
System.out.println(p.getName()+---gt;+p.getAge());
}
}
在此刻假如想持续通过此操作为目标进行实例化,则可以通过结构方法类(Constructor)终结。
packageorg.michael;
importjava.lang.reflect.
;
classPerson{
privateStringname;
privateintage;
publicPerson(Stringname,intage){
this.name=name;
this.age=age;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicvoidsetAge(intage){
this.age=age;
}
public

(Program this season
Java's reflection mechanism
Comprehensive explanation of factory mode
1. What is reflection
Under the Java.lang.reflect package
Under normal circumstances, we can instantiate a target through a class, then through reflection, we can actually get such a complete package through a target. Class name.
packageorg.michael;
classPerson{
privateStringname;
privateintage;
publicvoidsetName(Stringname){
this.name=name;
}
publicvoidsetAge(intage){
this.age=age;
}
publicStringgetName(){
returnthis.name;
}
publicintgetAge(){
returnthis.age;
}
}
publicclassDemo01{
publicstaticvoidmain(Stringargs){
Personp=newPerson();
//Assuming that you don't know that p is the target of that class, you can find it through the reflection mechanism
ClaS/SC=null;
c=p.getClass();
System.out.println(c.getName());
}
}
Take a look at the effect:
In addition to the package class name that can find the target location, it is also possible to actually list all the method names.
packageorg.michael;
importjava.lang.reflect.
;
classPerson{
privateStringname;
privateintage;
publicvoidsetName(Stringname){
this.name=name;
}
publicvoidsetAge(intage){
this.age=age;
}
publicStringgetName(){
returnthis.name;
}
publicintgetAge(){
returnthis.age;
}
}
publicclassDemo02{
publicstaticvoidmain(Stringargs){
Personp=newPerson();
//Assuming that you don't know that p is the target of that class, you can find it through the reflection mechanism
ClaS/SC=null;
c=p.getClass();
Methodm=c.getMethods();
for(inti=0;ilt;m.length;i  ){
System.out.println(m);
}
}
}
2. Research Class
The structure method of the Class class is private and cannot be seen directly from the outside, so there must be a method inside it to obtain the Class instance.
publicstaticClasslt;?gt;forName(StringclassName)throwsClassNotFoundException
This method can return an instance of the Class class, this method receives a complete package. Class name.
Through the newInstance method, the incoming complete string (package.class name) can be instantiated.
packageorg.michael;
importjava.lang.reflect.
;
classPerson{
privateStringname;
privateintage;
publicvoidsetName(Stringname){
this.name=name;
}
publicvoidsetAge(intage){
this.age=age;
}
publicStringgetName(){
returnthis.name;
}
publicintgetAge(){
returnthis.age;
}
}
publicclassDemo03{
publicstaticvoidmain(Stringargs){
Personp=null;
ClaS/SC=null;
try{
c=Class.forName(org.michael.Person);
}catch(Exceptione){}
try{
p=(Person)c.newInstance();
}catch(Exceptione){}
//The above two lines of code can also be replaced by the following line of code~
//p=(Person)Class.forName(org.michael.Person).newInstance();
p.setName(Michael);
p.setAge(30);
System.out.println(p.getName() ---gt; p.getAge());
}
}
If you want to use the above code to instantiate a target, there must be a future condition: there must be a parameterless structure method in the class of the target site, if there is no such parameterless structure, an error will definitely appear.
packageorg.michael;
importjava.lang.reflect.
;
classPerson{
privateStringname;
privateintage;
publicPerson(Stringname,intage){
this.name=name;
this.age=age;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicvoidsetAge(intage){
this.age=age;
}
publicStringgetName(){
returnthis.name;
}
publicintgetAge(){
returnthis.age;
}
}
publicclassDemo04{
publicstaticvoidmain(Stringargs){
Personp=null;
ClaS/SC=null;
try{
c=Class.forName(org.michael.Person);
p=(Person)c.newInstance();
}catch(Exceptione){
System.out.println(e);
}
System.out.println(p.getName() ---gt; p.getAge());
}
}
At this moment, if you want to continue to instantiate through this operation as the target, you can end it through the structure method class (Constructor).
packageorg.michael;
importjava.lang.reflect.
;
classPerson{
privateStringname;
privateintage;
publicPerson(Stringname,intage){
this.name=name;
this.age=age;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicvoidsetAge(intage){
this.age=age;
}
public)

[下载]11233808869.rar




上一篇:[零基础学JAVA]Java SE应用部分-35.JAVA类集之四
下一篇:[零基础学JAVA]Java SE实践开发-37.MIS信息管理系统实践开发[文

使用道具 举报

Archiver|手机版|小黑屋|吾爱开源 |网站地图

Copyright 2011 - 2012 Lnqq.NET.All Rights Reserved( ICP备案粤ICP备14042591号-1粤ICP14042591号 )

关于本站 - 版权申明 - 侵删联系 - Ln Studio! - 广告联系

本站资源来自互联网,仅供用户测试使用,相关版权归原作者所有

快速回复 返回顶部 返回列表