蟹皇面plus
2018-11-07 21:06
//抽象类 public abstract class Car { int num; String name; int rent; int capacity; int capacity2; } //载人的车类 public void SetAll(int Vnum,String Vname,int Vrent,int Vcapacity){ num=Vnum; name=Vname; capacity=Vcapacity; rent=Vrent; } public void PrintAll(){ System.out.println(num+".\t\t"+name+"\t\t"+rent+"元/天\t\t载人:"+capacity+"人"); } } //载货车类 public class Coach extends Car { public void SetAll(int Cnum,String Cname,int Crent,int Ccapacity){ num=Cnum; name=Cname; capacity=Ccapacity; rent=Crent; } public void PrintAll(){ System.out.println(num+".\t\t"+name+"\t\t"+rent+"元/天\t\t载货:"+capacity+"吨"); } } //能载人也能载货类 public class Pika extends Car{ public void SetAll(int Pnum,String Pname,int Prent,int Pcapacity1,int Pcapacity2){ num=Pnum; name=Pname; capacity=Pcapacity1; capacity2=Pcapacity2; rent=Prent; } public void PrintAll(){ System.out.println(num+".\t\t"+name+"\t\t"+rent+"元/天\t\t载人:"+capacity+"人"+" 载货:"+capacity2+"吨"); } } //main函数 import java.util.Scanner; public class Dada { public static void main(String[] args){ System.out.println("欢迎使用哒哒租车系统:"); System.out.println("您是否要租车:1.是 0.否"); Scanner scan=new Scanner(System.in); int k=scan.nextInt();//获取控制台上用户输入的数字 if(k==1){ System.out.println("您可租车的类型及价目表:"); System.out.println("序号\t\t汽车名称\t\t租金\t\t容量"); Car Aodi=new Van();//创建并显示可租车的对象 ((Van) Aodi).SetAll(1,"奥迪A4",500,4); ((Van) Aodi).PrintAll(); Car Mazida=new Van(); ((Van) Mazida).SetAll(2,"马自达6",400,4); ((Van) Mazida).PrintAll(); Car Xue=new Pika(); ((Pika) Xue).SetAll(3,"皮卡雪6",450,4,2); ((Pika) Xue).PrintAll(); Car Jin=new Van(); ((Van) Jin).SetAll(4,"金龙",800,20); ((Van) Jin).PrintAll(); Car Song=new Coach(); ((Coach) Song).SetAll(5,"松花江",400,4); ((Coach) Song).PrintAll(); Car Yi=new Coach(); ((Coach) Yi).SetAll(6,"依维柯",1000,20); ((Coach) Yi).PrintAll(); System.out.println("请输入您要租汽车的数量:"); int i=scan.nextInt(); int Choose,sum=0,rent1=0,CarDate,manned=0,shipment=0,Allsum=0,Allmanned=0,Allshipment=0;//分别为用户选择的车型,单次金额,租车单价,租车天数,载人量,载货量和总金额 for(int j=1;j<=i;j++){ System.out.println("请选择您要租的第"+j+"辆车的序号"); Choose=scan.nextInt(); if (Choose<1||Choose>6) System.out.println("请输入正确的序号:"); else { switch (Choose) { case 1: rent1 = Aodi.rent; manned = Aodi.capacity; shipment = 0; break; case 2: rent1 = Mazida.rent; manned = Mazida.capacity; shipment = 0; break; case 3: rent1 = Xue.rent; manned = Xue.capacity; shipment = Xue.capacity2; break; case 4: rent1 = Jin.rent; manned = Jin.capacity; shipment = 0; break; case 5: rent1 = Song.rent; manned = 0; shipment = Song.capacity; break; case 6: rent1 = Yi.rent; manned = 0; shipment = Yi.capacity; break; } Allmanned+=manned; Allshipment+=shipment; System.out.println("请输入租的天数:"); CarDate=scan.nextInt(); sum=rent1*CarDate; Allsum+=sum; System.out.println("当前载人数量为"+Allmanned+"个,载货数量为"+Allshipment+"吨"); System.out.println("您需要付的金额为:"+Allsum); } } }else{ System.out.println("Bye"); System.exit(0); } } //主要觉得冗长的就是main函数部分,原本想把显示可租车辆和选择车辆写成方法来使用,结果发现在另一个方法中创建的对象在main函数中无法使用,也没想到解决的方法,希望有大佬帮忙!
都不想回答了,没有认真学好基础。
抽象类 ,一般都要有一些抽象方法,你的抽象类根本就没有方法,SetAll,PrintAll不就可以直接变为抽象方法吗,后面还需要些一堆这样的转换吗?
((Van) Aodi)
公有public,私有private,继承 protected 对属性不知道该怎么用,直接默认...,实际项目中很少用默认的。
main中的创建那么多车 应该用for循环,数据可以使用数组,结构体之类的。
java方法的属性一般不暴露给外部使用,一般使用方法getXXX()获取对应的属性值。
先自己修改下
import java.util.Scanner;
public class leiming{
static int a;
public static void fangfa(){
Scanner b=new Scanner(System.in);
int c=b.nextInt();
leiming.a=c;
}}
public static void main(String[] args){
leiming.fangfa();
System.out.println(leiming.a);}
希望对你有所帮助!
Java入门第二季 升级版
530559 学习 · 6091 问题
相似问题