Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.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 访问具有相同名称的方法_Java_Class_Object_Methods - Fatal编程技术网

Java 访问具有相同名称的方法

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

我有三个类,它们都有一个同名的方法。My Main类具有以下方法(当用户单击鼠标时自动调用):

我的第二个类叫做Bed,它的方法是:

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
    }
}