慕粉3432276
2016-06-19 16:07
父类 package DaDa; public abstract class Car { private String carName; private int price; private int peopleNum; private double weight; public String getCarName() { return carName; } public void setCarName(String carName) { this.carName = carName; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } public int getPeopleNum() { return peopleNum; } public void setPeopleNum(int peopleNum) { this.peopleNum = peopleNum; } public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } }
子类——1
package DaDa;
public class passengerCar extends Car {
public passengerCar(String carName,int price,int peopleNum,double weight){
this.setCarName(carName);
this.setPrice(price);
this.setPeopleNum(peopleNum);
this.setWeight(weight);
}
}
子类——2
package DaDa;
public class pickUp extends Car {
public pickUp(String carName,int price,int peopleNum,double weight){
this.setCarName(carName);
this.setPrice(price);
this.setPeopleNum(peopleNum);
this.setWeight(weight);
}
}
子类——3
package DaDa;
public class truck extends Car {
public truck(String carName,int price,int peopleNum,double weight){
this.setCarName(carName);
this.setPrice(price);
this.setPeopleNum(peopleNum);
this.setWeight(weight);
}
}
main()测试类
ackage DaDa;
import java.util.*;
public class DaDaTaxi {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in=new Scanner(System.in);
Car[] carList={new passengerCar("奥迪A4",500,4,0),new passengerCar("马自达6",400,4,0),new pickUp("皮卡雪",450,4,2),new passengerCar("金龙",800,40,0),new truck("松花江",400,0,4),new truck("柯唯一",1000,0,20)};
System.out.println("请问是否需要租车:1,租车/0,不租车");
int input=in.nextInt();
if(input==1){
System.out.println("您可以租赁的车辆及相关信息:");
System.out.println("序号\t汽车名称\t租金\t载客量\t载货量");
for(int i=0;i<carList.length;i++){
System.out.println((i+1)+"\t"+carList[i].getCarName()+"\t"+carList[i].getPrice()+"元/天"+"\t"+carList[i].getPeopleNum()+"人"+"\t"+carList[i].getWeight()+"吨");
}
System.out.println("请输入租车的数量:");
int num=in.nextInt();
System.out.println("请输入租车的天数:");
int days=in.nextInt();
int allMoney=0;
int allPeople=0;
double allWeight=0;
for(int i=1;i<=num;i++){
System.out.println("请输入第"+i+"辆的序号:");
int nums=in.nextInt();
int money=carList[nums-1].getPrice()*days;
allMoney+=money;
int people=carList[nums-1].getPeopleNum();
allPeople+=people;
double weight=carList[nums-1].getWeight();
allWeight+=weight;
}
System.out.println("总金额为:"+allMoney);
System.out.println("总载客量为:"+allPeople);
System.out.println("总载货量为:"+allWeight);
}else{
System.out.println("信息错误");
System.exit(0);
}
}
}
赞一个
父类里面用了封装是吗,我看到有的没有用,封装那一章没怎么看懂,给赞一个
Java入门第二季 升级版
530655 学习 · 6091 问题
相似问题