Java-how to customize your own tool Library: mytools or myutil

Source: Internet
Author: User

After mastering the package knowledge, you can create your own tool library to reduce or eliminate repeated program code. For example, create an alias for system. Out. println () to reduce the input burden.

Package edu. foresthe. mytools;

Public class P {

Public static void RINT (string s ){

System. Out. Print (s );

}

Public static void rintln (string s ){

System. Out. println (s );

}

}

It can be guessed that the file is located at a classpath, followed by the Directory of edu/foresthe/mytools. After compilation, you can use the Import Statement to use the p. Class file on your system.

Import edu. foresthe. mytools .*;

Public class mytoolstest {

Public static void main (string [] ARGs ){

P. rintln ("available from now on !");

P. rintln ("" + 100); // force it to be a string

P. rintln ("" + 100l );

P. RINT ("" + 3.1415926 );

}

}

Additional topic: As long as all objects are placed in a string expression, they can be easily forcibly converted to a string expression. In the above example, the expression starting with an empty string is this method. However, if you call system. Out. println (100), it does not need to convert 100 to a string to work (rintln ). You can use the P class to achieve the same purpose through some additional overloading.

Congratulations, from now on, as long as you have implemented a new useful tool, you can add it to your own tool library.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.