标签:
JavaBean两种模型(属性私有化,必须含有无参构造器和setter,getter方法属性只能通过setter,getter访问):
1、贫血模型(只有私有属性和setter,getter方法):
1 public class Person { 2 3 private String name; 4 private String addr; 5 private int age; 6 7 public String getName() { 8 return name; 9 } 10 11 public void setName(String name) { 12 this.name = name; 13 } 14 15 public String getAddr() { 16 return addr; 17 } 18 19 public void setAddr(String addr) { 20 this.addr = addr; 21 } 22 23 public int getAge() { 24 return age; 25 } 26 27 public void setAge(int age) { 28 this.age = age; 29 }
2、充血模型(除私有属性和setter,getter方法外,还含有本身的其他方法):
1 public class Person { 2 3 private String name; 4 private String addr; 5 private int age; 6 7 public String getName() { 8 return name; 9 } 10 11 public void setName(String name) { 12 this.name = name; 13 } 14 15 public String getAddr() { 16 return addr; 17 } 18 19 public void setAddr(String addr) { 20 this.addr = addr; 21 } 22 23 public int getAge() { 24 return age; 25 } 26 27 public void setAge(int age) { 28 this.age = age; 29 } 30 31 public void sing() { 32 System.out.println(name+"is singing"); 33 }
标签:
原文地址:http://www.cnblogs.com/stivebone/p/5673156.html