Define the static domain NextID and a static method Getnextid, write the three employee objects to the array, and then print the employee information. Finally, print out a usable employee identification code to show the static method.
PackageCompany ; Public classGettext { Public Static voidMain (string[] args) {//TODO Auto-generated method stubsemployee[] Staff =NewEmployee[3]; staff[0] =NewEmployee ("Tom", 40000); staff[1] =NewEmployee ("Dick", 60000); staff[2] =NewEmployee ("Harry", 65000); for(Employee e:staff) {E.setid (); System.out.println ("Name=" + e.getname () + "id=" + e.getid () + "salary=" +e.getsalary ()); } intn =Employee.getnextid (); System.out.println ("Next Available id =" +N); }}
PackageCompany ; Public classEmployee {Private Static intNextID = 1; PrivateString name; Private Doublesalary; Private intID; PublicEmployee (String name,Doublesalary) { This. Name =name; This. Salary =salary; ID= 0; } PublicString GetName () {returnname; } Public Doublegetsalary () {returnsalary; } Public intgetId () {returnID; } Public voidsetId () {ID=NextID; NextID++; } Public Static intGetnextid () {returnNextID; } Public Static voidMain (string[] args) {//For independent testing of employee classes//TODO Auto-generated method stubsEmployee e =NewEmployee ("Herry", 50000); System.out.println (E.getname ()+ " " +e.getsalary ()); }}
Program Run Result:
Name= Tom id= 1 salary=40000.0
Name= Dick id= 2 salary=60000.0
Name= Harry id= 3 salary=65000.0
Next Available id = 4
Java programming Basics Object-oriented practice static factory method