JAVA程序设计实验报告4(全) 联系客服

发布时间 : 星期六 文章JAVA程序设计实验报告4(全)更新完毕开始阅读

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