如何从Java中的多重继承中获取所需的返回值?

huangapple 未分类评论55阅读模式
英文:

How to do get needed return vaule from multiple inheritence in java?

问题

我正在做Java谜题,以加深我对Java的理解,但有两个问题真的让我感到困惑:

在这种情况下,我怎么样才能从B类中获得所需的返回值?谢谢。

英文:

I am doing java puzzles to enhance my understanding about java but there two questions really confused me:

How can I get the needed return value from class B in that case? Thx.

答案1

得分: 0

什么是所需值的含义?据我理解,您指的是更改超类的值。
希望这段代码能帮助您。

class A0 {

    int f;

    A0(int f) {
        this.f = f;
    }
}

class A1 extends A0 {

    int f = 0;

    A1(int f) {
        super(f);
        this.f = f;
    }
}

class B extends A1 {

    int f = 0;

    B(int f) {
        super(f);
        System.out.println(super.f);
        this.f = f;
    }

    int getFofA() {
        return f;
    }
}

public class question {

    public static void main(String[] arg) {
        B b0 = new B(5);
        System.out.println(b0.getFofA());
    }
}
英文:

What do you meaning by needed value? As I understand it, you mean changing the value of the super class.
I hope this code will help you.

class A0 {

    int f;

    A0(int f) {
        this.f = f;
    }
}

class A1 extends A0 {

    int f = 0;

    A1(int f) {
        super(f);
        this.f=f;
    }
}

class B extends A1 {

    int f = 0;

    B(int f) {
        super(f);
        System.out.println(super.f);
        this.f=f;
    }

    int getFofA() {
        return f;
    }
}

public class question {

    public static void main(String[] arg) {
        B b0 = new B(5);
        System.out.println(b0.getFofA());
    }
}

huangapple
  • 本文由 发表于 2020年7月23日 11:02:11
  • 转载请务必保留本文链接:https://java.coder-hub.com/63046216.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定