1、在python中,首字母大写的名称指的是类
1)方法_init_()
是一个特殊的方法,每当根据类创建新实例时,python都会自动运行它。_init_()至少包含一个形参self,还必须位于其他形参前面。当python调用这个_init_()方法来创建实例时,都自动传入实参self,每个与类相关联的方法调用都自动传递实参self,它是一个指向实例本身的引用,让实例能够访问类中的属性和方法。
2)以self为前缀的变量可供类中的所有方法是用,我们可以通过类中的任何实例来访问这些变量。像这样可通过实例访问的变量成为属性。
3)实例:
#helloword.py
class Dog():
def _init_(self,name,age):
self.name=name
self.age=age
def sit(self):
print(self.name.title()+" is now sitting.")
def roll_over(self):
print(self.name.title()+" rolled over!")
2、根据类创建实例
#helloword.py
class Dog():
def __init__(self,name,age):
self.name=name
self.age=age
def sit(self):
print(self.name.title()+" is now sitting.")
def roll_over(self):
print(self.name.title()+" rolled over!")
my_dog=Dog('jin',21)
print("My dog name is "+my_dog.name.title()+".")
print("My dog age is "+str(my_dog.age)+".")
my_dog.sit()
my_dog.roll_over()
输出为:
D:\www>python helloword.py
My dog name is Jin.
My dog age is 21.
Jin is now sitting.
Jin rolled over!
方法__init__()并未显式的包含return语句,但python自动返回一个表示这个小狗的实例。
3、继承
1)创建子类时,父类必须包含在当前文件中,且位于子类前面。super()是一个特殊的函数,帮助python将父类和子类关联起来。
#helloword.py
class Dog():
def __init__(self,name,age):
self.name=name
self.age=age
def sit(self):
print(self.name.title()+" is now sitting.")
def roll_over(self):
print(self.name.title()+" rolled over!")
class LittleDog(Dog):
def __init__(self,name,age,weight):
super().__init__(name,age)
self.weight=weight
my_dog=LittleDog('jin',21,45)
print("My dog name is "+my_dog.name.title()+".")
print("My dog age is "+str(my_dog.age)+".")
print("My dog weight is "+str(my_dog.weight)+".")
my_dog.sit()
my_dog.roll_over()
输出为:
D:\www>python helloword.py
My dog name is Jin.
My dog age is 21.
My dog weight is 45.
Jin is now sitting.
Jin rolled over!
2)重写父类中的方法
使用继承时,可让子类保存从父类那里继承来的精华,剔除不必要的糟粕。
#helloword.py
class Dog():
def __init__(self,name,age):
self.name=name
self.age=age
def sit(self):
print(self.name.title()+" is now sitting.")
def roll_over(self):
print(self.name.title()+" rolled over!")
class LittleDog(Dog):
def __init__(self,name,age,weight):
super().__init__(name,age)
self.weight=weight
def sit(self):
print("LittleDog "+self.name.title()+" is now sitting.")
my_dog=LittleDog('jin',21,45)
print("My dog name is "+my_dog.name.title()+".")
print("My dog age is "+str(my_dog.age)+".")
print("My dog weight is "+str(my_dog.weight)+".")
my_dog.sit()
my_dog.roll_over()
输出为:
D:\www>python helloword.py
My dog name is Jin.
My dog age is 21.
My dog weight is 45.
LittleDog Jin is now sitting.
Jin rolled over!
3)将实例用作属性
#helloword.py
class Dog():
def __init__(self,name,age):
self.name=name
self.age=age
def sit(self):
print(self.name.title()+" is now sitting.")
def roll_over(self):
print(self.name.title()+" rolled over!")
class Meat():
def __init__(self,weight=10):
self.weight=weight
def show(self):
print("the meat weight is "+str(self.weight))
class LittleDog(Dog):
def __init__(self,name,age,weight):
super().__init__(name,age)
self.weight=weight
self.meat=Meat()
def sit(self):
print("LittleDog "+self.name.title()+" is now sitting.")
my_dog=LittleDog('jin',21,45)
print("My dog name is "+my_dog.name.title()+".")
print("My dog age is "+str(my_dog.age)+".")
print("My dog weight is "+str(my_dog.weight)+".")
my_dog.meat.show()
my_dog.sit()
my_dog.roll_over()
输出为:
D:\www>python helloword.py
My dog name is Jin.
My dog age is 21.
My dog weight is 45.
the meat weight is 10
LittleDog Jin is now sitting.
Jin rolled over!