代码如下:
public class Dog {
//成员变量
private String name;
private String sex;
private String coall;
private String jiao;
//对外提供getter和setter方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getCoall() {
return coall;
}
public void setCoall(String coall) {
this.coall = coall;
}
public String getJiao() {
return jiao;
}
public void setJiao(String jiao) {
this.jiao = jiao;
}
//成员方法
public String introduce() {
return sex+"狗"+name+"是"+coall+"色的";
}
public String bark() {
return sex+"狗"+name+"正在"+jiao+"叫";
}
//构造参数
public Dog() {
super();
}
public Dog(String name, String sex, String coall) {
super();
this.name = name;
this.sex = sex;
this.coall = coall;
}
public Dog(String name, String sex, String coall, String jiao) {
super();
this.name = name;
this.sex = sex;
this.coall = coall;
this.jiao = jiao;
}
}
public class DogTest {
public static void main(String[] args) {
//创建对象调用构造方法
Dog Dog1 = new Dog();
Dog Dog2 = new Dog("小陆","公","黄");
String introduce = Dog2.introduce();
//输出
System.out.println(introduce);
//创建对象调用构造方法
Dog Dog3 = new Dog("王恩恒","公","红","汪汪");
String bark = Dog3.bark();
System.out.println(bark);
}
}
代码结果: