catalogue
Object Oriented Programming
Class and object are two main aspects of object-oriented programming.
A Class can create a new Type, where an Object is an Instance of the Class.
You can have variables of type int, that is, variables that store integers are instances (objects) of the int class.
Attribute of class:
1. Field: an object can use its ordinary variables to store data. Such variables subordinate to an object or class are called fields. The two types of fields are class variables and object variables.
Fields and ordinary variables: fields are ordinary variables Bound to the Namespace of classes and objects.
Class Variable: shared and accessed by all instances in the class. There is only one copy. When any object changes the Class Variable, the changes will be reflected in all other instances.
The population in the following example needs sutdent Population.
Object variable: it is owned by each independent object or instance of the class and is not shared. Each object has dependent fields and copies, which are not associated.
For the name in the following example, you need to use self Name.
class Student: population = 0 def __init__(self, name): self.name = (name) print ('Here comes a student: {}'.format(self.name)) Student.population += 1 def graduate(self): print('{} graduates! Congratulations!'.format(self.name)) Student.population -= 1 if Student.population == 0: print('All students graduated!') else: print('We have {:d} students in our class.'.format(Student.population)) @classmethod def how_many(cls): print('We have {:d} students in our class'. format(cls.population)) stu1 = Student('Sarah') stu2 = Student('Sandra') stu3 = Student('Sara') Student.how_many() stu1.graduate() stu2.graduate() stu3.graduate()
Here comes a student: Sarah Here comes a student: Sandra Here comes a student: Sara We have 3 students in our class Sarah graduates! Congratulations! We have 2 students in our class. Sandra graduates! Congratulations! We have 1 students in our class. Sara graduates! Congratulations! All students graduated!
How here_ Many is a function corresponding to the classmethod modifier. This kind of function does not need instantiation and self parameter when calling, but the first parameter needs to be the cls parameter representing its own class.
2. Method: objects can also use functions belonging to classes to realize some functions. This function is called class method.
Class methods and ordinary functions: class methods must add an additional parameter at the beginning of the parameter list. By convention, it is given the name self. However, no assignment is required when calling.
class Person: def say_hi(self): print('Hello, how are you?') p = Person() p.say_hi() # The first two lines can also be written # Person().say_hi()
Hello, how are you?
__ init__ method
Run immediately when the object of the class is instantiated (created), which is used to initialize the object.
class Person: def __init__(self, name): self.name = name def say_hi(self): print('Hello, how are you?', self.name) Person('Vio').say_hi()
Hello, how are you? Vio
Inheritance
Inheritance implements the relationship between types and subtypes between classes.
The base class can be used as a public class so that subclasses can inherit common features from this class, that is, they will become subtypes of this type (class), and we can add some unique features of this class to these subtypes.
In order to use inheritance, you need to define a class followed by a tuple containing the base class name.
class Subclass(Superclass)
In the following example, the SchoolMember class will be called a Base Class or a Superclass. The Teacher and Student classes will be called Derived Classes or subclasses.
class SchoolMember: def __init__(self, name, age): self.name = name self.age = age print('Initialized SchoolMember: {}'.format(self.name)) def tell(self): print('Name: "{}" Age: "{}"'.format(self.name, self.age), end=' ') class Teacher(SchoolMember): def __init__(self, name, age, salary): SchoolMember.__init__(self, name, age) self.salary = salary print('Initialized Teacher: {}'.format(self.name)) def tell(self): SchoolMember.tell(self) print('Salary: "{}"'.format(self.salary)) class Student(SchoolMember): def __init__(self, name, age, marks): SchoolMember.__init__(self, name, age) self.marks = marks print('Initialized Student: {}'.format(self.name)) def tell(self): SchoolMember.tell(self) print('Marks: "{}"'.format(self.marks)) t = Teacher('Cecile', 30, 40000) s = Student('Vio', 24, 100) members = [t, s] for member in members: member.tell()
Initialized SchoolMember: Cecile Initialized Teacher: Cecile Initialized SchoolMember: Vio Initialized Student: Vio Name: "Cecile" Age: "30" Salary: "40000" Name: "Vio" Age: "24" Marks: "100"