这周学了类的封装
这是对类的属性和方法进行定义
public class Studet1 {
private String name;
private int character;
public void setName(String name){
this.name = name;
}
public String getName() {
return name;
}
public void setCharacter(int character){
if(character >= 1 && character <= 10){
this.character = character;
}else {
System.err.println("人品值必须介于[1,10]之间!");
}
}
public int getCharacter(){
return character;
}
public void introduceMyself(){
if(character >= 1 && character <= 10){
System.out.println("Hello,我叫"+name+",我的人品值为"+character);
}
}
}
`
这是对类的调用
public class TestStudent1 {
public static void main(String[] args) {
Studet1 studet1 = new Studet1();
studet1.setName("xxx");
studet1.setCharacter(-1);
studet1.introduceMyself();
}
}