代码:
public class Dog {
/*成员变量
* 名字
* 性别
* 颜色
* 叫声*/
private String a;
private String b;
private String c;
private String d;
//对外提供的方法getter/setter
public String getA() {
return a;
}
public void setA(String a) {
this.a = a;
}
public String getB() {
return b;
}
public void setB(String b) {
this.b = b;
}
public String getC() {
return c;
}
public void setC(String c) {
this.c = c;
}
public String getD() {
return d;
}
public void setD(String d) {
this.d = d;
}
//成员方法
public String intcoduce() {
//System.out.println( b + "狗" + a + "是" + c +"色的");
return b + "狗" + a + "是" + c +"色的";
}
public String bark() {
return b + "狗" + a + "正在" + d + "叫";
}
//构造方法
public Dog() {
}
public Dog(String b, String a, String c) {
this.b = b;
this.a = a;
this.c = c;
}
public Dog(String b, String a, String c, String d) {
this.b = b;
this.a = a;
this.c = c;
this.d = d;
}
}
代码:
public class Dog2 { //第二个类
public static void main(String[] args) { //主函数
// TODO Auto-generated method stub
Dog a1 = new Dog("母", "阿花", "黄");//创建第一个对象
String intcoduce = a1.intcoduce();//调用方法
System.out.println(intcoduce);//输出调用的方法
Dog a2 = new Dog("公","小海","绿色","汪汪汪");//创建第二个对象
String bark = a2.bark();//调用第二个方法
System.out.println(bark);//输出
}
}