如何在Generic JavaBean中定义protected,public和private?

时间:2016-04-07 07:12:28

标签: java javabeans

我想知道如何在我的类GenericBean中定义protected属性,公共属性和私有属性,这将导致JavaBean。到目前为止,我已经声明了一个类,它将允许使用来访问Bean的值,但是,我不知道如何处理这些属性的不同访问。任何的想法?这是我的课程:

abstract class GenericBean {

    protected PropertyChangeSupport chg = new PropertyChangeSupport(this);
    protected VetoableChangeSupport veto = new VetoableChangeSupport(this);

    public void addPropertyChangeListener(PropertyChangeListener pcl) {
        chg.addPropertyChangeListener(pcl);
    }

    public void removePropertyChangeListener(PropertyChangeListener pcl) {
        chg.addPropertyChangeListener(pcl);
    }

    class BoundedProperty<T>  implements PropertyChangeListener {
        private String name;
        private T value;
        private Object chgHandlerObject;
        private Method changeHandler;

        public BoundedProperty(String name) {
            this.name = name;
        }

        public T getValue() { return value; }

        public void setValue(T newValue) {
            T old = value;
            value = newValue;
            chg.firePropertyChange(name, old, value);
        }

        public void propertyChange(PropertyChangeEvent e) {
            if (!e.getPropertyName().equals(name)) return;
            if (changeHandler == null) return;
            try {
                changeHandler.invoke(chgHandlerObject);
            } catch(Exception exc) {
                exc.printStackTrace();
            }
        }

        public void setChangeHandler(Object handl, String mname) {
            try {
                Method m = handl.getClass().getDeclaredMethod(mname);
                chgHandlerObject = handl;
                changeHandler = m;
                chg.addPropertyChangeListener(this);
            } catch(Exception exc) {
                exc.printStackTrace();
                return;
            }
        }


        public void setChangeHandler(Object ohandler) {
            try {
                Method m = ohandler.getClass().getDeclaredMethod(name+"Change");
                chgHandlerObject = ohandler;
                changeHandler = m;
            } catch(Exception exc) {
                exc.printStackTrace();
                return;
            }
            chg.addPropertyChangeListener(this);
        }

        public void removeChangeHandler() {
            changeHandler = null;
            chgHandlerObject = null;
            chg.removePropertyChangeListener(this);
        }
    }


}

这样我可以决定哪些方法可用于某些字段?

0 个答案:

没有答案