JAVA generic methods and static method generics

Source: Internet
Author: User

[Java]View Plaincopy
  1. /*
  2. Generic methods and static method generics
  3. generics defined by a generic class are valid throughout a class if used by methods
  4. When the object of a generic class is explicit about the specific type to manipulate, all types to be manipulated are fixed
  5. In order for different methods to manipulate different types and the type is uncertain
  6. Then you can define the generic on the method
  7. static method Generics:
  8. Static methods do not have access to generics defined on the class
  9. If the application data type for a static method operation is undefined, you can define the generic on the method
  10. */
  11. Class demo<t>
  12. {
  13. public void method (T t)
  14. {
  15. System.out.println ("method:" + t);
  16. }
  17. public static <W> void Staticmethod (w w)
  18. {
  19. System.out.println ("Staticmethod:" + W);
  20. }
  21. Public <K> Void Show (k k)
  22. {
  23. System.out.println ("Show:" + K);
  24. }
  25. Public <Q> void print (q q)
  26. {
  27. System.out.println ("Print:" + q);
  28. }
  29. }
  30. Class Testdemo
  31. {
  32. public static void Main (String [] arags)
  33. {
  34. Demo<double> d = new demo<double> ();
  35. D.method (3.4);
  36. D.staticmethod (true);
  37. D.show ("haha");
  38. D.print (new Integer (4));
  39. }
  40. }

JAVA generic methods and static method generics

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.