简体   繁体   English

Java-继承,带有方法的类

[英]Java - inheritance, class with methods

I have my subclass: 我有我的子类:

 public class Actions extends Main{ public void getFireTarget() { GameObject target = getGameObjects().closest("Target"); do{ log("Shooting at the target"); getMouse().click(target); } while(target != null && !getDialogues().inDialogue() && getLocalPlayer().getTile().equals(rangeTile)); } } 

I want to write similar methods, so I can call them in my Main class, so I don't have to write over and over. 我想编写类似的方法,因此可以在Main类中调用它们,因此不必一遍又一遍地写。

My main class looks like this (won't fully paste it as it's long): 我的主类看起来像这样(因为很长,所以不会完全粘贴):

 public class Main extends AbstractScript{ ...code here Actions actions = new Actions(); } 

So I am trying to implement the methods in Actions by doing actions.getFireTarget(), which seems to work. 因此,我正在尝试通过执行action.getFireTarget()来实现Actions中的方法,该方法似乎有效。 But when I compile, I am getting two compile errors: 1) In the Main class, in the line: Actions actions = new Actions(); 但是,当我进行编译时,遇到了两个编译错误:1)在Main类的一行中:Actions actions = new Actions(); 2) In the Actions class, in the line where I am extending the superclass. 2)在Actions类中,在我扩展超类的那一行中。

Am I missing something in the sub class in order to store methods and then call them in the main method? 为了存储方法然后在main方法中调用它们,我是否在子类中缺少某些内容? Please advise! 请指教! Thanks 谢谢

The syntax is wrong. 语法错误。 () are not allowed here: public class SomeName() . ()在这里是不允许的: public class SomeName() Remove the brackets. 卸下支架。

I am having trouble understanding your details. 我无法理解您的详细信息。 Here is a short info about inheritance: 这是有关继承的简短信息:

public class A{
  protected int t;
  public void methodA(){}
}

public class B extends A{
  @Override
  public void methodA(){}
  public void methodB(){}
}

If you override the methodA in class B, any call from a instance of class B to the method will use the method defined in class B. (If you dont write the method in class B, it will use the method from class A) 如果您覆盖类B中的methodA,则从类B实例到该方法的任何调用都将使用类B中定义的方法。(如果您未在类B中编写该方法,它将使用类A中的方法)

Objects of class A cannot use methodB() defined in class B. Also you can access the field t in class B, because of the protected modified. A类的对象不能使用B类中定义的methodB()。由于受保护的修改,您还可以访问B类中的字段t。

I think its better for you your problem to instantiate other class to your main class if you have same function name or same variable name. 我认为如果您具有相同的函数名称或相同的变量名称,则可以将其他类实例化为您的主类,这对您更好。 try to compile and run this code 尝试编译并运行此代码

public class First{
    Second sec = new Second();
    String s = "This is first";
    public First(){

        System.out.println(this.s);
        System.out.println(getSecondString());
        System.out.println(sec.getSecondString());
    }
    public String getSecondString(){
        return "This is first";
    }
    public static void main(String args[]){
        new First();
    }
}

public class Second {

    String s = "This is second";
    public String getSecondString(){
        return s;
    }

}

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

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