java面向对象第三章简单习题

①创建database类,它相当于仓库,用于存放CD、DVD等光碟;
②CD、DVD等光碟类都具有相同的属性(标题title,播放时间playtime,是否外借isBrrow),在构造函数中初始化,以及方法print(输出三个属性);
③对于CD类,还需要添加独有的属性:艺术家artist,并且需要通过方法print输出,因此这里需要重写父类方法
④DVD类需要添加独有属性:导演director,并且需要通过方法print输出,因此这里需要重写父类方法
⑤最后将创建的CD、DVD对象存入database对象中,并打印database仓库中所有的光碟。
效果图如下所示:
在这里插入图片描述

package kehou;

public class datebase {
	private String title;
	private int playtime;
	private String isBrrow;
	public datebase(String title,int playtime,String isBrrow) {
		this.title=title;
		this.playtime=playtime;
		this.isBrrow=isBrrow;
	}
	public void print() {
		System.out.println("标题:"+this.title+"播放时间:"+this.playtime+"是否外借:"+this.isBrrow);
	}
	public String gettitle() {
		return(this.title);
	}
	public int getplaytime() {
		return(this.playtime);
	}
	public String getisBrrow() {
		return(this.isBrrow);
	}
}

package kehou;

public class CD extends datebase {
	public CD(String title,int playtime,String isBrrow,String artist) {
		super(title,playtime,isBrrow);
		this.artist=artist;
	}
	String artist;
	public void print() {
		System.out.println("CD"+"标题:"+super.gettitle()+",播放时间:"+super.getplaytime()+"分钟,是否外借:"+super.getisBrrow()+",艺术家:"+artist);
	}
}

package kehou;

public class DVD extends datebase {
	public DVD(String title,int playtime,String isBrrow) {
		super(title,playtime,isBrrow);
	}
	String director="黄渤";
	public void print() {
		System.out.println("DVD"+"标题:"+super.gettitle()+",播放时间:"+super.getplaytime()+"分钟,是否外借:"+super.getisBrrow()+",导演:"+director);
	}
}

package kehou;

public class test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		CD p=new CD("起风了时间",3,"外借","买辣椒也用劵");
		p.print();
		CD i=new CD("流量时间",3,"外借","半阳");
		i.print();
		DVD o=new DVD("一出好戏",3,"外借");
		o.print();
	}

}

在这里插入图片描述

(1)定义一个类,描述一个矩形Rectangle,包含有长、宽两种属性和计算面积方法getArea()。
(2)编写一个类Rectangular,继承自矩形类,同时该类描述长方体,具有长、宽、高属性和计算体积的方法。
(3)编写一个测试类,对以上两个类进行测试,创建一个长方体,定义其长、宽、高,输出其底面积和体积。

package kehou;

public class Reatangle {
	private int length;
	private int width;
	private int sum;
	public void getArea(int length,int width) {
		this.sum=length*width;
	}
	public void i() {
		System.out.println("底面积:"+this.sum);
	}
}

package kehou;

public class Rectangular extends Reatangle{
	int volume;
	public void getvolume(int length,int width,int gao) {
		this.volume=length*width*gao;
	}
	public void i() {
		System.out.println("体积:"+this.volume);
	}
}

package kehou;

public class text {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Reatangle e=new Reatangle();
		e.getArea(3, 4);
		Rectangular r=new Rectangular();
		r.getvolume(3, 4, 5);
		e.i();
		r.i();
	}

}

在这里插入图片描述

总结:多练才是王道。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值