Interface versus concrete class

前端 未结 5 751
北荒
北荒 2021-02-05 12:06

Below I have a Person interface, an implementing class and a driver class which initialises the Person with a name and just outputs it again. What is the advantage of using

5条回答
  •  耶瑟儿~
    2021-02-05 13:02

    One advantage I can think of is that you might have 2 very different types of people, but you want to wait until runtime (based on user input, config file, etc.) to decide which one to use. You can do something like this:

    Person person = null;
    if ...
       person = new PersonImpl();
    else 
       person = new PersonImpl2();
    

提交回复
热议问题