1. Simulation stack
PackageExercise;Importjava.util.ArrayList;Importjava.util.Arrays;Importjava.util.Collection;Importjava.util.Collections;ImportJava.util.Scanner; Public classTest { Public Static voidMain (string[] args) {ArrayList<Object> list =NewArraylist<>(); Test a=NewTest (list); System.out.print ("is the steak empty?"); System.out.println (A.isempty ()); A.push (2); A.push (3); A.push (5); System.out.print ("The size of the steak is"); System.out.println (A.getsize ()); System.out.print ("is the steak empty?"); System.out.println (A.isempty ()); System.out.print ("Get the Peek number"); System.out.println (A.peek ()); System.out.print ("Pop the Peek and return it"); System.out.println (A.pop ()); System.out.print ("Now, the peak is"); System.out.println (A.peek ()); System.out.print ("The size of the steak"); System.out.println (A.getsize ()); System.out.print ("List the Numbers"); System.out.println (A.tostring ()); } Test (ArrayList<Object>list) { This. List =list; } //class test simulation stack PrivateArraylist<object>list; Public BooleanIsEmpty () {returnList.isEmpty (); } Public intGetSize () {returnlist.size (); } PublicObject Peek () {returnList.get (List.size ()-1); } PublicObject Pop () {object o= List.get (List.size ()-1); List.remove (List.size ()-1); returno; } Public voidpush (Object i) {list.add (i); } PublicString toString () {return"The steak is" +list.tostring (); }}
The output is as follows
True3 is thesteak emptyfalse5return it 53 2List The numbers the steak is [2, 3]
2. Object as an example of a member variable, clock.
PackageExercise; Public classTimes {Private intlimit; Private intnum = 0; Times (intlimit) { This. Limit =limit; } intGetlimit () {returnlimit; } intGetnum () {returnnum; } voidtick () {num++; if(num = =limit) Num= 0; } voidShow () { while(true) {tick (); SYSTEM.OUT.PRINTLN (num); } } PublicString toString () {if(Num < 10) return"0" +num; Else return"" +num; } Public Static voidMain (string[] args) {//TODO auto-generated Method StubTimes T =NewTimes (24); T.show (); }}
This function defines how the numbers bounce. Next, refer to the two times object as a variable in another class
1 PackageExercise;2 3 Public classShowTime {4 PrivateTimes hours =NewTimes (24);5 PrivateTimes minutes =NewTimes (60);6 7 voidShow () {8 while(true) {9 Minutes.tick ();Ten if(Minutes.getnum () = = 0) One Hours.tick (); A //System.out.printf ("%02d:%02d\n", Hours.getnum () - //, Minutes.getnum ()); -System.out.println (Hours + ":" +minutes); the } - - } - + Public Static voidMain (string[] args) { - //TODO auto-generated Method Stub +ShowTime st =NewShowTime (); A st.show (); at //String s = "123"; - //Integer i = n; - //s=i+ ""; - //System.out.println (s); - } - in}
In line 14th, the ToString function in times is called directly with hours and minutes, because hours+ ":" The compiler determines that the default hours is a string, so call it directly.
2016/5/30 Study Record