//对象例子,建立一个对象:人,然后指挥人说出岁数class Demo
{
public static void main(String[] args)
{
Car c=new Car();
c.setAge(-20);
//c.speak();
}}
class Car
{
private int age;
public void setAge(int a)
{
if(a>0&&a<130)
{
age=a;
speak();
}
else
System.out.print("nono"); }
public int getAge()
{
return age;
}
void speak()
{
System.out.print("age="+age);
}}
|
|