JavaBean的介绍及属性分类

2019-05-07  本文已影响0人  背对背拥抱
1.什么是JavaBean?
  • JavaBean是可复用的平台独立的软件组件,开发者可以在软件构造器工具中对其直接进行可视化操作;
  • 软件组件就是指可以进行独立分离、易于重复使用的软件部分。
  • JavaBean就是一种基于Java平台的软件组件思想;
    JavaBean也是一种独立于平台和结构的应用程序编程接口(API)。JavaBean保留了其他软件组件的技术精华,并添加了被其他软件组件技术忽略的技术特性,使得它成为完整的软件组件解决方案的基础,并可移植的Java平台上方便的用于网络世界中。

通俗地讲,bean,保存数据的实体类,通常与数据库中的表对应,也称为,pojo、entity、domain。比如Person,Apple等,只有private属性和public setxxxx和getxxx;具体实例就是对应表中的一行,那些Hibernate,ibatis等orm框架,都支持它们和表的互相映射。

2.如何使用JavaBean?

使用javabean必须满足下面两种条件:

3.JavaBean的分类:

JavaBean可分为两种:

4.JavaBean属性的分类及使用:
1) 简单属性(simple property):

定义:

public class Student {
  private String name;
  private String age;
  public String getName() {
      return name;
  }
  public void setName(String name) {
      this.name = name;
  }
  public String getAge() {
      return age;
  }
  public void setAge(String age) {
      this.age = age;
  }
}
2) 索引属性(indexed property):

定义:

public class Student {
private String[] hobbies;

//设置或获取属性本身
public String[] getHobbies() {
    return hobbies;
}

public void setHobbies(String[] hobbies) {
    this.hobbies = hobbies;
}

//设置或获取属性中的元素
public String getHobbies(int i) {
    return hobbies[i];
}

public void setHobbies(int i,String hobby) {
    this.hobbies[i] = hobby;
}
}
3) 绑定属性(bound property):

定义:

使用步骤:

第一步:要使用这个类,bean必须有此类的数据域;

private PropertyChangeSupport listeners = new PropertyChangeSupport(this);

第二步:注册监听器,bean要实现下面两个方法:

void addPropertyChangeListener(PropertyChangeListener listener);
void removePropertyChangerListener(PropertyChangerListener listener);

第三步:将变化的bean属性事件传递给监听器:

isteners.firePropertyChange(String propertyName, Object oldString, Object newString);

注意:该方法有三个参数:属性的名字、旧的值以及新的值。属性的值必须是对象,如果是简单数据类型,则必须进行包装。

下面用SWT举个实际例子:
PropertyChangeSupport的方法可以写在Bean类中,但是在实际项目中,Bean类太多,我们一般把PropertyChangeSupport写在单独的类中,然后让所有的Bean类继承它即可。

第一步:
PropertyChangeSupport类:

public class Model {
    private PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
    
    public void addPropertyChangeListener(PropertyChangeListener listener) {
        changeSupport.addPropertyChangeListener(listener);
    }
         
    public void removePropertyChangeListener(PropertyChangeListener listener) {
        changeSupport.removePropertyChangeListener(listener);
    }
    
    protected void firePropertyChange(String propertyName, Object oldValue,Object newValue) {
        changeSupport.firePropertyChange(propertyName, oldValue, newValue);
    }
    
}

第二步:
JavaBean类:

public class Student extends Model{
    private String name;
    private String age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.firePropertyChange("name", this.name, this.name = name);
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.firePropertyChange("age", this.age, this.age = age);
    }
    
}

第三步:用SWT/Jface画的界面,需要绑定属性字段:

