下面这个程序主要是熟悉无参构造方法和有参构造方法。
class DogTest
{
public static void main(String[] args)
{
Dog d = new Dog();
d.show();
}
}
class Dog
{
private String name;
private int age;
private int num = 20;
//无参构造方法
public Dog(){}
//有参构造方法
public Dog(String name,int age)
{
this.name = name;
this.age = age;
}
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public void setAge(int age)
{
this.age = age;
}
public int getAge()
{
return age;
}
public void show()
{
int num = 10;
System.out.println(this.num);//输出的结果是20
System.out.println(num);//输出的结果是10
}
}
|
|