这篇文章主要为大家展示了“Java中如何使用接口实现火车票实例”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Java中如何使用接口实现火车票实例”这篇文章吧。import java.u
这篇文章主要为大家展示了“Java中如何使用接口实现火车票实例”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Java中如何使用接口实现火车票实例”这篇文章吧。
import java.util.Scanner;public class ApplyInterface {public static void main(String[] args) {//北京西站火车票服务器打开TrainService beijingWestService = new BeijingWestTrainService("北京西站");//北京站火车票服务器打开TrainService beijingService = new BeijingTrainService("北京站");//我是北京朝阳区大黄庄的 代售点Terminal t = new Terminal("北京朝阳区大黄庄的 代售点");Scanner in = new Scanner(System.in);System.out.println("请输入要查的火车站代码 1:北京西站 2:北京站");int trainId = in.nextInt();if (trainId == 1) {t.setTrainService(beijingWestService);}if (trainId == 2) {t.setTrainService(beijingService);}//打开火车电脑终端 查看火车票信息t.display();}}class Ticket {private String id;//车次private String startCity;//出发城市private String endCity;//到达城市private float price;//票价public Ticket(String id, String startCity, String endCity, float price) {this.id = id;this.startCity = startCity;this.endCity = endCity;this.price = price;}public String getId() {return this.id;}public String getStartCity() {return this.startCity;}public String getEndCity() {return this.endCity;}public float getPrice() {return this.price;}}interface TrainService {//A和B约定一个提供火车票的方法public Ticket[] getTickets();//获取火车站名字public String getName();}class BeijingWestTrainService implements TrainService {private String name;public BeijingWestTrainService(String name) {this.name = name;}public String getName() {return this.name;}//提供火车票的实现方法public Ticket[] getTickets() {Ticket[] tickets = { new Ticket("T1", "北京", "长沙", 175f) };return tickets;}}class BeijingTrainService implements TrainService {private String name;public BeijingTrainService(String name) {this.name = name;}public String getName() {return this.name;}//提供火车票的实现方法public Ticket[] getTickets() {Ticket[] tickets = { new Ticket("T215", "北京", "吉林", 130f) };return tickets;}}class Terminal {private String proxy;//代售点名称private Ticket[] tickets;// 火车票引用private TrainService trainService;// 服务器接口引用public Terminal(String proxy) {this.proxy = proxy;}public void setTrainService(TrainService trainService) {this.trainService = trainService;}//显示火车票的信息//思路://1. 首先要从服务器端 获取 火车票信息//2. 循环把火车票信息显示在电脑终端上public void display() {//获取火车票信息tickets = trainService.getTickets();//输出火车票信息System.out.println(trainService.getName());System.out.println("车次------出发地------到达地------票价");if (tickets != null) {for (int i = 0; i < tickets.length; i++) {System.out.println(tickets[i].getId() + "------" + tickets[i].getStartCity() + "------" + tickets[i].getEndCity() + "------" + tickets[i].getPrice());}}}}
以上是“Java中如何使用接口实现火车票实例”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注编程网精选频道!
--结束END--
本文标题: Java中如何使用接口实现火车票实例
本文链接: https://lsjlt.com/news/231509.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0