A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© fanxiaolin 中级黑马   /  2016-4-11 15:00  /  703 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

package com.mianxiangduixiang.text;

public class Person {
        private String name;
        private int age;
       
        public void speak() {
                System.err.println("我的名字是: "+name+" \t "+ "我的年龄书: " + age);
        }
       
        public String getName() {
                return name;
        }

        public void setName(String name) {
                this.name = name;
        }

        public int getAge() {
                return age;
        }

        public void setAge(int age) {
                        this.age = age;
        }

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

        public boolean equalsAge(Person person){
                if (0 > age || age > 100) {
                        throw new RuntimeException(age+"输入年龄错误");
                }else {
                        return person.age==this.age;
                }
               
               
        }

       
}


为什么 我把判断
if (0 > age || age > 100) {
throw new RuntimeException(age+"输入年龄错误");
写在setAge下面 后, 通过new  调用, 但是 判断没执行!
而是写在 boolean 下才能判断

是不是 , 我的构造函数  set 和get  没有运行  
所以就不能判断

1 个回复

倒序浏览
把你说的代码写出来看下,是不是你语句有写错
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马