第六章课堂作业

文章展示了如何在Java中创建一个名为Dog的类,包含私有成员变量如名字、性别、颜色和叫声,以及对应的getter和setter方法。通过构造方法初始化对象,并在Dog2类的main方法中创建了两个Dog对象,调用了introduce和bark方法输出相关信息。
摘要由CSDN通过智能技术生成

 

package 第六章;
 
 
 
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;
	}
}

 

package 第六章;
 
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);//输出
	}
 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值