(2021.05.10)
Each object is an instance of a class. The class definition typically specifies instance variables, a.k.a. data members, that the object contains, as well as the methods, a.k.a. member functions, that the object can execute.
Object-Oriented Programming (OOP) design goals
Software implementations should achieve robustness, adaptability, and reusability.
Robustness
A program produces the right output for all the anticipated inputs in the program's application. Robust, i.e., being capable of handling unexpected inputs that are not explicitly defined for its application.
Adaptability
Software needs to be able to evolve over time in response to changing conditions. Adaptablity, or evolvability, or portability, which is the ability of software to run with minimal change on different hardware and operating system platforms.
Reusability
Going hand in hand with adaptability is the desire that software be reusable, i.e., the same code should be usable as a component of different systems in various applications.
Design Priciples
Modularity
(2021.05.11 Tues)
Modularity refers to an organising principle in which different components of a software system are divided into separate functional units.
The use of modularity helps support the goal of robustness, adaptivity and reusability. Robustness is greatly increased because it is easier to test and debug separate components before they are integrated into a larger software system. Furthermore, bugs that persist in a relative isolation. The structure imposed by modularity also helps enable software reusability. If software modules are written in a general way, the modules can be reused when related need arises in other contexts. This is particularly relevant in a study of data structures, which can typically be designed with sufficient abstraction and generality to be reused in many applications.
Abstraction
(2021.05.17 Mon)
Abstraction: distill a complicated system down to its most fundamental parts. Applying the abstraction paradigm to the design of data structure gives rise to abstract data types (ADTs). An ADT is a mathematical model of a data structure that specifies the type of data stored, the operations supported on them, and the types of parameters of the operations. An ADT specifies what each operation does, but not how it does it. It is refered to the collective set of behaviors supported by an ADT as its public interface.
More formally, Python supports abstract data types using a mechanism known as an abstract base class (ABC). An abstract base class cannot be instantiated, but it defines one or more common methods that all implementations of the abstraction much have. An ABC is realized by one or more concrete classes that inherit from the abstract base class while providing implementations for htose method declared by the ABC.
Encapsulation
Different components of a software system should not reveal the internal details of their respective implementations. One of the main advantages of encapsulation is that **it gives one programmer freedom to implement the details of a component, without concern that other programmers will be writing code that intricately depends on those internal decisions. The only constraint: to maintain the public interface for the component, as other programmers will be writing code that depends on that interface.
By convention, names of members of a class that starts with a single underscore character (e.g., _secret) are assumed to be nonpublic and should not be relied upon.
Class definitions
(2021.05.18 Wed)
A class provides a set of behaviors in the form of member functions (a.k.a methods), with implementations that are common to all instances of that class. Attributes=fields=instance variables=data members.
The self identifier
The special parameter, self, serves to identify the particular instance upon which a member is invoked(调用).
In Python, the self identifier plays a key role. In the context of a class, there can presumably be many instances, and each must maitain its own instance variables to reflect its current state.
self identifies the instance upon which a method is invoked.
(self如同一个结构体,存储在init()里定义的类变量attribute,但凡类方法调用类变量,相当于从self中调用这些变量;self也包含方法)
The Constructor
A method named _ _ init _ _ serves as the constructor of the class. Its primary repsponsibility is to establish the state of a newly created object with appropriate instance variables.
Encapsulation
A single leading underscore in the name of a data member, such as _balance, implies that it is intended as nonpublic. User of a clas should not directly access such members.
Testing the class
refer to Python module unitest.
Reference
- M. T. Goodrich and etc, Data Structures & Algorithms in Python.