权限修饰符 小练习
package com.atguigu.exer;
/*
* 创建程序,在其中定义两个类,Person和PersonTest类,定义如下
* 用setAge()设置人分合法年龄(0-130),用getAge()返回人的年龄
*/
public class Person
{
private int age;
public void setAge(int i ) {
if( i < 0 || i > 130) {
throw new RuntimeException("传入的数据违法");
// return; 不能共存
}
age = i;
}
public int getAge() {
return age;
}
}
package com.atguigu.exer;
public class PersonTest
{
public static void main(String[] args)
{
Person b = new Person();
// b.age = 1; 编译不通过 因为age ,private了
b.setAge(7);
System.out.println("年龄为" + b.getAge());
}
}