Object-oriented programming (OOP) is a style of programming which, as the name suggest, is based around using objects. An object is an instance of a class, which can either be a basic class defined by the programming language, or a new class created by the programmer. OOP focuses on using custom classes to write code in a more intuitive way.
Each class can have attributes and methods. An attribute is another object; it could even be the same type of object, as in trees. Having all the relevant data bundled into a single object makes it easier to work with, rather than keeping track of a group of different variables and what they refer too. A method is a kind of function associated with the class. By importing a class you can then also gain access to all the methods of that class. A method is typically something that every member of that class should be able to do: for instance any Rectangle object should be able to compute its area (assuming it had length and width attributes).
An important part of OOP is subclasses. A subclass is a specialized version of another class. It might have more attributes, more methods, or even alter the methods of the parent class. Subclasses are used when there is a conceptual link between two or more types of objects. Many times the parent class is not actually usable, but by using multiple subclasses of it, code can become simpler. If the parent class has a method, your code can call that method on an object without knowing which of the subclasses that object is (of course you have to be sure that it is one of them rather than another class or None). Classes and subclasses make code easier to understand and are useful tools for programmers.
No comments:
Post a Comment