package com.java.java_test3;
 
public class Car{
public String name;   //车的名称
public float rent;    //每日租金
public int people;    //可载人数
public float goods;   //可载货物吨数
 
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
 
public float getRent(){
return rent;
}
public void setRent(float rent){
this.rent = rent;
}
 
public int getPeople(){
return people;
}
public void setPeople(int people){
this.people = people;
}
 
public float getGoods(){
return goods;
}
public void setGoods(float goods){
this.goods = goods;
}
}
package com.java.java_test3;
 
public class PassangerCar extends Car{
public PassangerCar(){
System.out.println("Please enter the PassangerCar parameter");
}
 
public PassangerCar(String name,float rent,int people){
this.name = name;
this.rent = rent;
this.people = people;
}
}package com.java.java_test3;
 
public class Pickup extends Car{
public Pickup(){
System.out.println("Please enter the Pickup parameter");
}
 
public Pickup(String name,float rent,int people,float goods){
this.name = name;
this.rent = rent;
this.people = people;
this.goods = goods;
}
}package com.java.java_test3;
 
public class Truck extends Car{
public Truck(){
System.out.println("Please enter the Truck parameter");
}
 
public Truck(String name,float rent,float goods){
this.name = name;
this.rent = rent;
this.goods = goods;
}
}package com.java.java_test3;
import java.util.Scanner;
 
public class Initial{
int total_People;   //总人数
float total_goods;  //可载货物总吨数
float total_price;  //租金总价格
int[] count = new int[6]; //用来统计每种车分别租没租,以及租了几辆
int days;           //租借天数
//用多态的方式将父类引用指向子类实例,由于子类的属性父类都有,故父类都可以访问到
//用这种方式比较方便遍历
Car[] car = {new PassangerCar("aodiA4",500,4),
 new PassangerCar("mazida6",400,4),
 new Pickup("pikaxue",450,4,2),
 new PassangerCar("jinlong",800,20),
 new Truck("songhj",400,4),
 new Truck("yiweike",1000,20)};

public static void main(String[] args){
System.out.println("welcom to the car rent system:");
System.out.println("do you want to rent car:1YES 0NO");
Scanner scanner = new Scanner(System.in);
if(1 == scanner.nextInt()){
System.out.println("the cars that you can choose and their price:");
System.out.println("number"+"\t"+"name"+"\t\t"+"price"+"\t\t"+"capacity");
Initial initial = new Initial();
initial.printPrice();    //打印出车辆的信息
initial.getInfo();       //指示用户选择车辆及天数
initial.printBill();     //打印统计出的总人数,总吨数,价格等
 
}
else{
System.out.println("bye bye");
}
scanner.close();
 
}
 
public void printPrice(){
for(int i=0,j=1; i<6; i++,j++){
//根据引用属于的类来分别打印出车辆信息
if(car[i] instanceof PassangerCar){
System.out.println(j+"."+"\t"+car[i].getName()+"\t\t"+car[i].getRent()+"/DAY"+"\t"+car[i].getPeople()+"People");
}
else if(car[i] instanceof Pickup){
System.out.println(j+"."+"\t"+car[i].getName()+"\t\t"+car[i].getRent()+"/DAY"+"\t"+car[i].getPeople()+"People "+car[i].getGoods()+"T");
}
else if(car[i] instanceof Truck){
System.out.println(j+"."+"\t"+car[i].getName()+"\t\t"+car[i].getRent()+"/DAY"+"\t"+car[i].getGoods()+"T");
}
}
}
 
public void getInfo(){
System.out.println("please enter the total amount that you want:");
Scanner scanner = new Scanner(System.in);
int amount = scanner.nextInt();
for(int i=1; i<=amount; i++){
System.out.println("please enter the "+i+" car:");
int number = scanner.nextInt();
if(number>0 && number<=6){
count[number-1]++;
}
else{
System.out.println("number is invalid");
}
}
System.out.println("please enter the days:");
days = scanner.nextInt();
scanner.close();
}
 
public void printBill(){
System.out.println("your bill:");
System.out.println("***the cars that can manned people are:");
for(int i=0; i<6; i++){
if(count[i]>0){
if(car[i].getPeople()>0){
System.out.print(car[i].getName()+"  ");
total_People += count[i] * car[i].getPeople();
}
}
}
System.out.println("total_People:"+total_People);
System.out.println("***the cars that can carry cargo are:");
for(int i=0; i<6; i++){
if(count[i]>0){
if(car[i].getGoods()>0){
System.out.print(car[i].getName()+"  ");
total_goods += count[i] * car[i].getGoods();
}
total_price += count[i] * car[i].getRent();
}
}
total_price *= days;
System.out.println("total_goods:"+total_goods);
System.out.println("***the total_price is:"+total_price);
}

}
1,父类car不是用public修饰成员变量吗?那么get. set方法还有啥意义?
2,子类里面这个this啥意义,跟父类里面的this不一样吧?
后面的也看的稀里糊涂的

解决方案 »

  1.   


    1,确实没有封装,set和get意义不是很大,这个类写的不规范。
    2. this表示当前类。子类中的this表示子类的当前实例,父类的this表示父类的当前实例。看不懂的,多看看就熟了,基础要打牢靠。
      

  2.   


    1,确实没有封装,set和get意义不是很大,这个类写的不规范。
    2. this表示当前类。子类中的this表示子类的当前实例,父类的this表示父类的当前实例。看不懂的,多看看就熟了,基础要打牢靠。
    谢谢。。
      

  3.   

    再问下,父类里面的this. name,跟子类里面的this. name没有联系吧,还有子类参数name跟这俩name有啥区别吗?
      

  4.   


    1,确实没有封装,set和get意义不是很大,这个类写的不规范。
    2. this表示当前类。子类中的this表示子类的当前实例,父类的this表示父类的当前实例。看不懂的,多看看就熟了,基础要打牢靠。
    再问下,父类里面的this. name,跟子类里面的this. name没有联系吧,还有子类参数name跟这俩name有啥区别吗?
      

  5.   


    这里情况特殊,因为子类没有设定新的变量,所以这里子类中的this.name其实也就是父类中的name变量。规范的写法,父类的变量需要加私有属性,子类构造通过set方法来设定父类变量。这个程序看懂就行了,不用深究,没有什么意义。参数name为了让读程序的人能看懂数据的传递关系,所以也用name命名。参数名起其他名字也不会影响程序的正常运行。