First, the object-oriented basis
Object-oriented noun interpretation:
class: used to describe a collection of objects that have the same properties and methods. It defines the properties and methods that are common to each object in the collection. An object is an instance of a class.
Class variables: class variables are common throughout the instantiated object. Class variables are defined in the class and outside the body of the function. Class variables are not typically used as instance variables.
data members: class variables or instance variables are used to manipulate the data related to the class and its instance objects.
method Overrides: if the method inherited from the parent class does not meet the requirements of the subclass, it can be overridden, which is called the override of the method, also known as the override of the method.
Instance variable: A variable defined in a method that acts only on the class of the current instance.
inheritance: A derived class (derived class) that inherits the fields and methods of the base class. Inheritance also allows the object of a derived class to be treated as a base class object. For example, there is a design where an object of type dog is derived from the animal class, which is the analog "is a (is-a)" Relationship (example, dog is a animal).
instantiation: Creates an instance of a class, the concrete object of the class.
method: a function defined in a class. (Method belongs to class, not instance)
object: An instance of a data structure defined by a class. The object consists of two data members (class variables and instance variables) and methods.
Ii. definition of a class
| 123456789101112131415161718192021222324252627282930313233343536373839404142 |
class Foo(object): # Foo为类名,括号内的表示是这个类继承自哪个类,这里的object是所有类的基类,一个类可以继承自多个类 ‘‘‘ 类的说明文档,Python会自动将这里面内容赋值给类的变量__doc__ ‘‘‘ class_var1 = ‘var1‘ # 类的变量,它属于类本身,而不是类的对象,可以通过类名.变量名的方式进行调用,也可以对象.变量名调用,前提是,没有同名的实例变量 def __init__(self, arg, arg2): ‘‘‘ 初始化方法,有点类似于java语言的构造方法,在创建类的对象的时候自动调用 :param var2: 参数,初始化方法也可以 :return: ‘‘‘ self.var2 = arg # 对象的变量,属于对象不属于类,只能通过对象.变量名的方式调用 self.__var3 = arg # 对象的变量,只能在内部调用,不能通过对象.变量名的方式调用,并且不能被继承 def func1(self, arg): ‘‘‘ 方法 1、方法属于类(也就是在实例化的时候不会像对象的变量一样单独开辟内存空间) 2、self表示类的对象本身,当我们通过对象名.方法()来调用的时候,解释器会自动将对象作为第一个参数传给方法,方法名.方法(对象名) :return: ‘‘‘ # 方法体,方法体可以通过self.关键字调用对象的变量和方法 self.__var3 = arg # 方法可以调用私有变量和方法 def __func2(self): ‘‘‘ 私有方法,和私有变量一样,不能被继承和外部调用 :return: ‘‘‘ passclass Foo2(Foo): def __init__(self, arg, arg2): ‘‘‘ 1、这里调用了父类的初始化方法,特别注意由于“__变量名”表示的私有方法, 2、这里尽管调用了父类的构造方法,__var3变量作为父类的私有方法,子类在没有重新定义之前依然没有这个变量,这点要特别注意 :param arg: :param arg2: :return: ‘‘‘ super(Foo2, self).__init__(arg, arg2) self.__var3 = arg2 # 由于__var3是父类的私有方法,尽管调用了父类的初始化方法,子类依然不会有,所以依然需要重新定义 |
Three main features of object-oriented 1, package
encapsulation, which is to save the content to a place, easy to invoke and modify, such as a variable encapsulated into a class, so that the invocation of the class instantiation, through the object name. Variable name to invoke and modify the variable.
| 12345678 |
class Role(object): def __init__(self, name): self.name = name # 定义一个实例变量 def get_name(self): return self.nameif __name__ == ‘__main__‘: zhangsan = Role(‘zhangsan‘) # 创建实例 print(zhangsan.name) # 调用实例的变量name |
Show results
The content is encapsulated in the Zhangsan variable, called directly by the Zhangsan instance of the call can be
2. Inheritance
Inheritance is the way a class can inherit another class, the methods and variables of another class, and so on. Is the same as the inheritance in reality.
| 12345678910111213141516171819 |
class Role(object): def __init__(self, name): self.name = name def get_name(self): return self.nameclass Teacher(Role): def __init__(self, name, course): ‘‘‘ 如果父类已经有一个方法,子类也有一个同名的方法,就会覆盖掉父类的方法,专业术语叫做重写 ‘‘‘ super(Teacher, self).__init__(name) # 通过super这种语法可以调用父类的方法和变量,这里调用父类的构造方法,初始化name self.course = course # 这个变量是父类所没有的 def say(self): # 定义父类的 print(‘My name is %s, i am a English teather‘ %self.name)if __name__ == ‘__main__‘: lisi = Teacher(‘lisi‘, ‘English‘) # 定义Teacher的实例 print(lisi.name) # name这个变量是子类通过继承的方式获取的 print(lisi.get_name()) # 继承自父类的方法 lisi.say() # 子类特有的方法 |
Execution results
| 123 |
lisilisiMy name islisi, i am a English teather |
3. polymorphic
Polymorphism is for interface reuse, a technique that allows you to set the parent object to be equal to one or more of his child objects, and after assignment, the parent can operate differently depending on the attributes of the child object currently assigned to it. To put it simply, it is a sentence: A pointer to the parent class type is allowed to be assigned a pointer to the child class type.
Python does not support polymorphism, and it does not use polymorphic
From for notes (Wiz)
My Python growth path---sixth day---python Foundation---February 20, 2016 (Sunny)