JavaBean

  1. /*在Java中,有很多class的定义都符合这样的规范:
  2. 若干private实例字段;
  3. 通过public方法来读写实例字段。
  4. */
  5. // 读方法:
  6. public Type getXyz()
  7. // 写方法:
  8. public void setXyz(Type value)
  9. // boolean字段比较特殊,它的读方法一般命名为isXyz()
  10. // 读方法:
  11. public boolean isChild()
  12. // 写方法:
  13. public void setChild(boolean value)
  14. 我们通常把一组对应的读方法(getter)和写方法(setter)称为属性(property)。例如,name属性:
  15. 对应的读方法是String getName()
  16. 对应的写方法是setName(String)
  17. // 只有getter的属性称为只读属性(read-only)
  18. // 只有setter的属性称为只写属性(write-only)
  19. // getter和setter也是一种数据封装的方法

实体类(entity)

JavaBean有特殊的写法:

  • 必须要有一个无参构造
  • 属性必须私有化
  • 必须有对应的get/set方法

一般用来和数据库的字段做映射 ORM;

ORM:对象关系映射

  • 表——> 类
  • 字段—-> 属性
  • 行记录——> 对象

People.java

  1. // 实体类 我们一般都是和数据库中的表结构一一对应!

javabean.jsp

  1. <%
  2. // People people = new People();
  3. // people.getAddress();
  4. // people.setAge();
  5. // people.setId();
  6. // people.setName();
  7. %>
  8. <%--等价于上面--%>
  9. <jsp:useBean id="people" class="com.shuai.entity.People" scope="page"/>
  10. <jsp:setProperty name="people" property="address" value="上海"/>
  11. <jsp:setProperty name="people" property="id" value="1"/>
  12. <jsp:setProperty name="people" property="age" value="18"/>
  13. <jsp:setProperty name="people" property="name" value="勇敢牛牛"/>
  14. 姓名:<jsp:getProperty name="people" property="name"/>
  15. ID:<jsp:getProperty name="people" property="id"/>
  16. 年龄:<jsp:getProperty name="people" property="age"/>
  17. 地址:<jsp:getProperty name="people" property="address"/>

枚举类

  1. // 我们通过static final来定义常量
  2. // 枚举类中的enum为了解决
  3. //无论是int常量还是String常量,使用这些常量来表示一组枚举值的时候,编译器无法检查每个值的合理性
  4. public class Object {
  5. public static void main(String[] args) {
  6. Weekday day = Weekday.SUN;
  7. if (day == Weekday.SAT || day == Weekday.SUN){
  8. System.out.println("work at home!");
  9. }else {
  10. System.out.println("Work at office!");
  11. }
  12. }
  13. }
  14. public class Object{
  15. public static void main(String[] args) {
  16. String s = Weekday.SUN.name();
  17. // "SUN" 返回常数定义的字符串,注意不要使用toString()
  18. int n = Weekday.SUN.ordinal();
  19. // 返回该常量的索引值,返回按常数定义的顺序(无实质意义)
  20. System.out.println(s);
  21. System.out.println(n);
  22. }
  23. }
  24. enum Weekday {
  25. SUN,MON,TUE,WED,THU,FRI,SAT;
  26. }
  27. public class Object{
  28. public static void main(String[] args) {
  29. Weekday day = Weekday.SUN;
  30. if (day.dayValue == 6 || day.dayValue == 0){
  31. System.out.println("work at home!");
  32. }else {
  33. System.out.println("work at office!");
  34. }
  35. }
  36. }
  37. enum Weekday {
  38. MON(1),TUE(2),WED(3),THU(4),FRT(5),SUN(0);
  39. // 这样就无须担心顺序的变化
  40. public final int dayValue;
  41. private Weekday(int dayValue){
  42. this.dayValue = dayValue;
  43. }
  44. }
  45. import javax.swing.*;
  46. public class Object{
  47. public static void main(String[] args) {
  48. Weekday day = Weekday.SUM;
  49. if (day.dayValue == 6 || day.dayValue == 0){
  50. System.out.println("Today is " + day +"work at home!");
  51. }else {
  52. System.out.println("Today is " + day + "work at office!");
  53. }
  54. }
  55. }
  56. enum Weekday{
  57. MON(1,"星期一"),TUE(2,"星期二"),WED(3,"星期三"),THU(4,"星期四"),FRI(5,"星期五"),SAT(6,"星期六"),SUM(0,"星期日");
  58. // enum的构造方法要声明为private,字段强烈建议声明为final
  59. public final int dayValue;
  60. private final String chinese;
  61. private Weekday(int dayValue,String chinese){
  62. this.dayValue = dayValue;
  63. this.chinese = chinese;
  64. }
  65. @Override
  66. public String toString() {
  67. return this.chinese;
  68. }
  69. }
  70. // enum适合用在switch语句中
  71. public class Object{
  72. public static void main(String[] args) {
  73. Weekday day = Weekday.WED;
  74. switch (day){
  75. case MON:
  76. case TUE:
  77. case WED:
  78. case THU:
  79. case FRI:
  80. System.out.println("Today is " + day + " work at office!");
  81. break;
  82. case SAT:
  83. case SUN:
  84. System.out.println("Today is" + day + " work at home!");
  85. break;
  86. default:
  87. throw new RuntimeException("connot process" + day);
  88. }
  89. }
  90. }
  91. enum Weekday{
  92. MON,TUE,WED,THU,FRI,SAT,SUN;
  93. }