java 反射模式_java 设计模式——反射机制的应用

Java反射机制是指:在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法和属性;这种动态获取的信息以及动态调用对象的方法的功能称为Java语言的反射机制。

class是一个类,通过类名获取反射机制。

一、

import java.lang.reflect.*;

class A{

public A(){

System.out.println("A()");

}

public A(int m,String n){

System.out.println("A(int m,String n)");

}

public A(int m){}

}

public class test {

public static void main(String[] args) throws Exception {

Class c = Class.forName("A"); //反射机制

Constructor con[]=c.getConstructors();

//定义Constructor类型的con数组,用来承装A类的构造函数

con[0].newInstance(); //顺序与构造函数顺序对应

con[1].newInstance(12,"hai");

}

}

运行结果如下

64cac25d6892ed6b8c2b1a15b0ad6167.png

二、

Constructor con=c.getConstructor(int.class,String.class); 创建指定构造函数对象con:通过参数不同,获取构造函数

con.newInstance(200,"hello"); 调用newInstance()函数新建对象

import java.lang.reflect.*;

class A{

public A(){

System.out.println("A()");

}

public A(int m,String n){

System.out.println(m+":"+n);

}

public A(int m){}

}

public class test {

public static void main(String[] args) throws Exception {

Class c = Class.forName("A"); //反射机制

Constructor con=c.getConstructor(int.class,String.class);

//通过参数指向制定构造函数

con.newInstance(200,"hello");

}

}

结果如下:

6e948c6e293f583660670f5425c67337.png

三、具体应用

1 import java.lang.reflect.*;

2 import java.util.Scanner;

3 interface IShape{

4 void input();

5 double getArea();

6 }

7 class Circle implements IShape{

8 double r;

9 public Circle(){}

10 public void input(){

11 System.out.println("please input r:");

12 Scanner sc = new Scanner(System.in);

13 r = sc.nextDouble();

14 }

15 public double getArea(){

16 return 3.14*r*r;

17 }

18

19 }

20 class Rect implements IShape{

21 double h,w;

22 public Rect(){}

23 public double getArea(){

24 return h*w;

25 }

26 public void input(){

27 System.out.println("please input h,w:");

28 Scanner sc = new Scanner(System.in);

29 h = sc.nextDouble();

30 w = sc.nextDouble();

31

32 }

33 }

34

35 public class test {

36 public static void main(String[] args) throws Exception {

37 /*Circle c = new Circle(); //这样写是没有问题的,但是没有应用反射机制

38 c.input();

39 double area = c.getArea();

40 System.out.println("area="+area);

41 */

42

43 /* Class c = Class.forName("Circle"); //这样写没有问题,也应用了反射机制,但该方法不是最优

44 //因为如果要计算Rect的面积,就得把所有的Circle全都改成Rect,麻烦,所以应用接口

45 Circle obj = (Circle)c.newInstance();

46 obj.input();

47 double area = obj.getArea();

48 System.out.println("area="+area);

49 */

50 Class c = Class.forName("Circle"); //这样只需要改名字就好了

51 IShape obj = (IShape)c.newInstance();

52 obj.input();

53 double area = obj.getArea();

54 System.out.println("area="+area);

55

56

57

58 }

59

60 }

标签:反射,java,area,System,class,println,Circle,设计模式,public

来源: https://www.cnblogs.com/ttting/p/11479139.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值