java: inheritance

2019-04-13 00:20发布

What are some alternatives to inheritance?

7条回答
做个烂人
2楼-- · 2019-04-13 00:29

I think you should try delegation, delegation is an alternative to inheritance. Delegation means that you include an instance of another class as an instance variable. and it plays a beneficial role that it doesn't force you to accept all the methods of the super class.

查看更多
女痞
4楼-- · 2019-04-13 00:35

I realise this is not Java per se, but Scala (a language running on the Java Virtual Machine) permits mixins (known as traits in Scala).

Mixins allow you to slot some functionality alongside an existing class, rather than within the inheritance tree.

When a class includes a mixin, the class implements the interface and includes, rather than inherits, all the mixin's attributes and methods. They become part of the class during compilation

查看更多
【Aperson】
5楼-- · 2019-04-13 00:36

I'm assuming you're playing with Java which has some rules about inheritance.

Implementing interfaces is a common and oft used option. So, for example, if you have a data-source class that talks to a RDBMS, rather than inheriting that class and using it to implement a NoSQL datasource, the both can implement the same interface.

Rather than..

public class RDBMSDataSource {

  ...

  public String loadSomeDataFromDataSource() {
    ...Do some stuff...
  }
}

public class NoSQLDataSource extends RDBMSDataSource {

   ...

   @Override
   public String loadSomeDataFromDataSource() {
      ...Do some other stuff...
   }
}

public class DataSourceClient {

   public void foo() {
      RDBMSDataSource ds = new NoSQLDataSource();
      ds.loadSomeDataFromDataSource();
   }
}

Which works but is hard to read, you could use this...

public interface DataSource {
   public String loadSomeDataFromDataSource();
}

public class RDBMSDataSource implements DataSource {

  ...

  public String loadSomeDataFromDataSource() {
    ...Do some stuff...
  }
}

public class NoSQLDataSource implements DataSource {

   ...

   @Override
   public String loadSomeDataFromDataSource() {
      ...Do some other stuff...
   }
}

public class DataSourceClient {

   public void foo() {
      DataSource ds = new NoSQLDataSource();
      ds.loadSomeDataFromDataSource();
   }
}

Another option would be composition. Say you have employees and customers. Your two options would be...

public class Person {

   protected String name;
   protected String address;
   ...More stuff...
}

public class Employee extends Person {

   protected String jobCode;
   protected String department;
   ...More stuff...
}

public class Customer extends Person {

   protected String salesPerson;
   protected Date registrationDate;
   ...More stuff...
}

...or...

public class ContactInfo {
   private String name;
   private String address;
   ...More stuff...
}

public class Employee {

   private ContactInfo contactInfo;
   private String jobCode;
   private String department;
   ...More stuff...
}

public class Customer {

   private ContactInfo contactInfo;
   private String salesPerson;
   private Date registrationDate;
   ...More stuff...
}

Since Java does not have multiple inheritance and you can implement multiple interfaces, you sometimes need to do the above to make sure your development is clean and readable.

查看更多
狗以群分
6楼-- · 2019-04-13 00:42

Delegation is an alternative to inheritance.

查看更多
Summer. ? 凉城
7楼-- · 2019-04-13 00:46

Effective Java: Favor composition over inheritance. (This actually comes from Gang of Four, as well).

The case he makes is that inheritance can cause a lot of unseemly side effects, if the extended class was not explicitly designed to be inherited. For example, any calls to super.someMethod() can lead you through unexpected paths of unknown code.

Instead, hold a reference to the class you would have otherwise extended, and delegate to it.

Here is a link to an interview with Erich Gamma where he talks about the concept a bit.

查看更多
登录 后发表回答