spring - java反射问题求解
PHP中文网
PHP中文网 2017-04-18 09:22:49
0
3
222

写一个方法:
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;
        }
    }

谢谢~~

PHP中文网
PHP中文网

认证高级PHP讲师

reply all(3)
巴扎黑

Design an interface so that all classes that want to call this function implement this interface

interface IHaveTm {
    void setIsTm(String isTm);
    String getIsTm();
}

public void <T extends IHaveTm> updateIsTm(T t, ...) {
    t.setIsTm(...)
}

Supplement:
If you don’t modify the class that calls this function and do a non-intrusive design, you may have to use reflection

public void updateIsTm(Object obj, ...) throws Exception {
    Method m = obj.getClass().getDeclaredMethod("setIsTm", String.class);
    if (m != null) {
        m.invoke(obj, ...);
    }
}

In this case, you have to rely on the agreement to ensure the security of the call. This is not recommended

刘奇
public class Main {

    public static void main(String[] args) {
        setIsTm(new A(), "haha");
        setIsTm(new B(), "haha again");
    }
    
    public static void setIsTm(TMSetable t, String var1) {
        t.setIsTm(var1);
    }
}

interface TMSetable {
    public void setIsTm(String isTm);
}

class A implements TMSetable {

    private String isTm;
    
    /* 
     * @see io.beansoft.netty.netty4.bootstrap.TMSetable#setIsTm(java.lang.String)
     */
    @Override
    public void setIsTm(String isTm) {
        this.isTm = isTm;
        System.out.println("A is setting isTm=" + isTm);
    }
    
}

class B implements TMSetable {
    
    private String isTm;
    /* 
     * @see io.beansoft.netty.netty4.bootstrap.TMSetable#setIsTm(java.lang.String)
     */
    @Override
    public void setIsTm(String isTm) {
        this.isTm = isTm;
        System.out.println("B is setting isTm=" + isTm);
    }
}

Output result:

A is setting isTm=haha
B is setting isTm=haha again

Judging from the title, I don’t see the need for generic design.

Ty80

From a design perspective, it is better to design an interface or abstract class to abstract the isTm attribute.

But if you can no longer modify these classes, and they do not inherit or implement a common parent class or interface, then you can only use reflection.

But using reflection will make the code very ugly, and it will be difficult to find problems. If it is not used well, it is a trap.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!