【问题标题】:Abstract class between two concrete class - java两个具体类之间的抽象类 - java
【发布时间】:2018-02-18 18:00:15
【问题描述】:

所以本质上,我在两个 concreate 类之间添加了一个抽象类,并且希望这个三明治的底层类能够调用两个父类的所有名称。 这不起作用,我想知道为什么?不是代码,我在没有计算机的情况下只写了逻辑。

调用 get name 后我期望的输出是: 客户!中间人!我的商店!

public abstract class StoreClerk extends Shop { 
    string name =  "MiddleMan!";

    public getName {
        return this.name + super.getName();
    }

}

public class Shop {
    string name = "myShop!";

    public getName() {
        return this.name;
    }
}

public class Customer extends StoreClerk {
    string name = "customer!";

    public getName() {
        return this.name + super.getName();
    }
}

public class void main(String args[]) {   
    Customer ibrahim = new Customer();
    ibrahim.getName();
}

【问题讨论】:

  • 对不起,如果这已经发布了!
  • string 在 Java 中不存在
  • @Li357 他写道这是伪代码
  • @payloc 在哪里?致 OP:这种继承没有意义。店员不是商店。商店可以有店员。顾客也不是店员,但商店可以有顾客..
  • @Li357 Not the code, I wrote that without the computer just the logic. 猜它代表pseudocode。不过,你是对的,代码在概念上是错误的。另外,易卜拉欣,如果你不是在电脑上做的,你怎么知道它不起作用?

标签: java class inheritance abstract


【解决方案1】:

工作代码:

abstract class StoreClerk extends Shop {
    String name = "MiddleMan!";

    public String getName() {
        return this.name + super.getName();
    }

}

class Shop {
    String name = "myShop!";

    public String getName() {
        return this.name;
    }
}

class Customer extends StoreClerk {
    String name = "customer!";

    public String getName() {
        return this.name + super.getName();
    }
}

public class Store {
    public static void main(String args[]) {
        Customer ibrahim = new Customer();
        System.out.println(ibrahim.getName());
    }
}

你的错误: 虽然有很多但很少是小鬼

  • 您在 getName() 中使用了 return,但您没有定义返回任何内容的函数。

    点赞:

public String getName() {
      return this.name + super.getName();
  }
  • 您没有显示代码的任何结果。

    点赞:

System.out.println(ibrahim.getName());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-17
    • 2020-08-26
    • 2011-01-08
    • 1970-01-01
    相关资源
    最近更新 更多