Javabean

xiaoxiao2021-03-06  97

What is javabean? He is actually a Java is a class, we have to discuss:

1. Execute a java.io.serializable interface 2. Provide a parameter-free constructor 3. Provide getTER and SETTER methods to access its properties. Let us create a simple javabean to see it!

Example:

Package com.stardeveloper.bean.test; public class simplebean implements java.io.serializable {

/ * Properties * / private string name = null; private int Age = 0;

/ * EMPTY Constructor * / public simplebean () {}

/ * Getter and setter methods * / public string getname () {return name;}

Public void setname (string s) {name = s;}

Public int getage () {returnid

Public void setage (int i) {agn = i;}}

Save him in the / web-inf / class / com / starteveloper / bean / test file; explain: The first line: package com.stardeveloper.bean.test; package declaration;

Below we define our classes, allowing him to execute the java.io.serializable interface, note that this Serializable interface does not include any method. We declare that both Name and AGE variables, these two variables are characteristic in JavaBean (Properties or variables) Called, these props are private, so other classes cannot be accessed directly, so for us to access, we provide Get and setter methods to get and set their values.

Private string name = null; private int Age = 0;

And we have created a parameter-free constructor, in keeping: a constructor without (EMPTY) parameter in JavaBean, instead of calling you unused. Just like: public simpleBean () {}; use Getter and setter method It is simple to get GET and SET JAVABEAN (Properties or variable) is simple, what we have to do is the name of the added characteristics, eg name; make his first write letters, name; now our Get Getter method and set of setter The method changes to:

Public string getname () {return name;} public void setname (string s) {name = s;}

Look! Multi-simple! Because the name is character type, we return getName () value should also be the same type, the same type is the same, so he has a character type parameter S; below we add four getter and setter method.

Public string getname () {return name;} public void setname (string s) {name = s;}

Public int getage () {returnid

Public void setage (int i) {agn = i;}

Finally, this class is completed in}. Compile, like other Java code, a simple Javabean after success is generated.

转载请注明原文地址:https://www.9cbs.com/read-96222.html

New Post(0)