In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "how to use the interface to achieve a train ticket example in Java". The content is simple and clear. I hope it can help you solve your doubts. Let the editor lead you to study and learn the article "how to use the interface to realize the train ticket instance in Java".
The practical application of / * * interface: topic: two people A Magi B develop a train ticket display function An is responsible for the client, B is responsible for the server: 1. Abstract class: 1.1 client display class (Terminal) 1.2 server service class (TrainService) 1.3 train ticket (Ticket) 2. Find out the class relationship: 2.1client side calls server side to get train ticket information display TrainService-> Terminal (call) 2.2.The server side provides train ticket information to client side Ticket- > TrainService (many to 1) Ticket-> Terminal (many to many) 3. Find out the class attribute: 3. 1 Ticket (train, departure, arrival, fare) 3. 2 TrainService (name of railway station) 3. 3 Terminal (name of consignment point) 4. Find the class method: 4.1 TrainService: provide train ticket information Ticket [] getTickets () 4.2 Terminal: display train ticket information display () * / import java.util.Scanner Public class ApplyInterface {public static void main (String [] args) {/ / Beijing West Railway Station train ticket server opens TrainService beijingWestService = new BeijingWestTrainService ("Beijing West Railway Station"); / / Beijing Railway Station train ticket server opens TrainService beijingService = new BeijingTrainService ("Beijing Railway Station") / / I am Terminal t = new Terminal of Dahuangzhuang, Chaoyang District, Beijing; Scanner in = new Scanner (System.in); System.out.println ("Please enter the railway station code 1: Beijing West Railway Station 2: Beijing Railway Station"); int trainId = in.nextInt () If (trainId = = 1) {t.setTrainService (beijingWestService);} if (trainId = = 2) {t.setTrainService (beijingService);} / / Open the train computer terminal to view the train ticket information t.display () }} class Ticket {private String id;// departure city private String endCity;// arrives city private float price;// fare 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 {/ / An and B agree on a method to provide train tickets public Ticket [] getTickets (); / / get the name of the railway station public String getName ();} class BeijingWestTrainService implements TrainService {private String name; public BeijingWestTrainService (String name) {this.name = name;} public String getName () {return this.name } / / implementation method for providing train tickets public Ticket [] getTickets () {Ticket [] tickets = {new Ticket ("T1", "Beijing", "Changsha", 175f)}; return tickets;}} class BeijingTrainService implements TrainService {private String name; public BeijingTrainService (String name) {this.name = name } public String getName () {return this.name;} / / implementation method of providing train tickets public Ticket [] getTickets () {Ticket [] tickets = {new Ticket ("T215", "Beijing", "Jilin", 130f)}; return tickets;}} class Terminal {private String proxy / / Agent name private Ticket [] tickets;// train ticket reference private TrainService trainService;// server interface reference public Terminal (String proxy) {this.proxy = proxy;} public void setTrainService (TrainService trainService) {this.trainService = trainService } / / display train ticket information / / idea: / / 1. First of all, the train ticket information should be obtained from the server / / 2. Cycle to display train ticket information on the computer terminal public void display () {/ / obtain train ticket information tickets = trainService.getTickets (); / / output train ticket information System.out.println (trainService.getName ()) System.out.println ("train-departure-arrival-fare"); if (tickets! = null) {for (int I = 0; I < tickets.length) System.out.println (tickets.getId () + "-" + tickets.getStartCity () + "-" + tickets.getEndCity () + "-" + tickets.getPrice ()) }} these are all the contents of the article "how to use the interface to implement a train ticket instance in Java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.