A general addition calculation is suitable for all types. Note: There is no need to overload the method.

Source: Internet
Author: User

This method implements addition operations. The data on both sides of addition can be of the original type, encapsulation type of the original type, or even a class object.

If you have any ideas or are lacking in this algorithm, I 'd like to advise you.

 

For this reason, we first write a class for demonstration only, all of which are simple. And override the tostring method:

  1. Public Class B {
  2. Public String tostring (){
  3. Return "/" This is B /"";
  4. }
  5. }

 

The specific implementation of addition operations is as follows:

  1. Public static object CAL (Object O1, object O2 ){
  2. If (! (O1 instanceof number) |! (O2 instanceof number )){
  3. Return o1.tostring () + o2.tostring ();
  4. } Else {
  5. // For the type that generates the most suitable size, write it from the maximum to the minimum
  6. If (O1 instanceof double) | O2 instanceof double ){
  7. Return (New Double (o1.tostring (). doublevalue () + new double (o2.tostring (). doublevalue ());
  8. } Else if (O1 instanceof float) | (O2 instanceof float )){
  9. Return (new float (o1.tostring (). floatvalue () + new float (o2.tostring (). floatvalue ());
  10. } Else if (O1 instanceof long) | (O2 instanceof long )){
  11. Return (New Long (o1.tostring (). longvalue () + new long (o2.tostring (). longvalue ());
  12. } Else if (O1 instanceof integer) | (O2 instanceof integer )){
  13. Return (New INTEGER (o1.tostring (). intvalue () + new INTEGER (o2.tostring (). intvalue ());
  14. } Else if (O1 instanceof short) | (O2 instanceof short )){
  15. Return (new short (o1.tostring (). Empty value () + new short (o2.tostring (). Empty value ());
  16. } Else if (O1 instanceof byte) | (O2 instanceof byte )){
  17. Return (New byte (o1.tostring (). bytevalue () + new byte (o2.tostring (). bytevalue ());
  18. } Else {
  19. Return (New Double (o1.tostring (). doublevalue () + new double (o2.tostring (). doublevalue ());
  20. }
  21. }
  22. }

 

Perform a few test examples and output the results:

(11.2, new B () = 11.2 "This is B"
(7, new B () = 7 "This is B"
(New B (), 7) = "This is B" 7
(New B (), 11.2) = "This is B" 11.2
(New B (), new B () = "This is B" "This is B"
(11.2, 11.69874) = 22.89874
(11, 11.69874) = 22.69874
(11.69874, 11) = 22.69874
(11,7) = 18
Short A = 9990; (a, 7) = 9997
Float B = 10.2f; (B, 7) = 17.2
Byte c = 10; (C, 7) = 17
(11151515151510l, 7) = 11151515151517
String 56 and number 7 are added = 567
Number 7 and string 56 are added = 756
Character 7 and number 7 add = 77
Add character 7 and character 0 = 10
Add two strings 11 and 0 to 110

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.