Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
赋值运算符重载Java 我在如何实现C++中的赋值操作符重载的java问题上遇到了麻烦。我知道没有这样的东西,但我需要模拟它。我尝试重写Clone()函数,但没有成功。有什么想法吗_Java_Clone_Overloading_Variable Assignment_Operator Keyword - Fatal编程技术网

赋值运算符重载Java 我在如何实现C++中的赋值操作符重载的java问题上遇到了麻烦。我知道没有这样的东西,但我需要模拟它。我尝试重写Clone()函数,但没有成功。有什么想法吗

赋值运算符重载Java 我在如何实现C++中的赋值操作符重载的java问题上遇到了麻烦。我知道没有这样的东西,但我需要模拟它。我尝试重写Clone()函数,但没有成功。有什么想法吗,java,clone,overloading,variable-assignment,operator-keyword,Java,Clone,Overloading,Variable Assignment,Operator Keyword,以下是我的主要观点 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); 这是克隆函数 public void Clone(Queue other) throws Throwable {

以下是我的主要观点

 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);
这是克隆函数

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;

}   
    }

}

Java不支持运算符重载


操作符重载违反了Java语言设计的核心原则之一:透明性。试图重载运算符是违反语言哲学的,应该避免…

如果您试图重写对象上的方法,它需要具有相同的签名(和大写)。您的方法签名是
克隆(队列)


但是,这与试图模拟运算符重载有什么关系,或者您试图重载的运算符是什么,目前还不清楚。

在Java中无法实现运算符重载。这样做需要编译器的支持。但是,您可以创建一个理解java语法的预处理器,并在构建之前运行它,它将运算符的调用转换为适当的函数,但这太过分了


clone
与运算符重载无关。

您需要编写一个显式复制构造函数。在Java中,所有东西都是指针(或者他们称之为“引用”),所以当您将p分配给q时,您只是说p和q指向同一个对象

q = new Queue(p)
是您想要的,假设您已经实现了相应的构造函数

编辑: 您的建议在语法“q=p”中是不可能的。如果要“模拟”赋值运算符,则需要在队列中编写一个新方法。例如,以下是Java程序员如何“模拟”重载加法运算符:

a.add(b);
因此,在本例中,您需要编写一个“assign”方法,该方法接受队列,复制其数据,并将这些副本分配给另一个对象的内部数据

q.assign(p);

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

<>这和你要得到的接近。

< P>我来自C++背景,所以我知道你来自哪里。 我认为这是你追求的克隆方法。不要忘记实现Cloneable接口,告诉“Object”这个对象是一个Cloneable对象。请参阅有效Java(第二版)第11项以获得详细解释。

