Assignment Operator Overloading Java

我们两清 提交于 2020-02-05 07:08:25

问题


Im having trouble figuring out how to implement the equivalent of overloading the assignment operator in C++ to Java. I know there is no such thing, but I need to simulate it. I've tried overriding the Clone() function, but no luck. Any ideas?

Below is my main

 Queue p = new Queue();
 Queue q = new Queue();

    p.enqueue('a');
    p.enqueue(9);
    p.enqueue(10);
    p.enqueue(310);
    p.enqueue(8);

    q = p;
    System.out.print(p);

And here is the clone function

public void Clone(Queue other) throws Throwable
{
    System.out.println("test\n");

    if(this == other)
    {

    }
    else
    {            
while(!isEmpty())
  dequeue();

Node tmp = other.head;
while(tmp != null){
    this.enqueue((T)tmp.element);
    tmp = tmp.next;

}   
    }

}

回答1:


Operator overloading is Not supported by Java.

Operator overloading violates one of the central tenets of the Java language design : transparency. It is against the language philosophy trying to overload an operator and should be avoided ...




回答2:


You're going to need to write an explicit copy constructor. In Java, everything's a pointer (or "reference" as they call them), so when you assign p to q, you're just saying that p and q point to the same object.

q = new Queue(p)

Is what you want, assuming you've implemented the appropriate constructor.

EDIT: What you're proposing is just not possible with the syntax "q = p". If you want to "simulate" the assignment operator, then you're going to need to write a new method in Queue. For example, here's how Java programmers "simulate" overloading the addition operator:

a.add(b);

So in your case you'll need to write an "assign" method that takes a Queue, copies its data, and assigns these copies to the internal data of the other object.

q.assign(p);

public void assign(Queue other) {
  this.primitive = other.primitive;
  this.someObject = new SomeObject(other.someObject);
}

That's about as close as you're going to get.




回答3:


Operator overloading cannot be done in Java. It requires support from the compiler to do just that. You can however create a preprocessor that understands java syntax and run it before building, that converts the call of your operator to an appropriate function but that is just overkill.

clone is not not related to operator overloading.




回答4:


There is no "equivalent" because you cannot overload the assignment operator in Java. You're attempting to imitate the behavior with the clone() method, but you've got it backwards. Without operator overloading, you have three options:

  1. Write a function that mutates the invoking object to become a copy of the one passed in.
  2. Write a function that makes a copy of the invoking object and returns it to something else that will store it.
  3. Use a copy constructor.

1. Write a function that mutates the invoking object to become a copy of the one passed in.

Note that this is identical to the assignment operator overload, except it's named "copy" instead of "operator=".

If you want to do this:

foo A;
foo B;
A.copy(B);  // A is now a copy of B.

In C++:

foo& copy(const foo& rhs)
{
    if(&rhs != this)
    {
        this->m_someInt = rhs.m_someInt;
        this->m_someBar = rhs.m_someBar;
    }
    return *this;
}

In Java:

foo copy(foo rhs)
{
    this.m_someInt = rhs.m_someInt;
    this.m_someBar.copy(rhs.m_someBar);
    return this;
}

Note that in C++, assigning this->m_someBar to rhs.m_someBarcopies rhs.m_someBar by value, but in Java such an assignment would cause both foo objects to share the same Bar object. Therefore, a similar copying mechanism must be used on your subojects as well if you don't intend for them to be shared.

2. Write a function that makes a copy of the invoking object and returns it to something else that will store it.

If you want to do this:

foo A;
foo B;
A = B.clone();  // A is now a copy of B.

In C++:

foo clone() const  // Ignoring the fact that this is unnecessary in C++ where assignments (and returns) are by-value anyway.
{
    foo theCopy;
    theCopy.m_someInt = this->m_someInt;
    theCopy.m_someBar = this->m_someBar;
    return theCopy;
}

In Java:

foo clone()
{
    foo theCopy = new foo();
    theCopy.m_someInt = this.m_someInt;
    theCopy.m_someBar = this.m_someBar.clone();
    return theCopy;
}

3. Use a copy constructor.

If you want to do this:

foo B;
foo A = new foo(B);  // Java version
foo A(B); // C++ version

In C++:

foo(const foo& rhs)
  :m_someInt(rhs.m_someInt), m_someBar(rhs.m_someBar)
{
}

// Or optionally:
foo(const foo& rhs) = default;

In Java:

foo(foo rhs)
{
    m_someInt = rhs.m_someInt;
    m_someBar = new Bar(rhs.m_someBar);
}

In conclusion, your issue is that you're trying to use clone() in the same way I use copy() above. The closest to an equivalent would be my copy method. My personal suggestion is to just use a copy constructor and call it a day.




回答5:


If you're trying to override the clone method on Object, it needs to have the same signature (and capitalization). Your method signature is Clone(Queue).

It's not really clear how this relates to trying to simulate operator overloading though, or what operator you're trying to overload...




回答6:


In case you are still interested in operator overloading in java environment, try Groovy. It runs in JVM




回答7:


I'm from a C++ background so I know where you are coming from. I think it is the clone method you are after. Don't forget to implement the Cloneable interface to tell 'Object' that this object is a cloneable object. See Effective Java (2nd Ed) item 11 for a thorough explanation.



来源:https://stackoverflow.com/questions/15129284/assignment-operator-overloading-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!