Building Java applications
In this section, you continue building up
Person
as a Java application. Along the way, you can get a better idea of how an object, or collection of objects, evolves into an application.The application entry point
All Java applications require a section point where the Java runtime knows to begin executing code. That passage point is the principle() strategy. Area objects — that is, articles (Person and Employee, for instance) that are a piece of your application's business space—ordinarily don't have primary() strategies, yet no less than one class in each application must.
As you most likely are aware, Person and its Employee subclass are theoretically some portion of a HR application. Presently you'll add another class to the application to give it a section point.
Creating a driver class
The purpose of a driver class (as its name implies) is to "drive" an application. Notice that this simple driver for a human-resources application contains a
main()
method:package com.makotojava.intro; public class HumanResourcesApplication { public static void main(String[] args) { } } |
Now, create a driver class in Eclipse using the same procedure you used to create
Person
and Employee
. Name the class HumanResourcesApplication
, being sure to select the option to add a main()
method to the class. Eclipse will generate the class for you.
Next, add some code to your new
main()
method so that it looks like this:package com.makotojava.intro; import java.util.logging.Logger; public class HumanResourcesApplication { private static final Logger log = Logger.getLogger(HumanResourcesApplication.class.getName()); public static void main(String[] args) { Employee e = new Employee(); e.setName("J Smith"); e.setEmployeeNumber("0001"); e.setTaxpayerIdentificationNumber("123-45-6789"); e.setSalary(BigDecimal.valueOf(45000.0)); e.printAudit(log); } } |
Finally, launch the
HumanResourcesApplication
class and watch it run. You should see this output:Sep 19, 2015 7:59:37 PM com.makotojava.intro.Person printAudit INFO: Name=J Smith,Age=0,Height=0,Weight=0,EyeColor=null,Gender=null TaxpayerIdentificationNumber=123-45-6789,EmployeeNumber=0001,Salary=45000.00 |
That's all there is to creating a simple Java application. In the next section, you begin looking at some of the syntax and libraries that can help you develop more-complex applications.
0 coment�rios:
Post a Comment