When to implement and extend? [closed]

巧了我就是萌 提交于 2019-12-20 08:13:07

问题


  • When should implement or extend be used?
  • What are some real-world examples?

Is this correct?

Implementing appears to be a way to enforce that certain methods exists in a class, and that these methods function calls are properly formatted. Implementing is not a way of passing variables or "settings" to the class though?

Expected real-life scenario: I have an e-commerce platform featuring multiple payment classes that all follow the same design. When a new payment class should be added, it's really easy to follow the defined design of the interface to ensure that all bits and pieces are there, from the beginning.

Extending classes makes the extended (child?) class inherit everything from its parent class except methods & variables declared as private?

Expected real-life scenario: I have one class called sessions with two child classes named sessioncookies and databasesessions. sessioncookies and databasesessions, together inherit a number of mutual config options from their parent sessions, making it easy to change a config option to affect all sorts of eventual visitor data storage.


回答1:


Inheritance is useful to reduce the amount of code you rewrite. If you have several classes with a few common methods or fields, instead of defining these methods and fields over and over you can factor them into a base class and have each of the child classes extend that base class.

Interfaces (and implements) are useful when you'd like to define a common protocol for how a group of objects should behave. For example, you might want to mandate that objects that are comparable can be compared for equality and hashed, etc.

Using inheritance is ultimately a design choice. Be on the lookout for cases where you define the same methods across several classes; those are excellent cases where you can factor those methods out into a base class. The same goes for classes that observe some of the same characteristics: you can guarantee consistency by putting those characteristics in an interface to be implemented by those related classes.

Inheritance is a big concept in OOP that goes way beyond just PHP. I recommend you read the wikipedia article on inheritance and perhaps Design Patterns by the Gang of Four.

I believe your understanding of inheritance is mainly correct. The next step would be to use it in production.




回答2:


Implement:

Interfaces are abstract classes, so you can only declare things. A class implements an interface. You can implement multiple interfaces.

Extend:

You extend classes when you want a more specific version of a class.

Example:

// Contract: a pet should play
public interface Pet {
    public void play(); 
}

// An animal eats and sleeps
class Animal {
    public void eat();
    public void sleep();
}


public class Dog extends Animal implements Pet {

    public void play() {...}
}


public class Camel extends Animal {
}

Both Camel and Dog are Animals, so they extend Animal class. But only the Dog can be a Pet!



来源:https://stackoverflow.com/questions/4961906/when-to-implement-and-extend

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!