Why doesn't Java offer operator overloading?

前端 未结 17 2187
梦谈多话
梦谈多话 2020-11-22 07:38

Coming from C++ to Java, the obvious unanswered question is why didn\'t Java include operator overloading?

Isn\'t Complex a, b, c; a = b + c; much simpl

17条回答
  •  感情败类
    2020-11-22 08:31

    This is not a good reason to disallow it but a practical one:

    People do not always use it responsibly. Look at this example from the Python library scapy:

    >>> IP()
    
    >>> IP()/TCP()
    >
    >>> Ether()/IP()/TCP()
    >>
    >>> IP()/TCP()/"GET / HTTP/1.0\r\n\r\n"
    >>
    >>> Ether()/IP()/IP()/UDP()
    >>>
    >>> IP(proto=55)/TCP()
    >
    

    Here is the explanation:

    The / operator has been used as a composition operator between two layers. When doing so, the lower layer can have one or more of its defaults fields overloaded according to the upper layer. (You still can give the value you want). A string can be used as a raw layer.

提交回复
热议问题