繁体   English   中英

在此示例中如何使用关键字“super”?

[英]How can I use the keyword “super” in this example?

这是我的超类...

package com.company;

import java.util.Random;

public class Terning
{

// Make a random number generator for a 10 sided dice.
Random random = new Random();

public void RanNum()
{
    for (int i = 0; i < 10; i++)
    {
        int answer = random.nextInt(10) + 1;

        System.out.println(answer);
    }
}
}

这是我的子类...

package com.company;

// Extend FlaskTerning to inherit Terning methods
public class FalskTerning extends  Terning
{


@Override
// Could be given another name to distinguis between the superclass method and the subclass method

public void RanNum() {
    super.RanNum();

    // The if-statement I want to make.
    if (bla bla bla)
    {
        
        answer == 9;
    }
}
}

最后,这是我的主要...

package com.company;

// Main method
public class Main {

public static void main(String[] args)


{

    Terning terning = new Terning();
    terning.RanNum();
}
}

背景故事:我制作了一个“x”面骰子(这里我们尝试使用 10 个)和一个“假骰子”。 我想实现一个子和超级 class (继承)。

问题:如何使子类“FalskTerning”(FalseDice)获取“Terning”(Dice)产生的随机数的值并将其转换为 if 语句。 示例(如果数字 x 小于 5,则答案为 10)?

此外,我想知道,为什么我不能在我的子类“FalskTerning”的 if 语句中使用超类“Terning”中的“answer”。

祝你有美好的一天!

这是我的超类...

package com.company;

import java.util.Random;

public class Terning
{

// Make a random number generator for a 10 sided dice.
Random random = new Random();

public void RanNum()
{
    for (int i = 0; i < 10; i++)
    {
        int answer = random.nextInt(10) + 1;

        System.out.println(answer);
    }
}
}

这是我的子类...

package com.company;

// Extend FlaskTerning to inherit Terning methods
public class FalskTerning extends  Terning
{


@Override
// Could be given another name to distinguis between the superclass method and the subclass method

public void RanNum() {
    super.RanNum();

    // The if-statement I want to make.
    if (bla bla bla)
    {
        
        answer == 9;
    }
}
}

最后,这是我的主要...

package com.company;

// Main method
public class Main {

public static void main(String[] args)


{

    Terning terning = new Terning();
    terning.RanNum();
}
}

背景故事:我制作了一个“x”面骰子(这里我们尝试使用 10 个)和一个“假骰子”。 我想实现一个子和超级 class (继承)。

问题:如何使子类“FalskTerning”(FalseDice)获取“Terning”(Dice)产生的随机数的值并将其转换为 if 语句。 示例(如果数字 x 小于 5,则答案为 10)?

此外,我想知道,为什么我不能在我的子类“FalskTerning”的 if 语句中使用超类“Terning”中的“answer”。

祝你有美好的一天!

暂无
暂无

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

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