收藏 分销(赏)

《移动通信软件编程基础-JAVA》第四章(上)-一个习题.doc

上传人:仙人****88 文档编号:11398070 上传时间:2025-07-21 格式:DOC 页数:5 大小:125.50KB 下载积分:10 金币
下载 相关 举报
《移动通信软件编程基础-JAVA》第四章(上)-一个习题.doc_第1页
第1页 / 共5页
《移动通信软件编程基础-JAVA》第四章(上)-一个习题.doc_第2页
第2页 / 共5页


点击查看更多>>
资源描述
第4章 类和对象的高级特征 第4章 类和对象的高级特征 【实验目标】 完成本章的内容以后,您将达到: u 理解类和对象 u 理解抽象和封装 u 理解对象与类之间的关系 u 掌握包的创建和导入本章实验给出了全面的操作步骤,请学生按照给出的步骤独立完成实验,以达到要求的实验目标。 · 第一阶段——指导学习(40分钟) 1. 编写一个Demo.java,编译并运行 1) 建立文件名为“Demo.java”,输入以下程序代码。[注意:Java文件名必须与用public 修饰的类名保持一致] package com.imti.study; /** * 超类Box * @1.0版 2008年7月27日 * @author xx */ class Box { double width; double height; double depth; /** *构造方法重载 */ Box(double w, double h, double d) { width = w; height = h; depth = d; } /** *构造方法重载 */ Box(double len) { width = height = depth = len; } double volume() { return width * height * depth; } } /** * BoxWeight继承类Box * @1.0版 2008年7月27日 * @author xx */ class BoxWeight extends Box { double weight; /** *构造方法重载 */ BoxWeight(double w, double h, double d,double m) { super(w,h,d); weight=m; } /** *构造方法重载 */ BoxWeight(double len,double m) { super(len); weight=m; } /** *方法重写 */ double volume() { return width + height + depth; } } /** * BoxCost继承类BoxWeight * @1.0版 2008年7月27日 * @author xx */ class BoxCost extends BoxWeight { double cost; /** *构造方法重载 */ BoxCost(double w,double h,double d,double m,double c) { super(w,h,d,m); cost=c; } /** *构造方法重载 */ BoxCost(double len,double m,double c) { super(len,m); cost=c; } /** *方法重写 */ double volume() { return width*2; } } /** * Demo测试类 * @1.0版 2008年7月27日 * @author xx */ public class Demo{ public static void main(String args[]) { BoxCost cost1=new BoxCost(10,18,16,8,18.9); double vo1=cost1.volume(); System.out.println("Weight of BoxCost is:"+cost1.weight); System.out.println("Cost of BoxCost is:"+cost1.cost); System.out.println("Volume of BoxCost is:"+vo1); } } 2)编译并运行。 第二阶段——练习(40分钟) 习题一 有一个汽车类Vehicle,该类有两个属性name(名字),color(颜色),和一个void drive()方法,有两个类Bus和Car分别继承于Vehicle,要求分别在各自的构造方法中通过super调到父类的构造方法,并重写父类的void drive()方法.通过main方法进行测试.
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 通信科技 > 开发语言

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服