/**
* Created by 刘朋程 on 2014-5-22.
*/
class
Testprivate
{
private int age =
18;
//私有化age并赋值为18public void setAge(int age) //设置age范围
{
if (age>0 && age<100)
{
this.age = age;
speak();
//调用方法
}
else
System.out.println("你输入的age不满足要求");}
public int
getAge()
//获取age值
{
return age;
}
void
speak()
//定义方法speak输出一句话
{
System.out.println("my age is"
+ " "+ age);
}
}// Created by 刘朋程 on 2014-5-22
class TestprivateDemo
{
public static void main(String[] args)
{
Testprivate a = new Testprivate(); //新建一个对象
a.setAge(20);
//设置age值为20
}
}// Created by 刘朋程 on 2014-5-22
面向对象(封装private),布布扣,bubuko.com