概念
问题的引入
封装性的体现
权限修饰符
案例
package lesson.l11_oop2;/*** Illustration** @author DengQing* @version 1.0* @datetime 2022/7/3 15:28* @function*/
public class Person {private int age;public void setAge(int age) {
/* if (age<0||age>130){throw new RuntimeException("传入的数据非法");}this.age=age;*/if (age >= 0 && age <= 130) {this.age = age;} else {
// this.age = 0;throw new RuntimeException("传入的数据非法");}}public int getAge() {return this.age;}
}class PersonTest {public static void main(String[] args) {Person person = new Person();person.setAge(-9);System.out.println(person.getAge());}
}