class Dog
{
{
System.out.println("汪汪汪");
}//构造代码块
private String name;
private int age;
public Dog(){} //空参数的构造函数
public Dog(String name,int age)
{
this.name=name;
this.age=age;
}//有参数的构造函数
public void setName(String name)//set前面肯定是void
{
this.name=name;
}
public String getName()
{
return name;
}
public void setAge(int age)
{
this.age=age;
}
public int getAge()
{
return age;
}
}
class DogTest
{
public static void main(String[] args)
{
Dog h=new Dog();
System.out.println("------------");
h.setName("阿黄");
System.out.println(h.getName());
h.setAge(8);
System.out.println(h.getAge());
System.out.println("************");
Dog l=new Dog("豆豆",1);
System.out.println(l.getName());
System.out.println(l.getAge());
}
}
/*结果
汪汪汪
------------
阿黄
8
************
汪汪汪
豆豆
1
*/
|
|