- package com.itheima;
- public class Objects {
- public static void main(String[] args){
- Car c = new Car();
- c.setAge(-20);
- }
- }
- class Car{
- private String name="胡平燕";
- private char sex='男';
- private int age= 26;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public char getSex() {
- return sex;
- }
- public void setSex(char sex) {
- this.sex = sex;
- }
- public int getAge() {
- return age;
- }
- void demo(){
- System.out.print("age="+age);
- }
- public void setAge(int age) {
- if(age>0 && age<130)
- {
- this.age = age;
- demo();
-
- }
- else
- {
- System.out.print("您输入的年龄有误");
- }
-
- }
- }
- /*
- if(age>0 && age<130)
- {
- System.out.print("您输入的年龄有误");
- }
- else
- {
- this.age = age;
- demo();
- }
- 为什么我这样写年龄是-20岁也输出呢
- 我不都是一样的判断吗 如果年龄小于0就输出 “年龄输入有误”
- 否则就输出正确的年龄
- */
复制代码
谁能说说有什么区别 |