Java:传递相同对象作为参数的对象构造函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20011797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: An Object Constructor Passing Same Object as Parameter
提问by Maggie S.
I have made an object called Transaction which I am passing in an ArrayQueue.
我创建了一个名为 Transaction 的对象,我将它传入一个 ArrayQueue。
Here is the Transaction class constructors (I have the appropriate setter and getters too):
这是 Transaction 类构造函数(我也有适当的 setter 和 getter):
public class Transaction {
private int shares;
private int price;
public Transaction(int shares, int price) {
this.shares = shares;
this.price = price;
}
public Transaction(Object obj) {
shares = obj.getShares();
price = obj.getPrice();
}
}
In the second constructor there I am wanting a scenario where I can pass into it a different Transaction object that has been dequeue(ed) and get the info from that transaction and make it into a new transaction or possibly manipulate it before I put it back into the queue. But when I compile it does not like this.
在第二个构造函数中,我想要一个场景,我可以将一个不同的 Transaction 对象传递给它,该对象已出列(ed)并从该事务中获取信息并将其放入新事务中或可能在我放回之前对其进行操作进入队列。但是当我编译时它不喜欢这样。
Is this acceptable programming practice to pass a specific object into it's own object's constructor? Or is this even possible?
将特定对象传递给它自己的对象的构造函数是否可以接受这种编程实践?或者这甚至可能吗?
采纳答案by Akira
You need to specify the same type:
您需要指定相同的类型:
public Transaction(Transaction obj) {
shares = obj.getShares();
price = obj.getPrice();
}
Provided that you have defined getShares() and getPrice().
前提是您已经定义了 getShares() 和 getPrice()。
回答by johnsoe
Yes this is entirely possible.
是的,这是完全可能的。
public Transaction(Transaction other){
shares = other.shares;
price = other.price;
}
You do not need to call their getters because privacy only applies to other classes.
您不需要调用它们的 getter,因为隐私仅适用于其他类。
回答by Mariusz Jamro
It's called copy-constructorand you should use public Transaction(Transaction obj)
instead of Object
and also provide getters:
它被称为复制构造函数,您应该使用public Transaction(Transaction obj)
代替Object
并提供 getter:
public class Transaction {
private int shares;
private int price;
public Transaction(int shares, int price) {
this.shares = shares;
this.price = price;
}
public Transaction(Transaction obj) {
this(obj.getShares(), obj.getPrice()); // Call the constructor above with values from given Transaction
}
public int getShares(){
return shares;
}
public int getPrice(){
return price;
}
}
回答by Ankit Rustagi
Yes, you can do that but you will have to type cast the parameter
是的,你可以这样做,但你必须输入参数
public Transaction(Object obj) {
Transaction myObj = (Transaction) obj;
shares = MyObj.getShares();
price = MyObj.getPrice();
}
回答by Damian
for instance lets assume we have a class of Author with all the getters created to pass another object as a parameter we use the following syntax
例如让我们假设我们有一个 Author 类,所有的 getter 被创建来传递另一个对象作为参数,我们使用以下语法
public Author(ClassName variable){
this(obj.getlength(), obj.getwidht())// height and width are the instance variable of the class.
}