Java polymorphism, interface, dynamic loading class, etc.

Source: Internet
Author: User

Write a general program for a research institute to calculate the time required for each vehicle to run 1000 kilometers, and the parameters of each vehicle are known as 3 integers a, B, and C expressions. There are two kinds of tools: car and plane, where the speed of the car is the formula: A*b/c,plane's speed Operation formula is: A+b+c. Three categories need to be written: Computetime.java,plane.java,car007.java and interface Common.java, requiring that, in the future, if the 3rd vehicle is to be added, no prior procedures need to be modified, and only new means of transport should be prepared. It runs as follows, entering Computetime's four parameters from the command line, the first is the type of vehicle, the 第二、三、四个 parameters are integers a, B, and C respectively, for example:

Calculate Plane time: "Java computetime Plane 20 30 40"

Calculate Car007 time: "Java computetime Car007 23 34 45"

If the 3rd means of transport is ship, you only need to write Ship.java, run-time input: "java computetime shipping 22 33 44"

Tip: Make full use of the concept of interface, the interface object acts as a parameter.

Another way to instantiate an object: Class.forName (str). newinstance (); For example, if you need to instantiate a Plane object, just call Class.forName ("Plane"). Newinstance ().

Answer code:

To visit the classpath path, please compile from the bottom up

目录结构
CalTime
--------|--------
  |        |
  |        |
vehicle    ComputTime.java
  |
---------
|   |
|   |
all Palne.java/Car.java
|
|

Common.java

The code is as follows:

1.computtime.java Please make sure the input is correct, which does not capture numberfromatexception

Import CalTime.vehicle.all.Common;
Import java.lang.*;
public class Computetime {
public static void Main (String args[]) {
System.out.println ("means of transport:" +args[0]);
SYSTEM.OUT.PRINTLN ("parameter A:" +args[1]);
SYSTEM.OUT.PRINTLN ("parameter B:" +args[2]);
SYSTEM.OUT.PRINTLN ("parameter C:" +args[3]);
Double a=double.parsedouble (args[1]);
Double b=double.parsedouble (args[2]);
Double c=double.parsedouble (args[3]);
Double v,t;
try {
Common d= (Common) class.forname ("Caltime.vehicle.") +args[0]). newinstance ();
V=d.runtimer (A,B,C);
t=1000/v;
System.out.println ("Average speed:" +v+ "km/h");
System.out.println ("Run Time:" +t+ "hours");
catch (Exception e) {
SYSTEM.OUT.PRINTLN ("Class not Found");
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.