Operator overloading in Java

前端 未结 10 1760
独厮守ぢ
独厮守ぢ 2020-11-21 23:51

Please can you tell me if it is possible to overload operators in Java? If it is used anywhere in Java could you please tell me about it.

10条回答
  •  遇见更好的自我
    2020-11-22 00:47

    Or, you can make Java Groovy and just overload these functions to achieve what you want

    //plus() => for the + operator
    //multiply() => for the * operator
    //leftShift() = for the << operator
    // ... and so on ...
    
    class Fish {
        def leftShift(Fish fish) {
            print "You just << (left shifted) some fish "
        }
    }
    
    
    def fish = new Fish()
    def fish2 = new Fish()
    
    fish << fish2
    

    Who doesnt want to be/use groovy? :D

    No you cannot use the compiled groovy JARs in Java the same way. It still is a compiler error for Java.

提交回复
热议问题