6-1 Java类实现-小狗类输出样例: Puppy's age is : 2 Puppy's name is : wangzai Puppy's sex is : male
时间: 2024-05-05 22:20:07 浏览: 118
好的,这是一个小狗类的示例代码,可以实现输出小狗的年龄、名字和性别:
```
public class Puppy {
private int age;
private String name;
private String sex;
public Puppy(int age, String name, String sex) {
this.age = age;
this.name = name;
this.sex = sex;
}
public int getAge() {
return age;
}
public String getName() {
return name;
}
public String getSex() {
return sex;
}
public void setAge(int age) {
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public void setSex(String sex) {
this.sex = sex;
}
public void printInfo() {
System.out.println("Puppy's age is : " + age);
System.out.println("Puppy's name is : " + name);
System.out.println("Puppy's sex is : " + sex);
}
}
```
你可以根据需要调用该类的构造方法和方法,实现对小狗对象的创建和信息输出。例如:
```
Puppy puppy = new Puppy(2, "wangzai", "male");
puppy.printInfo();
```
以上代码会输出:
```
Puppy's age is : 2
Puppy's name is : wangzai
Puppy's sex is : male
```
阅读全文