Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Fig. 8.4 : ThisTest.java
- //digunakan secara implisit dan eksplisit untuk merujuk ke object's members
- public class ThisTest
- {
- public static void main(String[] args)
- {
- SimpleTime time = new SimpleTime(15, 30, 19);
- System.out.println(time.buildString() );
- } //akhir main
- }//akhir class ThisTest
- //class SimpleTime mendemonstrasikan the "this" reference
- class SimpleTime
- {
- private int jam; //0-23
- private int menit; //0-59
- private int detik; //0-59
- // if the constructor uses parameter names identical to
- // instance variable names the "this" reference is
- // required to distinguish between the names
- public SimpleTime( int jam, int menit, int detik)
- {
- this.jam = jam; // set "this" object's jam
- this.menit = menit; // set "this" object's menit
- this.detik = detik; // set "this" object's detik
- }//akhir SimpleTime constructor
- //use explicit and implicit "this" to call toUniversalString
- public String buildString()
- {
- return String.format("%24s: %s\n%24s: %s",
- "this.toUniversalString()", this.toUniversalString(),
- "toUniversalString()", toUniversalString() );
- }//akhir method buildString
- //mengubah ke String pada format waktu Universal (JJ:MM:DD)
- public String toUniversalString()
- {
- // "this" is not required here to access instance variables,
- // because method does not have local variables with same
- // names as instance variables
- return String.format("%02d : %02d : %02d",
- this.jam, this.menit, this.detik);
- } // akhiir method toUniversalString
- }//akhir program
Advertisement
Add Comment
Please, Sign In to add comment