Learn Javabean

xiaoxiao2021-03-06  90

What is javabean

His actually a Java is a class, we have to discuss: 1. Execute java.io.serializable interface 2. Provide a parameter-free constructor 3. Provide Getter and Setter methods to access its properties. Let us create one Simple javabean to see it! Example: package com.stardeveloper.bean.test;

Public class SimpleBeans 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 () {

Return Age;

}

Public void setage (INT i) {

AGE = I;

}

} Save him in the / web-inf / class / com / starteveloper / bean / test file; explain: first line: package com.stardeveloper.bean.test; package declaration; let us define our class, make He can execute a 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 called as features (Properties or variables) in JavaBean, these props are private Therefore, other classes cannot be directly accessed, in order to access themselves, we provide Get and setter methods to get and set their values, private string name = null;

Private int Age = 0; and below we created a parameter-free constructor, in keeping: a constructor without (EMPTY) parameter in JavaBean, instead of calling you. Just like: public simpleBean () {}; To use the Get and Setter methods to get and set javabean features (Properties or variables) is simple, what we have to do is the name of the added features, eg name; make his first writing letters capital, name; now our Get The setter method of the Getter method and SET becomes: public string getname () {

Return Name;

}

Public void setname (string s) {

Name = s;

} Look! More simple! Because the name is a character pattern, we return getName () value should also be the same type, which is the same for setName (), so he has a character type parameter S; let's add four getter and Setter method, public string getName () {

Return Name;

}

Public void setname (string s) {

Name = s;

}

Public int getage () {

Return Age;

}

Public void setage (INT i) {

AGE = I;

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

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

New Post(0)