Java作业,求大哥大姐给个答案,要不我的选修就挂了,谢谢......

来源:百度知道 编辑:UC知道 时间:2024/05/26 08:14:19
定义一个人类,包含姓名,性别,年龄等信息,所有的变量必须私有其他累只能通过该类的方法获取和修改以上属性.实例化一个人类,并通过该类的方法修改实例化的人的信息!

public class PersonTest {
  public static void main(String[] args) {
  Person person = new Person("Allen", "male", 18);
  System.out.println(person.toString());
  // reset the name
  person.setName("John");
  // reset the sex
  person.setSex("female");
  // reset the age
  person.setAge(20);
  System.out.println(person.toString());
  }
  }

  class Person {
  // name
  private String name;
  // sex
  private String sex;
  // age
  private int age;

  // constructor
  public Person(String name, String sex, int age) {
  this.name = name;
  this.sex = sex;
  this.age = age;
  }

  public String getName() {
  return this.name;
  }

  public String getSex() {
  return sex;
  }

  public void setSex(String argSex) {
  sex = argSex;
  }

  public in