简体   繁体   English

两个具体类之间的抽象类 - java

[英]Abstract class between two concrete class - java

So essentially, I have sandwhiched an abstract class between two concreate classes and would like the bottom-class of this sandwich to be able to call all the name of the two parent classes.所以本质上,我在两个 concreate 类之间添加了一个抽象类,并希望这个三明治的底层类能够调用两个父类的所有名称。 This doesn't work, I would like to know why?这不起作用,我想知道为什么? Not the code, I wrote that without the computer just the logic.不是代码,我在没有计算机的情况下编写的只是逻辑。

The output I expect after calling get name would be: customer!middleMan!myShop!调用 get name 后我期望的输出是:customer!middleMan!myShop!

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();
}

Working Code:工作代码:

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());
    }
}

Your Mistake: Although there were many but few that are imp are你的错误:虽然有很多但很少是小鬼

  • you were using return in your getName() but you were not defining the function to return anything.您在 getName() 中使用了 return ,但没有定义返回任何内容的函数。

    Like :喜欢 :

 public String getName() { return this.name + super.getName(); }
  • You were not displaying any outcome of the code.您没有显示代码的任何结果。

    Like :喜欢 :

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

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

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