Operator overloading in Java

前端 未结 10 1762
独厮守ぢ
独厮守ぢ 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:33

    Just use Xtend along with your Java code. It supports Operator Overloading:

        package com.example;
    
    @SuppressWarnings("all")
    public class Test {
      protected int wrapped;
    
      public Test(final int value) {
        this.wrapped = value;
      }
    
      public int operator_plus(final Test e2) {
        return (this.wrapped + e2.wrapped);
      }
    }
    
    package com.example
    
    class Test2 {
    
        new() {
            val t1 = new Test(3)
            val t2 = new Test(5)
            val t3 = t1 + t2
        }
    
    }
    

    On the official website, there is a list of the methods to implement for each operator !

提交回复
热议问题