赞
踩
一、定义个Person类,属性有身份证号码、姓名、性别、年龄、籍贯、住址,行为包括:
(1)eating(String food),能够表示吃什么食物;
(2)shopping(String goods),表示购物;
(3)sleeping(int time),表示睡觉,参数time表示睡觉的时长;
(4)setName(String aName),表示修改人的姓名;
(5)setAge(int aAge),表示修改人的年龄;
(6)printPerson(),表示打印人的所有信息。
- package success;
-
- class Person{
- private String name;//姓名
- private int age;//年龄
- private String credit;//身份证号码
- private String hometown;//籍贯
- private String address;//地址
- private String sex;//性别
- private String behavior;//行为
- public Person(String name,int age,String credit,String hometown,String address,String sex,String behavior) {
- this.name=name;
- this.age=age;
- this.credit=credit;
- this.hometown=hometown;
- this.address=address;
- this.sex=sex;
- this.behavior=behavior;
- }
- public void eating(String food) {
- System.out.println(name+"在吃"+food);
- }
- public void shopping(String goods) {
- System.out.println(name+"在买"+goods);
- }
- public void sleeping(int time) {
- System.out.println(name+"睡了"+time+"个小时");
- }
- public void setaName(String aName) {
- name=aName;
- }
- public void setaAge(int aAge) {
- if(age>0)
- age=aAge;
- }
- public void printPerson() {
- System.out.println("姓名:"+name+"、年龄:"+age+"、身份证号码:"+credit+"、籍贯:"+hometown+"、住址:"+address+""
- + "、性别:"+sex+"、行为:"+behavior);
- }
- }
- public class Demo3{
- public static void main(String[]args) {
- Person p1=new Person("Tom",12,"123456","北京","北京天安门","、不详","干饭");
- p1.eating("饼干");
- p1.sleeping(8);
- p1.shopping("电脑");
- p1.printPerson();
- }
- }
-
-
-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。