Java 调用特定其他对象的方法
我想创建一个以某种方式链接到同一类的另一个对象的对象。应该在新对象的构造函数中指定此链接Java 调用特定其他对象的方法,java,Java,我想创建一个以某种方式链接到同一类的另一个对象的对象。应该在新对象的构造函数中指定此链接 public class Counter { public Counter(){ // default counter constructor } public Counter(Counter oldCounter){ // do stuff specifying new object is linked to oldCounter } pub
public class Counter {
public Counter(){
// default counter constructor
}
public Counter(Counter oldCounter){
// do stuff specifying new object is linked to oldCounter
}
public void someMethod(){
// this method should call a method belonging to oldCounter
oldCounter.someOtherMethod();
}
尝试在存档中搜索答案,但找不到任何结果…请将参数作为私有实例成员记住,然后使用该成员:
public class Counter {
// The instance member we'll use, note that we initialize it to `null`
// because you have a zero-args constructor, so we want to be sure we
// know whether we have one or not
private Counter otherCounter = null;
public Counter() {}
public Counter(Counter oldCounter) {
// Remember it here
this.otherCounter = oldCounter;
}
public void someMethod() {
// Use it here
if (this.otherCounter != null) {
this.otherCOunter.someOtherMethod();
}
}
}
请记住作为私有实例成员的参数,然后使用该成员:
public class Counter {
// The instance member we'll use, note that we initialize it to `null`
// because you have a zero-args constructor, so we want to be sure we
// know whether we have one or not
private Counter otherCounter = null;
public Counter() {}
public Counter(Counter oldCounter) {
// Remember it here
this.otherCounter = oldCounter;
}
public void someMethod() {
// Use it here
if (this.otherCounter != null) {
this.otherCOunter.someOtherMethod();
}
}
}
请记住作为私有实例成员的参数,然后使用该成员:
public class Counter {
// The instance member we'll use, note that we initialize it to `null`
// because you have a zero-args constructor, so we want to be sure we
// know whether we have one or not
private Counter otherCounter = null;
public Counter() {}
public Counter(Counter oldCounter) {
// Remember it here
this.otherCounter = oldCounter;
}
public void someMethod() {
// Use it here
if (this.otherCounter != null) {
this.otherCOunter.someOtherMethod();
}
}
}
请记住作为私有实例成员的参数,然后使用该成员:
public class Counter {
// The instance member we'll use, note that we initialize it to `null`
// because you have a zero-args constructor, so we want to be sure we
// know whether we have one or not
private Counter otherCounter = null;
public Counter() {}
public Counter(Counter oldCounter) {
// Remember it here
this.otherCounter = oldCounter;
}
public void someMethod() {
// Use it here
if (this.otherCounter != null) {
this.otherCOunter.someOtherMethod();
}
}
}
要实现你想要的,克劳德的答案足够好了
您拥有同一类的两个版本。对我来说,似乎需要遵循一些设计模式来更好地组织代码。在这种情况下,工厂模式可能是一个不错的选择 要实现你想要的,Crowder的答案已经足够好了
您拥有同一类的两个版本。对我来说,似乎需要遵循一些设计模式来更好地组织代码。在这种情况下,工厂模式可能是一个不错的选择 要实现你想要的,Crowder的答案已经足够好了
您拥有同一类的两个版本。对我来说,似乎需要遵循一些设计模式来更好地组织代码。在这种情况下,工厂模式可能是一个不错的选择 要实现你想要的,Crowder的答案已经足够好了
您拥有同一类的两个版本。对我来说,似乎需要遵循一些设计模式来更好地组织代码。在这种情况下,工厂模式可能是一个不错的选择 您的代码执行您所说的操作(假设您将提供给构造函数的参数保存在某个位置)。你的问题是什么?我如何“保存论点”?如何在构造函数中指定链接?您的代码执行您所说的操作(假设您将提供给构造函数的参数保存在某个位置)。你的问题是什么?我如何“保存论点”?如何在构造函数中指定链接?您的代码执行您所说的操作(假设您将提供给构造函数的参数保存在某个位置)。你的问题是什么?我如何“保存论点”?如何在构造函数中指定链接?您的代码执行您所说的操作(假设您将提供给构造函数的参数保存在某个位置)。你的问题是什么?我如何“保存论点”?如何在构造函数中指定链接?