简体   繁体   English

如何使用另一个类的方法?

[英]how to use method from another class?

I think I have my method set properly to give me the total number of rooms but it won't compile. 我想我已经正确设置了方法,可以为我提供房间总数,但是无法编译。 Where did I go wrong? 我哪里做错了?

public class Hotel {


    public static int singleRooms;
    public static int doubleRooms;
    public static int kingRooms;

    public Hotel(int numberSingleRooms, int numberDoubleRooms, int numberKingRooms) {
        singleRooms = numberSingleRooms;
        doubleRooms = numberDoubleRooms;
        kingRooms = numberKingRooms;
        }

    public static Hotel IvanHotel = new Hotel(1,3,5);


    int totalRooms = GetTotalRooms(IvanHotel);

    public static void main(String[] args) {
        System.out.println(totalRooms);
    }

}

public class GetTotalRooms {

    public static int totalRooms;

    public int GetTotalRooms(Hotel yourHotel) {

        totalRooms = Hotel.singleRooms + Hotel.doubleRooms + Hotel.kingRooms;

        return totalRooms;

    }


}

What should I change in order to get it to compile? 我应该更改什么才能使其编译? I am getting a cannot find symbol error and I don't know why. 我收到无法找到的符号错误,我也不知道为什么。

Looks like you confused with the concept of static . 看起来您对static的概念感到困惑。 You can't access non-static values from static context. 您不能从static上下文访问非静态值。

This code should fix things up 这段代码应该解决问题

public class Hotel { 
    //...
    static int totalRooms = GetTotalRooms.getRoomsCount(IvanHotel);
    //...     
} 

class GetTotalRooms { 

    public static int getRoomsCount(Hotel yourHotel) {
        totalRooms = Hotel.singleRooms + Hotel.doubleRooms + Hotel.kingRooms;

        return totalRooms;
    } 
} 

If you are a beginner I would suggest reading a lot of sample codes and start with basic concepts. 如果您是初学者,建议您阅读大量示例代码,并从基本概念入手。 Don't skip the basic concepts. 不要跳过基本概念。

You need to do a bit more reading to understand what the purpose of static is and why you'd use it. 您需要多读一些书,以了解static的目的是什么以及为什么要使用它。 The way you wrote your code, you cannot call a non static method in a static way, you first need to create an instance of your GetTotalRooms first before you can do that GetTotalRooms so in the case of your code you'd write this 在编写代码的方式中,您不能以静态方式调用非静态方法,首先需要首先创建GetTotalRooms的实例,然后才能执行该GetTotalRooms因此,在编写代码的情况下,请编写此代码

//...
private GetTotalRooms getTotalRooms = new GetTotalRooms();
int totalRooms = getTotalRooms.GetTotalRooms(IvanHotel);
//...

or you should follow what Doc said and make the method your calling static as well. 或者您应该遵循Doc所说的,并使该方法也成为static调用。

I'd also recommend you practice your naming conventions, 1st your class name sounds like a method name and then you have a method with the exact same name of your class, your Hotel class is fine but the GetTotalRooms class is the problem, 我还建议您练习命名约定,首先,您的类名听起来像方法名,然后您有了一个与您的类名完全相同的方法,您的Hotel类很好,但是GetTotalRooms类是问题所在,

id rather name it something like RoomsList since you want to return the total amount of rooms you have in your Hotel id而不是像RoomsList这样的名称,因为您想返回Hotel的房间总数

your class names should refer an Object, GetTotalRooms is not an object it's an equation in your case. 您的类名称应引用一个对象, GetTotalRooms不是对象, GetTotalRooms您的情况下的等式。 Then your method name should refer to an action, and it should start with a lower case character, the name you chose for your method is good, it tells me exactly whats going to happen when I call it, just make it start with a lowercase so that it won't look like a class. 然后,您的方法名称应引用一个动作,并应以小写字母开头,为您的方法选择的名称很好,它告诉我在调用它时会发生什么,只需使其以小写字母开头这样它就不会像一堂课。

you can read more about it here: 你可以在这里读更多关于它的内容:

Coding like Shakespeare 像莎士比亚一样编码

and

dzone dzone

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

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