封装

package fengzhuang;


public class Fengzhuang {//创建类


private int age;// 成员变量
private static String name;


public void setName(String myName) {//封装
name = myName;
}
public void setAge(int myAge) {
if (myAge <= 0) {
System.out.println("年龄出错,使用默认年龄18岁代替");
age=18;
}
else
age=myAge;
}
public String getName() {
System.out.println("姓名:" + name);
return name;
}
public int getAge() {
System.out.println("年龄:" + age);
return age;
}
public static void main(String[] args) {// 声明对象和创建对象
Fengzhuang p1 = new Fengzhuang();
p1.setName("zhangsan");
p1.setAge(-10);
p1.getName();
p1.getAge();
}
}封装;