本渣渣鸽了一个月终于有时间更新。因为有c++基础,学起来这章还是比较简单的,本章我觉得是程序猿质变课程,理解面向对象的思想,掌握面向对象的基本原则以及 Java 面向对象编程基本实现原理,熟练使用封装、继承、多态面向对象三大特性,感受和c++之间的异同,下面是学习路线。
1.类和对象
2. 封装
3.继承
4.多态
再配上一个比较综合练习题,编写一个控制台版的租车系统。
1.展示所有可租车辆
2.选择车型,租车量,租车天数
3.显示租车清单,包括总金额,总载货量,以及总计价格
最后附上我自己的源码
package 其其租车系统; import java.util.Scanner; import javax.print.DocFlavor.INPUT_STREAM; public class Test { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input=new Scanner(System.in); Smallcar c1=new Smallcar(); int smallcarmoney[]=new int[10]; int smallcarpeople[]=new int[10]; Bus c2=new Bus(); int busmoney[]=new int[10]; int buspeople[]=new int[10]; Pika c3=new Pika(); int pikamoney[]=new int[10]; int pikapeople[]=new int[10]; int pikagoods[]=new int[10]; Lorry c4=new Lorry(); int lorrymoney[]=new int[10]; int lorrygoods[]=new int[10]; System.out.println("欢迎使用其其租车系统"); System.out.println("1.租车 2.退出"); int num1=input.nextInt(); switch (num1) { case 1: System.out.println("车型如下"); System.out.println("1.小车 载客:4人 拉货0kg 租金:100圆/天"); System.out.println("2.巴士 载客:30人 拉货0kg 租金:500圆/天"); System.out.println("3.皮卡 载客:4人 拉货200kg 租金:200圆/天"); System.out.println("4.卡车 载客:0人 拉货2000kg 租金:500圆/天"); System.out.println("请输入一共要租几辆车"); int num2=input.nextInt(); for(int i=1;i<=num2;i++){ System.out.println("请选择第"+i+"辆车型号"); int a=input.nextInt(); switch (a) { case 1: smallcarpeople[i]=c1.fun2(); smallcarmoney[i]=c1.money(); break; case 2: buspeople[i]=c2.fun2(); busmoney[i]=c2.money(); break; case 3: pikapeople[i]=c3.fun2(); pikamoney[i]=c3.money(); pikagoods[i]=c3.func1(); break; case 4: lorrymoney[i]=c4.money(); lorrygoods[i]=c4.func1(); break; default: System.out.println("选择车型不存在"); break; } } int money1=0; for(int a:smallcarmoney){ money1+= a; } int money2=0; for(int b:busmoney){ money2+= b; } int money3=0; for(int c:lorrymoney){ money3+= c; } int money4=0; for(int d:pikamoney){ money4+=d; } int money=money1+money2+money3+money4; int people1=0; for(int e:buspeople){ people1+=e; } int people2=0; for(int f:smallcarpeople){ people2+=f; } int people3=0; for(int g:pikapeople){ people3+=g; } int people=people1+people2+people3; int goods1=0; for(int h:lorrygoods){ goods1+=h; } int goods2=0; for(int j:pikagoods){ goods2+=j; } int good=goods1+goods2; System.out.println("共可载"+people+"个人"); System.out.println(" 共可载"+good+"kg货物"); System.out.println("请输入租车天数"); int days=input.nextInt(); System.out.println("合计费用"+days*money+"圆"); break; case 2: System.out.println("欢迎下次光临"); break; default: System.out.println("输入错误"); break; } } }
package 其其租车系统; public abstract class Car { public abstract int money(); }
package 其其租车系统; public interface carrygoods { int func1(); }
package 其其租车系统; public interface carrypeople { int fun2(); }
package 其其租车系统; public class Smallcar extends Car implements carrypeople{ public int money() { // TODO Auto-generated method stub return 100; } @Override public int fun2() { // TODO Auto-generated method stub return 4; } }
package 其其租车系统; public class Lorry extends Car implements carrygoods { public int money() { // TODO Auto-generated method stub return 500; } @Override public int func1() { // TODO Auto-generated method stub return 2000; } }
package 其其租车系统; public class Pika extends Car implements carrygoods,carrypeople { public int money() { // TODO Auto-generated method stub return 200; } @Override public int fun2() { // TODO Auto-generated method stub return 4 ; } @Override public int func1() { // TODO Auto-generated method stub return 200; } }
package 其其租车系统; public class Bus extends Car implements carrypeople{ public int money() { // TODO Auto-generated method stub return 500; } @Override public int fun2() { // TODO Auto-generated method stub return 30; } }
附上运行效果图
时间: 2024-10-10 02:39:17