简体   繁体   English

由于某种原因,我的java类不会继承公共void。 有人可以帮我吗?

[英]For some reason my java class won't inherit a public void. Can somebody help me?

I'm pretty much new to Java development and I'm trying to build a simple android game, for this, I need to inherit some methods from other classes. 我对Java开发非常陌生,因此我试图构建一个简单的android游戏,为此,我需要从其他类继承一些方法。 Only, for some reason it won't. 只是出于某种原因,它不会。 Am I missing something here? 我在这里想念什么吗?

Here is code from the public Java class "Addition" 这是来自公共Java类“ Addition”的代码

public class Addition {
    Random random = new Random();

    int Answer;
    int PartOne;
    int PartTwo;
    int FalseAnswer;

    //CALL IN THIS FIRST!

    public void Calculate() {
        Answer = random.nextInt(999) + 2;
        PartTwo = random.nextInt(Answer--) + 1;
        PartOne = Answer - PartTwo;
        FalseAnswer = random.nextInt(Math.round(Answer * 1.2f)) + Math.round(Answer * 0.8f);
    }

//some more stuff. //一些更多的东西。

Here is the class subtraction, that I want to base upon Addition. 这是我要基于加法的类减法。 Here is the code that I have there: 这是我在那里的代码:

    public class Subtraction{
    Addition addition = new Addition();
    addition.Calculate();
}

Don't mind the formatting, it is just to demonstrate. 不要介意格式,只是为了演示。 It is giving me an error, saying Calculate does not exist or cannot be found (neither can the other methods in the class). 它给我一个错误,说Calculate不存在或找不到(类中的其他方法也不能)。

I have also already tried inherting directly: 我也已经尝试过直接继承:

public class Subtraction extends Addition{
    Calculate();
}

Didn't work either. 也没用。 The only thing it let's me do is this: 它让我做的唯一一件事是:

public class Subtraction extends Addition{
    @Override
    public void Calculate() {
        super.Calculate();
    }
}

Which is not what I want. 这不是我想要的。 I'm sure I'm missing something obvious, but can someone please point it out? 我确定我缺少明显的东西,但是有人可以指出吗? Thank you in advance. 先感谢您。

public class Subtraction extends Addition{

}

is all you need. 是你所需要的全部。 Don't write Calculate(); 不要写Calculate(); in that class, you've already inherited it. 在该类中,您已经继承了它。

You get the error because you can't call methods like that outside of any class method. 之所以会出现错误,是因为您无法在任何类方法之外调用此类方法。 That' why this would work 这就是为什么这行得通

public class Subtraction extends Addition{
    @Override
    public void Calculate() {
        super.Calculate(); // calls the 'calculate' method of the 'Addition' class
    }
}

& this won't: &这不会:

public class Subtraction extends Addition{
    Calculate(); // Outside of any method, invalid call
}

If you aren't trying to inherit from the Addition class using: 如果您不尝试使用以下方法从Addition类继承:

public class Subtraction extends Addition
{
    @Override
    public void Calculate()
    {
        super.Calculate(); // calls the 'calculate' method of the 'Addition' class
    }
}

Then you need to put 那你需要放

Addition addition = new Addition();
addition.Calculate();

into a method that can be called. 成为可以调用的方法 Do it like this: 像这样做:

public class Subtraction
{
    public void Calculate()
    {
        Addition addition = new Addition();
        addition.Calculate();
    }
}

You have to do it this way because the method calls have to be in an executable context (in a method or constructor). 您必须采用这种方式,因为方法调用必须在可执行上下文中(在方法或构造函数中)。

暂无
暂无

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

相关问题 试图在公共 static void 内乘法。 任何帮助表示赞赏 - Trying to multiply within the public static void. Any help is appreciated 有人可以帮助我解决在64位Windows 8.1系统上与Java,Ant和Eclipse产生的令人沮丧的冲突吗? - Can somebody help me resolve some frustrating conflicts with Java, Ant, and Eclipse on a 64 bit Windows 8.1 system? 为什么我的公共void构造函数{}无法编译? - Why won't my public void Constructor {} compile? 有人可以帮我建立一个可以与 MongoDB 一起使用的@Query - Can somebody help me building up an @Query that can be used with MongoDB 有人可以帮我点击JAVA Jbutton时添加文本吗? - Could somebody help me with adding text to a JAVA Jbutton when it is clicked? 我的actionlistener不在我的多类/框架Java项目上工作,有人可以帮助我修复它吗? - My actionlistener is not working on my multi-class/frame Java project, can anyone help me fix it? 嗨,我是 Java 的新手,我在试图找出底部“case SCISSORS:”的问题时遇到了麻烦,有人可以帮我:) - Hi, im new to Java and im having trouble trying to figure out the problem with “case SCISSORS:” at the bottom can somebody help me:) 内部类中的Java KeyListener由于某种原因未获取事件 - Java KeyListener in inner class doesn't get the event for some reason 有人可以帮我解决此消息java Exception吗? - Can some one help me with this message java Exception? 有人可以帮我弄清楚为什么我的零没有附加? - Can some help me figure out why my zeroes are not appending?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM