码迷,mamicode.com
首页 > 编程语言 > 详细

JavaBean

时间:2016-07-15 13:11:26      阅读:279      评论:0      收藏:0      [点我收藏+]

标签:

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     }

 

JavaBean

标签:

原文地址:http://www.cnblogs.com/stivebone/p/5673156.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!