简体   繁体   English

如何在 JAVA 中调用同一包中的其他类的方法或访问字段

[英]How can I invoke a method or access a field from other class in the same package in JAVA

I am a very beginner of JAVA programming.我是 JAVA 编程的初学者。 I am working on my course project which is creating a game.我正在开发我的课程项目,该项目正在创建一个游戏。 I created 2 classes for this game and they are in the same package.我为这个游戏创建了 2 个类,它们在同一个包中。 In class B, I need to invoke the method from class A, and access the field also.在 B 类中,我需要调用 A 类中的方法,并访问该字段。 How should the code be to access the method which is defined in class A?代码应该如何访问A类中定义的方法? I searched for the solution online and someone suggested that write the code like this:我在网上搜索了解决方案,有人建议像这样编写代码:

Classname.methodname(args)

However, there is a compile error(I think)但是,有一个编译错误(我认为)

error: cannot find symbol  public A newA;  symbol:   class A    location: class B

which the code I wrote is something like this:我写的代码是这样的:

newA.moveRight(A.getANumber());

One method I learnt from the lesson is that I extends the class to access the method.我从课程中学到的一种方法是扩展类以访问该方法。 But those are 2 different class so I cannot extend the class.但那些是 2 个不同的类,所以我不能扩展这个类。

Can someone tell me the reason about the mistake I made or the solution about this issue?有人可以告诉我我犯的错误的原因或有关此问题的解决方案吗? Thank you so much for the help.十分感谢你的帮助。

There are two ways to go about solving this problem.有两种方法可以解决这个问题。 The first is make the methods static and second is make an instance of the other class.第一个是使方法静态,第二个是创建另一个类的实例。

Suppose you have to classes, ClassA and ClassB假设你有类, ClassAClassB

ClassA A类

ClassA has no static methods and a public initializer, so the only way to call methods from ClassA is to make an instance of it. ClassA没有静态方法和公共初始化程序,因此从ClassA调用方法的唯一方法是创建它的实例。

public class ClassA{

    public ClassA(){
        //Intentionally blank
    }

    public void doSomething(int anInt){
        //Intentionally blank
    }

    public int doSomethingElse(){
        return -1;
    }
}

ClassB B级

ClassB has a private initializer and static methods, so it is non instantiatable and its methods can be called with out having an instance of the class. ClassB有一个私有初始化器和静态方法,所以它是不可实例化的,它的方法可以在没有类的实例的情况下被调用。

public class ClassB{

    private ClassB(){
        //Intentionally blank
    }

    public static void doSomething(int anInt){
        //Intentionally blank
    }

    public static int doSomethingElse(){
        return -1;
    }
}

Now suppose you have this code:现在假设你有这个代码:

public class ClassMain{

    public static void main(String... args){
        ClassB classB = new ClassB();//Will throw an error
        ClassA classA = new ClassA();//Will compile fine

        ClassB.doSomething(ClassB.doSomethingElse());//Will compile fine
        classB.doSomething(classB.doSomethingElse());//Will give a warning

        ClassA.doSomething(ClassA.doSomethingElse());//Will throw an error
        classA.doSomething(classA.doSomethingElse());//Will compile fine
    }
}

To call methods from ClassA you will need to define an instance( ClassA classA = new ClassA(); ) in order to call its methods.要从ClassA调用方法,您需要定义一个实例( ClassA classA = new ClassA(); )以调用其方法。 This is because the methods are not class methods.这是因为这些方法不是类方法。

To call methods from ClassB you can simply access them with the class name( ClassB.doSomethingElse(); ).要从ClassB调用方法,您可以简单地使用类名( ClassB.doSomethingElse(); )访问它们。 The reason you can do this with ClassB is because its methods are static and are therefor class members.你可以用ClassB这样做的原因是因为它的方法是static ,因此是类成员。

Summary概括

A method must be static in order for its methods to be called by class name, otherwise you will need an instance of the class.方法必须是静态的,才能通过类名调用其方法,否则您将需要该类的实例。

This is oracles tutorial about class instantation. 是关于类实例化的 oracles 教程。 This is oracles tutorial about class members. 是关于班级成员的 oracles 教程。

I suggest you read through them in order to get a full understanding of inheritance, static members and so on.建议大家通读一遍,对继承、静态成员等有一个全面的了解。

Using the Java reflection can easily invoke the method and access the fields.使用 Java 反射可以轻松调用方法并访问字段。

Class<?> clazzA = ClassA.class;
ClassA objectA = new ClassA();
Method method = clazzA.getMethod(methodName, parasType);
method.setAccessible(true);
method.invoke(objectA,paras);
Field field = clazzA.getField(fieldName);
field.setAccessible(true);
Object fieldType = field.get(objectA);

you can't just access a classes filed directly.您不能只访问直接提交的类。 so you need to create a setter and getter method to access or modify other classes fields所以你需要创建一个 setter 和 getter 方法来访问或修改其他类的字段

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

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