//运用一下今天学习的java类的私有privte
//定义一个名字为person的类,具有的属性是年龄
/*class person
{
private int age;//用private的将age进行封装
public void setAge(int a)//提供对外的接口
{
if (a > 0 && a < 130)
{
age = a;
speak();
}
else
System.out.println("illegatily");
}
private void speak()
{
System.out.println("age="+age);
}
}
*/
//调用person类
class persondemo
{
public static void main(String[] args)
{
person p = new person();
p.setAge(-40);
System.out.println("------------------------------------");
//bycicle b =new bycicle();
//setBycicle(2,"red");
}
}
//练习:描述一个自行车bicycle的类;
class bycicle
{
private int num;
private String color;
public void setBycicle(int num,String color)
{
num = num;
color = color;
miaoSu();
}
private void miaoSu()
{
System.out.println("bycicle.color="+color+"bycicle.num="+num);
}
}
不知怎么结果打印的结果总为illegatily
|