AkramAbd

8.4 Referring to the Current Object's Members with the this

Feb 27th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.91 KB | None | 0 0
  1. //ThisTest.java
  2. //this used implicity and explicity to refer to members of an object.
  3.  
  4. public class ThisTest
  5. {
  6.     public static void main (String[] args)
  7.     {
  8.         SimpleTime time = new SimpleTime(15, 30, 19);
  9.         System.out.println(time.buildString());
  10.     }
  11. }
  12.  
  13. class SimpleTime
  14. {
  15.     private int hour;
  16.     private int minute;
  17.     private int second;
  18.    
  19.     public SimpleTime(int hour, int minute, int second)
  20.     {
  21.         this.hour=hour;
  22.         this.minute=minute;
  23.         this.second=second;
  24.     }
  25.    
  26.     public String buildString()
  27.     {
  28.         return String.format("%24s: %s\n%24s: %s",
  29.             "this.toUniversalString()", this.toUniversalString(),
  30.             "toUniversalString()", toUniversalString());
  31.     }
  32.    
  33.     public String toUniversalString()
  34.     {
  35.         return String.format( "%02d:%02d:%02d",
  36.             this.hour, this.minute, this.second);
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment