封装
package com.oop.Demo04;
public class Student {
private String name;
private int id;
private char sex;
private int age;
public String getName(){
return this.name;
}
public void setName(){
this.name =name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if (age>120||age<0){
age =3;
}else {
this.age = age;
}
}
}
package com.oop;
import com.oop.Demo03.Pet;
import com.oop.Demo04.Student;
public class Application {
public static void main(String[] args) {
Student student = new Student();
student.setName("阳光");
System.out.println(student.getName());
student.setAge(50);
System.out.println(student.getAge());
}
}
转载请注明原文地址:https://ipadbbs.8miu.com/read-56463.html