类的方法:
在类的内部,使用def关键字可以为类定义一个方法,与一般函数定义不同,类方法必须包含参数self,且为第一个参数。
init()方法是一种特殊的方法,被称为类的构造函数或初始化方法,当创建了这个类的实例时就会调用该方法
在python3中,所有类最顶层父类都是object类,与java类似,如果定义类的时候没有写出父类,则object类就是其直接父类
如何条用下面类中的study方法?
1 #!/usr/bin/env python3
2 class People:
3 def init(self,name,age):
4 print("i can work")
5 self.name = name
6 self.age = age
7 print(self.name)
8 print(self.age)
9 def study(score):
10 score += 1
11 print("your score is:" ,score)
12 p = People("wengwu","40",100)
13 p.study(score)
解决方法如下,注意两次代码中的第12行代码
1 #! /usr/bin/env python3
2 class People:
3 def init(self,name,age):
4 print("i can work")
5 self.name = name
6 self.age = age
7 print(self.name)
8 print(self.age)
9 def study(self,score):
10 score += 1
11 print("your score is:" ,score)
12 p = People("wengwu",40)
13 p.study(100)
我的问题是:如果不写第8行代码,如何访问到i:将第8行代码修改为
self.j = 200
1 #!/usr/bin/env python3
2 class Complex:
3 def init(self,realpart,imagpart):
4 self.p = realpart
5 self.q = imagpart
6 print("this is test")
7 j = 200 【 方法中的属性】
8 print(j)
9 i = 100 【类中的属性】
10 x = Complex(3.0, -4.5)
11 print(x.p)
12 print(x.q)
13 print(x.i)
说明:
1、下面代码演示了类属性的条用
2、在for循环中Geese()执行了4次
3、将第14行代码去掉, 将第16行代码修改为Geese() ,执行效果一样
1 #! /usr/bin/env python3
2 class Geese():
3 neck = "this is neck"
4 wing = "this is wing"
5 leg = "this is leg"
6 number = 0
7 def init(self):
8 Geese.number += 1
9 print("i am is number of",Geese.number,"geese")
10 print(Geese.neck)
11 print(Geese.wing)
12 print(Geese.leg)
13
14 list = []
15 for i in range(4):
16 list.append(Geese())
17 print("the total number of Geese is:",Geese.number)
继承:如下定义了两个类, 从执行结果来看,第10行代码执行的是父类中的“构造方法”,第11行代码执行的是子类自己的实例方法; 如果在student类中增加init__方法,则如下
第10行代码将执行student类中的__init__方法
1 #!/usr/bin/env python3
2 class Person():
3 def __init(self):
4 print("person can eat food")
5 class Student(Person):
6 def study(self,python):
7 self.p = python
8 print("I am studing"+" " + self.p)
9 person1 = Person()
10 student1 = Student()
11 student1.study("python")
下面的例子也是子类继承父类的,执行结果为:
i am a student
jack
wengwu
根据上面的执行结果可以看到,类中的可执行语句,比如print ,会执行
1 #! /usr/bin/env python3
2 class Person(object):
3 """
4 it will return the Person
5 """
6 def init(self,name):
7 self.n = name
8 print(self.n)
9 def get_detail(self):
10 return self.n
11 class Student(Person):
12 print("i am a student")
13 person1 = Person("jack")
14 student1 = Student("wengwu")
@property 装饰器,将方法转化为属性
1 #! /usr/bin/env python3
2 class Tec():
3 def init(self,wigth,heigh):
4 self.wigth = wigth
5 self.heigh = heigh
6 @property
7 def area(self):
8 return self.wigth * self.heigh
9 print(Tec(60,50).area)
改版:
1 #! /usr/bin/env python3
2 class Account(object):
3 def init(self,rate):
4 #self.amt = 10 【 也可以正常执行,此处相当于设置初始值】
5 self.rate = rate
6 @property
7 def amount(self):
8 return self.__amt
9 @property
10 def cny(self):
11 return self.__amt * self.rate
12 @amount.setter
13 def amount(self,value):
14 if value < 0:
15 print("sorry, no negative amount in the account.")
16 return
17 self.__amt = value
18 #if __name == 'main':
19 acc = Account(6.6)
20 acc.amount = 80
21 print("Dollar amount:", acc.amount)
22 print("In CNY:",acc.cny)
23 acc.amount = -100
24 print("Dollar amount:", acc.amount)
如果把17行注释掉,则执行结果总是如下:
Dollar amount:10
In CNY :66.0
sorry ....
Dollar amount:10