JavaBean
/*在Java中,有很多class的定义都符合这样的规范:
若干private实例字段;
通过public方法来读写实例字段。
*/
// 读方法:
public Type getXyz()
// 写方法:
public void setXyz(Type value)
// boolean字段比较特殊,它的读方法一般命名为isXyz()
// 读方法:
public boolean isChild()
// 写方法:
public void setChild(boolean value)
我们通常把一组对应的读方法(getter)和写方法(setter)称为属性(property)。例如,name属性:
对应的读方法是String getName()
对应的写方法是setName(String)
// 只有getter的属性称为只读属性(read-only)
// 只有setter的属性称为只写属性(write-only)
// getter和setter也是一种数据封装的方法
实体类(entity)
JavaBean有特殊的写法:
- 必须要有一个无参构造
- 属性必须私有化
- 必须有对应的get/set方法
一般用来和数据库的字段做映射 ORM;
ORM:对象关系映射
- 表——> 类
- 字段—-> 属性
- 行记录——> 对象
People.java
// 实体类 我们一般都是和数据库中的表结构一一对应!
javabean.jsp
<%
// People people = new People();
// people.getAddress();
// people.setAge();
// people.setId();
// people.setName();
%>
<%--等价于上面--%>
<jsp:useBean id="people" class="com.shuai.entity.People" scope="page"/>
<jsp:setProperty name="people" property="address" value="上海"/>
<jsp:setProperty name="people" property="id" value="1"/>
<jsp:setProperty name="people" property="age" value="18"/>
<jsp:setProperty name="people" property="name" value="勇敢牛牛"/>
姓名:<jsp:getProperty name="people" property="name"/>
ID:<jsp:getProperty name="people" property="id"/>
年龄:<jsp:getProperty name="people" property="age"/>
地址:<jsp:getProperty name="people" property="address"/>
枚举类
// 我们通过static final来定义常量
// 枚举类中的enum为了解决
//无论是int常量还是String常量,使用这些常量来表示一组枚举值的时候,编译器无法检查每个值的合理性
public class Object {
public static void main(String[] args) {
Weekday day = Weekday.SUN;
if (day == Weekday.SAT || day == Weekday.SUN){
System.out.println("work at home!");
}else {
System.out.println("Work at office!");
}
}
}
public class Object{
public static void main(String[] args) {
String s = Weekday.SUN.name();
// "SUN" 返回常数定义的字符串,注意不要使用toString()
int n = Weekday.SUN.ordinal();
// 返回该常量的索引值,返回按常数定义的顺序(无实质意义)
System.out.println(s);
System.out.println(n);
}
}
enum Weekday {
SUN,MON,TUE,WED,THU,FRI,SAT;
}
public class Object{
public static void main(String[] args) {
Weekday day = Weekday.SUN;
if (day.dayValue == 6 || day.dayValue == 0){
System.out.println("work at home!");
}else {
System.out.println("work at office!");
}
}
}
enum Weekday {
MON(1),TUE(2),WED(3),THU(4),FRT(5),SUN(0);
// 这样就无须担心顺序的变化
public final int dayValue;
private Weekday(int dayValue){
this.dayValue = dayValue;
}
}
import javax.swing.*;
public class Object{
public static void main(String[] args) {
Weekday day = Weekday.SUM;
if (day.dayValue == 6 || day.dayValue == 0){
System.out.println("Today is " + day +"work at home!");
}else {
System.out.println("Today is " + day + "work at office!");
}
}
}
enum Weekday{
MON(1,"星期一"),TUE(2,"星期二"),WED(3,"星期三"),THU(4,"星期四"),FRI(5,"星期五"),SAT(6,"星期六"),SUM(0,"星期日");
// enum的构造方法要声明为private,字段强烈建议声明为final
public final int dayValue;
private final String chinese;
private Weekday(int dayValue,String chinese){
this.dayValue = dayValue;
this.chinese = chinese;
}
@Override
public String toString() {
return this.chinese;
}
}
// enum适合用在switch语句中
public class Object{
public static void main(String[] args) {
Weekday day = Weekday.WED;
switch (day){
case MON:
case TUE:
case WED:
case THU:
case FRI:
System.out.println("Today is " + day + " work at office!");
break;
case SAT:
case SUN:
System.out.println("Today is" + day + " work at home!");
break;
default:
throw new RuntimeException("connot process" + day);
}
}
}
enum Weekday{
MON,TUE,WED,THU,FRI,SAT,SUN;
}