第一范文网 - 专业文章范例文档资料分享平台

JAVA程序设计实验报告4(全)

来源:用户分享 时间:2025/5/24 21:59:54 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

Point p = new Point (3,5); Circle c=new Circle (4,6,3); System.out.println(p); System.out.println(c);

System.out.println(\圆的周长为:\+c.circumference());

System.out.println(\圆的面积为\+c.area()); } }

2、定义一个名为Vehicles(交通工具)的基类,并添加brand(品牌)和color(颜色),以及成员方法run(行驶,在控制台显示“我已经开动了”),showInfo(显示品牌和颜色),添加构造方法对成员属性初始化。编写Car类继承于Vehicles类,增加int型成员属性seats(座位)和成员方法showCar(在控制台显示小汽车的信息),并编写构造方法对其初始化。编写Truck(卡车)类继承于Vehicles类,增加float型成员属性load(载重)和成员方法showTruck(在控制台显示卡车的信息),并编写构造方

5

法。在main方法中new对象v1,c1,t1,并调用方法在控制台显示成员信息。 class Vehicles {

String brand; String color; public void run() {

System.out.println(\我已经开动了!\); }

public Vehicles(String brand,String color) {

this.brand=brand; this.color=color; }

public void showinfo() {

System.out.println(\商标:\+brand); System.out.println(\颜色:\+color); } }

6

class Car extends Vehicles {

int seats;

public Car(String brand,String color,int seats) {

super(brand,color); this.seats=seats; }

public void showCar() {

super.showinfo();

System.out.println(\座位:\+seats); } }

class Truck extends Vehicles {

float load;

public Truck(String brand,String color,int load) {

7

super(brand,color); this.load=load; }

public void showTruck() {

super.showinfo();

System.out.print(\载重:\+load); } }

public class TestVehicles {

public static void main(String args[]) {

Vehicles v1=new Vehicles(\别克\,\白色\); v1.showinfo();

Car c1=new Car(\宝马\,\黑色\,5); c1.showCar();

Truck t1=new Truck(\解放\,\红色\,10); t1.showTruck(); }

8

搜索更多关于: JAVA程序设计实验报告4(全) 的文档
JAVA程序设计实验报告4(全).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c8pavk41i4l5nd0f7mk1w_2.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top