- // Fig. 8.13: EmployeeTest.java
- //static member demonstration.
- public class EmployeeTest
- {
- public static void main( String[] args )
- {
- //show that count is 0 before creating Employees
- System.out.printf( "Empployees before instantiation: %d\n",
- Employee.getCount() );
- //create two Employees; count should be 2
- Employee e1 = new Employee( "Susan", "Baker" );
- Employee e2 = new Employee( "Bob", "Blue" );
- //show that coount is 2 after creating two Employees
- System.out.println( "\nEmployees after instantiation: ");
- System.out.printf( "via e1.getCount(): %d\n", e1.getCount() );
- System.out.printf( "via e2.getCount(): %d\n", e2.getCount() );
- System.out.printf( "via Employee.getCount(): %d\n",
- Employee.getCount() );
- // get names of Employess
- System.out.printf( "\nEmployee 1: %s %s\nEmployee 2: %s %s\n",
- e1.getFirstName(), e1.getlastname(),
- e2.getFirstName(), e2.getlastname() );
- // in this example, there is only one reference to each Employee,
- // so the following two statements indicate that these objects
- // are eligible for garbage collection
- e1 = null;
- e2 = null;
- } // end main
- } // end class EmployeeTest
Composition & Enumerations A. COMPOSITION Composition adalah dimana hubungan suatu object bergantung dengan objek lainnya. Berikut ini program class Date, class Employee dan class EmployeeTest. Source Code : Class Date // Fig. 8.7: Date.java // Date class declaration. public class Date { private int month; private int day; private int year; private static final int[] daysPerMonth = //days in each month {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //contruktor : call checkMonth to confirm proper value for month; //call checkDay to confirm proper value for day public Date (int theMonth, int theDay, int theYear) { month = checkMonth(theMonth); year = theYear; day = checkDay(theDay); System.out.printf ("Date object constructor for date %s\n", this); } //utility method to confirm proper month value private int checkMonth...
Komentar
Posting Komentar