Java 访问具有相同名称的方法
我有三个类,它们都有一个同名的方法。My Main类具有以下方法(当用户单击鼠标时自动调用): 我的第二个类叫做Bed,它的方法是:Java 访问具有相同名称的方法,java,class,object,methods,Java,Class,Object,Methods,我有三个类,它们都有一个同名的方法。My Main类具有以下方法(当用户单击鼠标时自动调用): 我的第二个类叫做Bed,它的方法是: public void mouseDown() 我的第三个类叫做CreateBedButton,它有一个方法(返回一个Bed对象): 我能够从Main中的mouseDown方法相当容易地访问Bed类的mouseDown方法,但是我不知道如何指示我要调用CreateBedButton类的mouseDown方法。它们都没有参数,但当我调用Bed类的mouseDown
public void mouseDown()
我的第三个类叫做CreateBedButton,它有一个方法(返回一个Bed对象):
我能够从Main中的mouseDown方法相当容易地访问Bed类的mouseDown方法,但是我不知道如何指示我要调用CreateBedButton类的mouseDown方法。它们都没有参数,但当我调用Bed类的mouseDown方法时,我通过使用以下方法创建的Bed对象访问它:
bed.mouseDown();
当我试着打电话时:
mouseDown();
在Main中,它再次调用Main方法的mouseDown方法。是否有任何方法可以访问所有三个方法/明确我实际要访问的方法???调用.mouseDown()
访问主类的方法。调用bed.mouseDown()
访问bed
class'方法。调用createRedButton.mouseDown()
调用createRedButton
class'方法
这假设您有变量Bed Bed=new Bed()代码>和CreateRedButton CreateRedButton=new CreateRedButton()
已声明
下面是一个例子
class Main {
public void main(String [] args) { // Starting point of program
Bed bed = new Bed();
CreateRedButton createRedButton = new CreateRedButton();
mouseDown(); // Calls Main's mouseDown();
bed.mouseDown(); // Calls Bed's mouseDown();
Bed catchingBed = createRedButton.mouseDown(); // Calls CreateRedButton's mouseDown(); - Which returns a Bed
// Or you can even do this..
CreateRedButton redButton = new CreateRedButton();
Bed bed = redButton.mouseDown();
bed.mouseDown();
}
public void mouseDown() {
// Some code
}
}
class Bed {
public void mouseDown() {
// Some code
}
}
class CreateRedButton {
public Bed mouseDown() {
// Some code to return a Bed
}
}
调用.mouseDown()
访问Main
class'方法。调用bed.mouseDown()
访问bed
class'方法。调用createRedButton.mouseDown()
调用createRedButton
class'方法。这假设您有变量Bed Bed=new Bed()代码>和CreateRedButton CreateRedButton=new CreateRedButton()
已声明。
mouseDown();
class Main {
public void main(String [] args) { // Starting point of program
Bed bed = new Bed();
CreateRedButton createRedButton = new CreateRedButton();
mouseDown(); // Calls Main's mouseDown();
bed.mouseDown(); // Calls Bed's mouseDown();
Bed catchingBed = createRedButton.mouseDown(); // Calls CreateRedButton's mouseDown(); - Which returns a Bed
// Or you can even do this..
CreateRedButton redButton = new CreateRedButton();
Bed bed = redButton.mouseDown();
bed.mouseDown();
}
public void mouseDown() {
// Some code
}
}
class Bed {
public void mouseDown() {
// Some code
}
}
class CreateRedButton {
public Bed mouseDown() {
// Some code to return a Bed
}
}