没有“等价物”,因为您无法在Java中重载赋值运算符。您正试图用clone()方法模拟这种行为,但您的做法是相反的。如果没有运算符重载,则有三个选项:

  • 编写一个函数,使调用对象变为传入对象的副本
  • 编写一个函数,创建调用对象的副本,并将其返回给将存储它的其他对象
  • 使用复制构造函数
  • 1.编写一个函数,使调用对象变为传入对象的副本。 请注意,这与赋值运算符重载相同,只是它的名称为“copy”,而不是“operator=”

    如果要执行此操作,请执行以下操作:

    foo A;
    foo B;
    A.copy(B);  // A is now a copy of B.
    
    foo A;
    foo B;
    A = B.clone();  // A is now a copy of B.
    
    foo B;
    foo A = new foo(B);  // Java version
    foo A(B); // C++ version
    
    在C++中:

    foo& copy(const foo& rhs)
    {
        if(&rhs != this)
        {
            this->m_someInt = rhs.m_someInt;
            this->m_someBar = rhs.m_someBar;
        }
        return *this;
    }
    
    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;
    }
    
    foo(const foo& rhs)
      :m_someInt(rhs.m_someInt), m_someBar(rhs.m_someBar)
    {
    }
    
    // Or optionally:
    foo(const foo& rhs) = default;
    
    在Java中:

    foo copy(foo rhs)
    {
        this.m_someInt = rhs.m_someInt;
        this.m_someBar.copy(rhs.m_someBar);
        return this;
    }
    
    foo clone()
    {
        foo theCopy = new foo();
        theCopy.m_someInt = this.m_someInt;
        theCopy.m_someBar = this.m_someBar.clone();
        return theCopy;
    }
    
    foo(foo rhs)
    {
        m_someInt = rhs.m_someInt;
        m_someBar = new Bar(rhs.m_someBar);
    }
    
    <>注意,在C++中,将<代码> > -> MyAdbar Bux/Cube > <代码> RH.MyAdbar Bux/Cuth>按值复制<代码> RS.MyAdbar Bux/Cuff>,但在java中,这样的赋值会导致FO对象共享同一个BAR对象。因此,如果您不想共享子对象,则必须在子对象上使用类似的复制机制

    2.编写一个函数,创建调用对象的副本,并将其返回给将存储它的其他对象。 如果要执行此操作,请执行以下操作:

    foo A;
    foo B;
    A.copy(B);  // A is now a copy of B.
    
    foo A;
    foo B;
    A = B.clone();  // A is now a copy of B.
    
    foo B;
    foo A = new foo(B);  // Java version
    foo A(B); // C++ version
    
    在C++中:

    foo& copy(const foo& rhs)
    {
        if(&rhs != this)
        {
            this->m_someInt = rhs.m_someInt;
            this->m_someBar = rhs.m_someBar;
        }
        return *this;
    }
    
    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;
    }
    
    foo(const foo& rhs)
      :m_someInt(rhs.m_someInt), m_someBar(rhs.m_someBar)
    {
    }
    
    // Or optionally:
    foo(const foo& rhs) = default;
    
    在Java中:

    foo copy(foo rhs)
    {
        this.m_someInt = rhs.m_someInt;
        this.m_someBar.copy(rhs.m_someBar);
        return this;
    }
    
    foo clone()
    {
        foo theCopy = new foo();
        theCopy.m_someInt = this.m_someInt;
        theCopy.m_someBar = this.m_someBar.clone();
        return theCopy;
    }
    
    foo(foo rhs)
    {
        m_someInt = rhs.m_someInt;
        m_someBar = new Bar(rhs.m_someBar);
    }
    
    3.使用复制构造函数。 如果要执行此操作,请执行以下操作:

    foo A;
    foo B;
    A.copy(B);  // A is now a copy of B.
    
    foo A;
    foo B;
    A = B.clone();  // A is now a copy of B.
    
    foo B;
    foo A = new foo(B);  // Java version
    foo A(B); // C++ version
    
    在C++中:

    foo& copy(const foo& rhs)
    {
        if(&rhs != this)
        {
            this->m_someInt = rhs.m_someInt;
            this->m_someBar = rhs.m_someBar;
        }
        return *this;
    }
    
    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;
    }
    
    foo(const foo& rhs)
      :m_someInt(rhs.m_someInt), m_someBar(rhs.m_someBar)
    {
    }
    
    // Or optionally:
    foo(const foo& rhs) = default;
    
    在Java中:

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

    总之,您的问题是,您试图以与上面使用copy()相同的方式使用clone()。最接近等效的方法是我的
    copy
    方法。我个人的建议是只使用一个复制构造函数,今天到此为止。

    克隆()与运算符重载有什么关系?你想重载
    =
    运算符的想法是非常可怕的。请浏览您自己的代码示例并思考为什么这是一件非常非常糟糕的事情。@Aniket它复制了一个对象。复制对象通常是运算符重载的目的。这就是他们之间的关系。好的,那么您将如何使用“q=p”进行复制和对象,并让它执行深度复制呢。因为当我运行代码时,它会复制,但只是一个浅拷贝。所以两个对象都指向同一个对象。是的,这就是赋值操作符在Java中的工作方式。您不能覆盖该功能并使其进行深度复制。你可能想用一个函数或者像我回答的那样的预处理器来实现它。是的,我已经实现了这个构造函数。它是有效的。但是我的任务是实现C++中的赋值操作符,java java。