简体   繁体   English

从另一个类调用方法时遇到麻烦

[英]Trouble calling a method from another class

Below is my class file Rectangle and the class file testRectangle with the main method. 下面是我的类文件Rectangle和类文件testRectangle及其主要方法。 Now in testRectangle when I call on the method "perimeter" in the class "Rectangle", I receive an error message. 现在在testRectangle中,当我在类“ Rectangle”中调用方法“ perimeter”时,我收到一条错误消息。 The error message I receive states, "Change modifier of 'perimeter()' to 'static'. The method can't be static because I will have several different rectangle objects in the main method. Am I doing something wrong? Any help would be greatly appreciated. 我收到的错误消息是“将'perimeter()'的修饰符更改为'静态'。该方法不能是静态的,因为在主方法中我将有几个不同的矩形对象。我做错了吗?有什么帮助吗?非常感谢。

Rectangle.java Rectangle.java

public class Rectangle {
    private  int length;
    private  int width;

    Rectangle(int len, int wid) {
        length = len;
        width = wid;
    }
    public int perimeter(Rectangle rec){
        int p = 2*length + 2* width;
        return p;
    }
}

testRectangle.java testRectangle.java

    public class testRectangle {

        public static void main(String[] args) {
            Rectangle r1 = new Rectangle(5,4);
            int r1Perimeter = Rectangle.perimeter(r1);
 //the line above this is where I get the error message
 //the red squiggly line goes under "Rectangle.perimeter(r1);
        }

    }

You have to call that method on an object of Rectangle. 您必须在Rectangle的对象上调用该方法。 Because, perimeter() is an instance method, so you have to call on an instance. 因为perimeter()是实例方法,所以您必须调用实例。

If perimeter() is a static method, then you can call it with Class, like Rectangle.perimeter(r1); 如果perimeter()是静态方法,则可以使用Class调用它,例如Rectangle.perimeter(r1);

int r1Perimeter = r1.perimeter(r1);

And there is no need to pass the Rectangle object there, define perimeter() method like below 并且不需要在那里传递Rectangle对象,定义如下的perimeter()方法

public int perimeter(){
   int p = 2*length + 2* width;
   return p;
}

When you call Rectangle.perimeter , where Rectangle is a class, it's only allowed if perimeter is a static method. 当您调用Rectangle.perimeter ,其中Rectangle是一个类时,仅当perimeter静态方法时才允许使用。

But the problem here is in how you defined perimeter : 但是这里的问题是您如何定义perimeter

public int perimeter(Rectangle rec){

You really want this to be an instance method, because you want it to work on on an instance of Rectangle . 您确实希望这是一个实例方法,因为您希望它在Rectangle的实例上工作。 So you really don't want to include a Rectangle parameter. 因此,您确实不想包含Rectangle参数。 In fact, your code body never uses rec , so the parameter here is wasted. 实际上,您的代码体从不使用rec ,因此这里的参数被浪费了。 You should change it to 您应该将其更改为

public int perimeter() {
    ...
}

and then the call Rectangle.perimeter(r1) becomes r1.perimeter() . 然后调用Rectangle.perimeter(r1)变为r1.perimeter()

Whenever you call a class method, in this case perimeter() , the compiler needs to know which 'rectangle' you are referring to. 每当您调用类方法(在这种情况下为perimeter() ,编译器都需要知道您所指的是哪个“矩形”。 If you were simply to refer to Rectangle.perimeter() , you are not specifying which rectangle, so the compiler becomes confused. 如果仅引用Rectangle.perimeter() ,则没有指定哪个矩形,因此编译器会感到困惑。 Once you create a rectangle Rectangle r1 = new Rectangle(5,4) , you have to let the compiler know that you are getting the perimeter of triangle r1 . 创建矩形Rectangle r1 = new Rectangle(5,4) ,必须让编译器知道您正在获取三角形r1的周长。 To do this you must call r1.perimeter() . 为此,您必须调用r1.perimeter()

Essentially, when you define a method to a class, a pointer this to the object is passed along with it so that the compiler knows which specific Rectangle instance you are referring to. 本质上,当您为类定义方法时,指向this对象的指针将与对象一起传递,以便编译器知道您要引用的是哪个Rectangle实例。

If, on the other hand, you really wanted to call a method with Rectangle.perimeter() , then you would be referring to a method that would be the same for every rectangle. 另一方面,如果您真的想使用Rectangle.perimeter()调用方法,那么您将引用的方法对于每个矩形都是相同的。 For example, you could create a method called getNumSides() , where no matter the rectangle, the value will always be the same. 例如,您可以创建一个名为getNumSides()的方法,无论矩形如何,该值始终是相同的。 To do this you must declare it with static , meaning that the this pointer is no longer passed when the method is called. 为此,必须使用static声明它,这意味着在调用方法时不再传递this指针。 The method would be declared with public static int getNumSides() and would be called with Rectangle.getNumSides() . 该方法将使用public static int getNumSides()进行声明,并将使用Rectangle.getNumSides()进行调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM