本帖最后由 lockwood 于 2015-5-6 21:37 编辑
// I adopt two methods to define class.
//first
class Dog { // create Dog class
private String name;//define name;
private int age;//define age;
public Dog(String name, int age) { //define constructor;
this.name = name;
this.age = age;
}
public Dog() {}
public void show() {
System.out.println(name + "***" + age);
}
//second
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;
}
}
class DogTest {
public static void main(String[] args) {
Dog d1 = new Dog("阿黄",20);
d1.show();
Dog d2 = new Dog();
d2.setName("小黑");
d2.setAge(30);
System.out.println(d2.getName());
System.out.println(d2.getAge());
}
} |
|