Class
is an object-oriented vector
definition of the class:
class A (object): pass 全局变量 函数1。。。 def....(self): 函数2.。。。 def....():
There are many functions in the class, the first parameter of the function is self
Variables can be directly defined directly within the class
When a class calls a function or variable internally, it must use self .
Self represents the individual after the class instantiation
Instantiate the class with the first letter lowercase to represent the class, and then instantiate the
Constructors for classes
class A (object): def __init__(self,name): self.name=name
Is the function that the class executes first when it is instantiated.
Inherited:
class A(object): def __init__(self,name): self.name=name def student(self): print ("please tall my name")class B (A): def grand(self): print("i will be back")a=A("huang")
b invokes the attribute in a, which teaches inheritance
Rewrite:
class A(object): def __init__(self,name): self.name=name def student(self): print ("please tall my name")class B (A):def __init__(self): print("this is test")def grand(self): print("i will be back")def student(self): print("my name is tom")B.student()
A has student,b also has the student, if has the init constructor in B, overrides the function, calls the Class B's student, does not go to the parent class to look for
To give a simple example:
class 人(object): def __init__(self,name): self.name=name 有两条腿 有年龄 有头发class 男人(人): 有妻子: 有胡子:class女人(人): 有丈夫:class和尚(人): def __init__(self): 没有头发
That's basically what it means,
A man has two legs, an age, a wife, a beard, and a male heir to the hair.
A woman has two legs, an age, a husband, a woman with hair, a man's property,
Monk has two legs, age, no hair, hair this attribute and the parent class (person) conflict, then need to rewrite, need to have a constructor
Introduction to Python (vii) class