day05综合练习

来源:互联网 发布:手机霸屏软件 编辑:程序博客网 时间:2024/04/29 20:06

class day05
{
 public static void main(String[] args)
 {
  //输入年龄并打印
  GouZao gz = new GouZao(30);
  System.out.println(gz.getAge());
  gz.setAge(160);
  System.out.println(gz.getAge());
  //比较两人年龄是否相同
  GouZao p1 = new GouZao(33);
  GouZao p2 = new GouZao(40);
  System.out.println(p1.compare(p2));
 }
}
class GouZao
{
 {
  System.out.println("我是构造代码块...");
 }
 private int age;
 GouZao(int age)
 {
  this.age = age;
 }
 public int getAge()
 {
  return age;
 }
 public boolean compare(GouZao gz)
 {
  return this.age == gz.age;
 }
 public void setAge(int age)
 {
  if (age < 150 && age > 0)
  {
   this.age = age;
  }
  else
  {
   this.age = -1;
   System.out.println("你不是人...");
  }
 }
}
原创粉丝点击