我的python学习笔记.创建和使用类

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!



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值