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

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

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

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

  1. class A0 {
  2. int f;
  3. A0(int f) {
  4. this.f = f;
  5. }
  6. }
  7. class A1 extends A0 {
  8. int f = 0;
  9. A1(int f) {
  10. super(f);
  11. this.f = f;
  12. }
  13. }
  14. class B extends A1 {
  15. int f = 0;
  16. B(int f) {
  17. super(f);
  18. System.out.println(super.f);
  19. this.f = f;
  20. }
  21. int getFofA() {
  22. return f;
  23. }
  24. }
  25. public class question {
  26. public static void main(String[] arg) {
  27. B b0 = new B(5);
  28. System.out.println(b0.getFofA());
  29. }
  30. }
英文:

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.

  1. class A0 {
  2. int f;
  3. A0(int f) {
  4. this.f = f;
  5. }
  6. }
  7. class A1 extends A0 {
  8. int f = 0;
  9. A1(int f) {
  10. super(f);
  11. this.f=f;
  12. }
  13. }
  14. class B extends A1 {
  15. int f = 0;
  16. B(int f) {
  17. super(f);
  18. System.out.println(super.f);
  19. this.f=f;
  20. }
  21. int getFofA() {
  22. return f;
  23. }
  24. }
  25. public class question {
  26. public static void main(String[] arg) {
  27. B b0 = new B(5);
  28. System.out.println(b0.getFofA());
  29. }
  30. }

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:

确定