我的python学习笔记.创建和使用类
2017-09-28 10:57
369 查看
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 na
4000
me 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!
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 na
4000
me 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!
相关文章推荐
- python学习笔记(九)创建和使用类
- Python下字符串的创建和转义字符的使用 - 千月的python linux 系统管理指南学习笔记(10)
- python学习笔记.4使用列表的一部分,创建切片
- mongoDB学习笔记3--python下使用
- Windows编程 C++ DLL编程 学习笔记(一)静态链接库的创建和使用
- [Linux网络编程学习笔记]FIFO的创建和使用
- php学习笔记(7):PHP数组的创建修改使用
- python学习笔记2-强大的动态创建
- [学习笔记]使用py2exe打包python程序
- Flex企业应用开发实践学习笔记(六)——使用ActionScript创建自定义组件
- Python学习笔记IDLE使用
- 【PHP学习笔记】使用ZipArchive 创建zip包,总是不成功问题探究
- 学习笔记之使用域名创建虚拟主机
- 动态链接库创建与使用(学习笔记) .
- iPhone开发学习笔记[3/50]-使用工具栏创建简单的多视图应用程序
- [原创]W2k Driving 学习笔记(二)使用GCC创建 Windows NT 下的内核DLL
- Python学习笔记十二:列表(4)--len 、in、For的使用
- 动态链接库创建与使用(学习笔记)
- [原创]W2k Driving 学习笔记(二)使用GCC创建 Windows NT 下的内核DLL
- 动态链接库创建与使用(学习笔记)