import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.StatusLineManager;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.jface.databinding.swt.WidgetProperties;
import org.eclipse.core.databinding.beans.BeanProperties;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class StudentDesign extends ApplicationWindow {
    private DataBindingContext m_bindingContext;
    private Text text;
    private Text text_1;
    Student student  = new Student();
    private int flag = 0;

    /**
     * Create the application window.
     */
    public StudentDesign() {
        super(null);
        createActions();
//      addToolBar(SWT.FLAT | SWT.WRAP);
//      addMenuBar();
//      addStatusLine();
    }

    /**
     * Create contents of the application window.
     * @param parent
     */
    @Override
    protected Control createContents(Composite parent) {
        Composite container = new Composite(parent, SWT.NONE);
        container.setLayout(new GridLayout(5, false));
        new Label(container, SWT.NONE);
        new Label(container, SWT.NONE);
        new Label(container, SWT.NONE);
        new Label(container, SWT.NONE);
        new Label(container, SWT.NONE);
        new Label(container, SWT.NONE);
        new Label(container, SWT.NONE);
        new Label(container, SWT.NONE);
        new Label(container, SWT.NONE);
        new Label(container, SWT.NONE);
        new Label(container, SWT.NONE);
        new Label(container, SWT.NONE);
        new Label(container, SWT.NONE);
        
        Label label = new Label(container, SWT.NONE);
        label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
        label.setText("\u59D3\u540D\uFF1A");
        
        text = new Text(container, SWT.BORDER);
        GridData gd_text = new GridData(SWT.LEFT, SWT.CENTER, true, false, 1, 1);
        gd_text.widthHint = 100;
        text.setLayoutData(gd_text);
        new Label(container, SWT.NONE);
        new Label(container, SWT.NONE);
        new Label(container, SWT.NONE);
        
        Label label_1 = new Label(container, SWT.NONE);
        label_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
        label_1.setText("\u5E74\u9F84\uFF1A");
        
        text_1 = new Text(container, SWT.BORDER);
        GridData gd_text_1 = new GridData(SWT.LEFT, SWT.CENTER, true, false, 1, 1);
        gd_text_1.widthHint = 100;
        text_1.setLayoutData(gd_text_1);
        new Label(container, SWT.NONE);
        new Label(container, SWT.NONE);
        new Label(container, SWT.NONE);
        
        Button button = new Button(container, SWT.NONE);
        button.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                flag++;
                student.setName("张三"+flag);
                student.setAge("十八岁"+flag);
            }
        });
        GridData gd_button = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
        gd_button.widthHint = 80;
        button.setLayoutData(gd_button);
        button.setText("\u6309\u94AE");
        new Label(container, SWT.NONE);
        m_bindingContext = initDataBindings();

        return container;
    }

    /**
     * Create the actions.
     */
    private void createActions() {
        // Create the actions
    }

    /**
     * Create the menu manager.
     * @return the menu manager
     */
    @Override
    protected MenuManager createMenuManager() {
        MenuManager menuManager = new MenuManager("menu");
        return menuManager;
    }

    /**
     * Create the toolbar manager.
     * @return the toolbar manager
     */
    @Override
    protected ToolBarManager createToolBarManager(int style) {
        ToolBarManager toolBarManager = new ToolBarManager(style);
        return toolBarManager;
    }

    /**
     * Create the status line manager.
     * @return the status line manager
     */
    @Override
    protected StatusLineManager createStatusLineManager() {
        StatusLineManager statusLineManager = new StatusLineManager();
        return statusLineManager;
    }

    /**
     * Launch the application.
     * @param args
     */
    public static void main(String args[]) {
        Display display = Display.getDefault();
        Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
            public void run() {
                try {
                    StudentDesign window = new StudentDesign();
                    window.setBlockOnOpen(true);
                    window.open();
                    Display.getCurrent().dispose();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Configure the shell.
     * @param newShell
     */
    @Override
    protected void configureShell(Shell newShell) {
        super.configureShell(newShell);
        newShell.setText("New Application");
    }

    /**
     * Return the initial size of the window.
     */
    @Override
    protected Point getInitialSize() {
        return new Point(475, 352);
    }
    protected DataBindingContext initDataBindings() {
        DataBindingContext bindingContext = new DataBindingContext();
        //
        IObservableValue observeTextTextObserveWidget = WidgetProperties.text(SWT.Modify).observe(text);
        IObservableValue nameStudentObserveValue = BeanProperties.value("name").observe(student);
        bindingContext.bindValue(observeTextTextObserveWidget, nameStudentObserveValue, null, null);
        //
        IObservableValue observeTextText_1ObserveWidget = WidgetProperties.text(SWT.Modify).observe(text_1);
        IObservableValue ageStudentObserveValue = BeanProperties.value("age").observe(student);
        bindingContext.bindValue(observeTextText_1ObserveWidget, ageStudentObserveValue, null, null);
        //
        return bindingContext;
    }
}

此时,我们无论是修改界面,还是修改bean,都能动态的获取值和变化值。

4)约束属性(constrained property):

定义:

约束属性是指当该种属性的值发生变化时,要通知其它的对象,但是该对象可否决属性值的变更。

约束属性用到的类是VetoableChangeSupport,用法和绑定属性类似,在此我就不详细介绍了…

上一篇下一篇

猜你喜欢

热点阅读