当前位置:   article > 正文

编写Person类,类中具有姓名,年龄,性别,地址属性, 属性值如下列表:姓名年龄性别地址;张三23男河南;李四45男河南;王五34女山西;赵六22男上海;田七33女北京 1) 将以上5个人的信息存_设计一个person类,包含姓名,性别,年龄,家庭住址

设计一个person类,包含姓名,性别,年龄,家庭住址

Person类:

  1. public class Person {
  2. private String name;
  3. private int age;
  4. private String sex;
  5. private String address;
  6. public Person(){
  7. }
  8. public Person(String name, int age, String sex, String address) {
  9. this.name = name;
  10. this.age = age;
  11. this.sex = sex;
  12. this.address = address;
  13. }
  14. @Override
  15. public String toString() {
  16. return "Person{" +
  17. "name='" + name + '\'' +
  18. ", age=" + age +
  19. ", sex='" + sex + '\'' +
  20. ", address='" + address + '\'' +
  21. '}';
  22. }
  23. public String getName() {
  24. return name;
  25. }
  26. public void setName(String name) {
  27. this.name = name;
  28. }
  29. public int getAge() {
  30. return age;
  31. }
  32. public void setAge(int age) {
  33. this.age = age;
  34. }
  35. public String getSex() {
  36. return sex;
  37. }
  38. public void setSex(String sex) {
  39. this.sex = sex;
  40. }
  41. public String getAddress() {
  42. return address;
  43. }
  44. public void setAddress(String address) {
  45. this.address = address;
  46. }
  47. }

Homework类:

  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.ListIterator;
  4. public class Homework {
  5. public static void main(String[] args) {
  6. List list = new ArrayList();
  7. Person p = new Person("张三",23,"男","河南");
  8. list.add(p);
  9. list.add(new Person("李四",45,"男","河南"));
  10. list.add(new Person("王五",34,"女","山西"));
  11. list.add(new Person("赵六",22,"男","上海"));
  12. list.add(new Person("田七",33,"女","北京"));
  13. ListIterator it = list.listIterator();
  14. while (it.hasNext()){
  15. Person per =(Person)it.next();
  16. String name = per.getName();
  17. if ("李四".equals(name)){
  18. it.add(new Person("zhao6",24,"女","广州"));
  19. }
  20. if ("王五".equals(name)){
  21. it.remove();
  22. }
  23. }
  24. for (Object obj:list){
  25. System.out.println(obj);
  26. }
  27. }
  28. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/136470
推荐阅读
  

闽ICP备14008679号