不调用复制构造函数,但默认构造函数称为java

时间:2017-03-07 23:44:07

标签: java constructor copy clone

package primary;

public class Room implements Cloneable{

    int room_no;
    private int leftStrength; //number of students sitting on left side
    private int rightStrength;//number of students sitting on right side
    private int capacity;
    private int timeSlot;
    private boolean checkBig;
     boolean invigilanceRequired;
    private int rightCapacity;
    private int leftCapacity;
    public Room(int room_no,int capacity)
    {
        this.room_no=room_no;
        this.capacity=capacity;
        rightStrength=0;
        leftStrength=0;
        timeSlot=0;
        checkBig=true;
        invigilanceRequired=true;
        rightCapacity=capacity;
        leftCapacity=capacity;
    }

    public Room(Room other)
    {
        this.room_no=other.room_no;
        this.capacity=other.capacity;
        this.rightStrength=other.rightStrength;
        this.leftStrength=other.leftStrength;
        this.timeSlot=other.timeSlot;
        this.checkBig=other.checkBig;
        this.invigilanceRequired=other.invigilanceRequired;
        this.rightCapacity=other.rightCapacity;
        this.leftCapacity=other.leftCapacity;
    }
    @Override
    protected Object clone() throws CloneNotSupportedException {
        // TODO Auto-generated method stub
        return super.clone();
    }

}

我尝试过复制构造函数和clone()来制作Room对象的副本,但每次它给出相同的对象并且不复制。我发现它总是调用参数化构造函数。 我称之为Room copy构造函数的代码的一小部分:

public TimeInterval(TimeInterval other) throws CloneNotSupportedException 
    {
        course_to_room=new ArrayList<>();
        map=new HashMap<>();
        rooms=new ArrayList<>();
        this.day_no=other.day_no;
        this.time_interval=other.time_interval;
        for(Course course:other.course_to_room)
        {
            this.course_to_room.add((Course)course.clone());
        }

        for(Room room:other.rooms)
        {
            Room tempRoom=new Room(room);
            this.rooms.add(tempRoom);
        }


        for(Integer key:other.map.keySet())
        {
            ArrayList<Course> temp=other.map.get(key);
            ArrayList<Course> newClone=new ArrayList<>();
            for(Course course:temp)
            {
                newClone.add(new Course(course));
            }


            this.map.put(key, newClone);
        }
    }

在这里,我正在执行复制Room对象的确切代码:

TimeInterval save1=new TimeInterval(time1);

当我从2个对象,save1和time1打印任何变量时,它们是不同的。

我正在添加输入命令和输出

System.out.println("Old time 1,Room 5 capacity left                           "+save1.R4.getRightCapacity());
System.out.println("Old time 2,Room 5 capacity left "+save2.R4.getRightCapacity());
System.out.println("Old time 2,Room 5 right strength "+save2.R4.getRightStrength());
System.out.println("Old time 1,Room 5 capacity left "+time1.R4.getRightCapacity());
System.out.println("Old time 2,Room 5 capacity left "+time2.R4.getRightCapacity());
System.out.println("Old time 2,Room 5 time 1 right strength "+time1.R4.getRightStrength());

输出:

旧时代1,房间5容量剩下60

旧时代2,房间5容量剩下60

旧时代2,房间5右强度0

旧时间1,房间5容量0

旧时代2,房间5容量剩下60

旧时代2,房间5时间1右强度60

2 个答案:

答案 0 :(得分:0)

我已经解决了这个问题。我在所有构造函数中打印了一些东西并逐行调试。我发现这个语句(TimeInterval save1 = new TimeInterval(time1);)导致了这个问题。 我写了我在步骤中找到的东西:

  1. 当TimeInterval save1 = new TimeInterval(time1)时;开始执行,它调用了Room的参数化构造函数(我错误地称之为默认值)。然后它调用了房间的复制构造函数。

  2. 但我希望它只是调用Room的复制构造函数。然后,我发现我在TimeInterval类的复制构造函数之外创建了一些Room对象(不只是引用,而是整个对象,通过调用&#39; new&#39;关键字)。

  3. 我将这些行放在TimeInterval类的参数化构造函数中创建Room对象的位置。

  4. 问题得到解决。

  5. 打印错误值的问题:当TimeInterval类使用参数化构造函数创建Room对象时(对于在TimeInterval类中的构造函数外部编写的那些行),它将我在Room对象中打印的那些变量初始化为默认值(这是0)。然后,它调用Room的复制构造函数,只是复制了这些Room对象(其变量具有默认值),而不是我想要的那些房间对象的副本。

答案 1 :(得分:-1)

Java没有像C ++那样拥有复制构​​造函数。

你制作了两个构造函数,一个是房间号和容量,另一个是现有房间。您没有默认构造函数。此外,没有自动使用复制构造函数&#39;在Java中。相反,如果你想制作一个副本,那么你明确地通过显式调用你所做的Room(Room)构造函数来做到这一点。

您想要获得克隆&#39;调用Room(Room)构造函数的方法?在这种情况下,你可以从克隆&#39;内部调用它。方法,如果这是所需要的。

当你说他们不同时,你的意思是什么?您是否打印地址并注意到它们是不同的物理对象?或者你是说那些应该相同的参数不是?