猿问

java反射问题求解

写一个方法:

public void (类,参数1,参数2) {


}

调用这个方法的类是各个不同的类,但是这些类中都有一个公共的属性isTm,想把这个函数定义成公共的。不同的类传进来,可以根据不同的类,来设置属性isTm的值,应该如何设计和实现?

举例如下:


package basic;


public class Test {


    public void updateIsTm(T<? extends Object> pojo) {

        if (null != pojo) {

            // TODO

            //如果传进来的是A a对象,就调用a.setIsTm()方法,修改值。 

            //如果传进来的是B b对象,就调用b.setIsTm()方法,修改值。

        }

    }

}



 //类A和类B有公共的属性isTm

     

    class A {

    

        String a;

        String isTm;

    

        public String getIsTm() {

            return isTm;

        }

    

        public void setIsTm(String isTm) {

            this.isTm = isTm;

        }

    }

    

    class B {

    

        String b;

        String isTm;

    

        public String getIsTm() {

            return isTm;

        }

    

        public void setIsTm(String isTm) {

            this.isTm = isTm;

        }

    }

谢谢~~


波斯汪
浏览 632回答 3
3回答

一只斗牛犬

设计一个接口,让要调用这个函数的类都实现这个接口interface IHaveTm {&nbsp; &nbsp; void setIsTm(String isTm);&nbsp; &nbsp; String getIsTm();}public void <T extends IHaveTm> updateIsTm(T t, ...) {&nbsp; &nbsp; t.setIsTm(...)}补充:如果不修改调用这个函数的类,做无侵入式设计的话,估计只能用反射了public void updateIsTm(Object obj, ...) throws Exception {&nbsp; &nbsp; Method m = obj.getClass().getDeclaredMethod("setIsTm", String.class);&nbsp; &nbsp; if (m != null) {&nbsp; &nbsp; &nbsp; &nbsp; m.invoke(obj, ...);&nbsp; &nbsp; }}这样的话就得靠约定来保证调用的安全性了,不建议这么做

临摹微笑

public class Main {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; setIsTm(new A(), "haha");&nbsp; &nbsp; &nbsp; &nbsp; setIsTm(new B(), "haha again");&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; public static void setIsTm(TMSetable t, String var1) {&nbsp; &nbsp; &nbsp; &nbsp; t.setIsTm(var1);&nbsp; &nbsp; }}interface TMSetable {&nbsp; &nbsp; public void setIsTm(String isTm);}class A implements TMSetable {&nbsp; &nbsp; private String isTm;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; /*&nbsp;&nbsp; &nbsp; &nbsp;* @see io.beansoft.netty.netty4.bootstrap.TMSetable#setIsTm(java.lang.String)&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; @Override&nbsp; &nbsp; public void setIsTm(String isTm) {&nbsp; &nbsp; &nbsp; &nbsp; this.isTm = isTm;&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("A is setting isTm=" + isTm);&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;}class B implements TMSetable {&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; private String isTm;&nbsp; &nbsp; /*&nbsp;&nbsp; &nbsp; &nbsp;* @see io.beansoft.netty.netty4.bootstrap.TMSetable#setIsTm(java.lang.String)&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; @Override&nbsp; &nbsp; public void setIsTm(String isTm) {&nbsp; &nbsp; &nbsp; &nbsp; this.isTm = isTm;&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("B is setting isTm=" + isTm);&nbsp; &nbsp; }}输出结果:A is setting isTm=hahaB is setting isTm=haha again从题目来看,没看出有泛型设计的必要。

qq_花开花谢_0

从设计上来说,设计一个接口或抽象类将isTm这个属性抽象出来比较好。但如果这些类你已经没办法去修改,而他们之间也没有继承或实现共同的父类或接口,那就只能用反射了。但用反射的话,会让代码变得很不堪,出问题也不好找,用得不好属于埋雷行为。
随时随地看视频慕课网APP

相关分类

Java
我要回答