yxxs123 发表于 2017-5-6 12:08:04

python入门(七)类与对象和类与方法

python入门(七)类与对象和类与方法

参考书籍 python_note_pdq.pdf
11.1 用户定义数据类型
class Point:
    pass
a = Point()
print(a,type(a))
控制台打印:
<__main__.Point object at 0x00B9F1D0> <class '__main__.Point'>

定义了一个自定义的类Point,pass表示暂时没有内容,下面的a就在使用这个类型了。

11.2 属性
class Point:
    pass
a = Point()
a.x = 3.0
a.y = 4.0
print(a.x,a.y)
控制台打印信息:
3.0 4.0

11.3 同一性
class Point:
    pass
a = Point()
a.x = 3.0
a.y = 4.0
b = Point()
b.x = 3.0
b.y = 4.0
print(a==b,id(a),id(b))
c = a
print(a==c,id(a),id(c))
控制打印输出:
False 12186064 12539312
True 12186064 12186064

虽然坐标相同,表示同一个点,但是==并不相同。这里有模糊性。

11.4 长方形类
class Rectangle:
    pass
class Point:
    pass
#查找右上角的点的位置
def findUpperRight(rectangle):
p = Point()
p.x = rectangle.width + rectangle.corner.x
p.y = rectangle.height + rectangle.corner.y
return p
r1 = Rectangle()
r1.width = 10
r1.height = 2
r1.corner = Point()
r1.corner.x = 1
r1.corner.y = 1
up_corner = findUpperRight(r1)
print("(",str(up_corner.x),",",str(up_corner.y),")")
控制台输出:
( 11 , 3 )

11.5 对象拷贝
比如Point没有包含任何嵌入的对象,copy方法已经足够了。这种复制叫做浅拷贝。
import copy
class Point():
    pass
p1 = Point()
p1.x = 2.0
p1.y = 4.0
p2 = p1
print(p1,p2,id(p1),id(p2))
p2 = copy.copy(p1)
print(p1,p2,id(p1),id(p2))
控制台打印信息:
<__main__.Point object at 0x00C0ECF0> <__main__.Point object at 0x00C0ECF0> 12643568 12643568
<__main__.Point object at 0x00C0ECF0> <__main__.Point object at 0x00C451D0> 12643568 12866000

比如Recrangle对象包含了嵌入对象,copy模块包含了一个名为deepcopy 的方法,它可以拷贝任何嵌入的对象。这种拷贝我们称之为深拷贝。
import copy
class Rectangle():
    pass
class Point():
    pass
r = Rectangle()
r.width=20
r.height=10
r.corner = Point()
r.corner.x = 2.0
r.corner.y = 4.0
p = copy.copy(r)
print(p.corner,r.corner,id(p.corner),id(r.corner))
p = copy.deepcopy(r)
print(p.corner,r.corner,id(p.corner),id(r.corner))
控制台打印信息:
<__main__.Point object at 0x00BF55B0> <__main__.Point object at 0x00BF55B0> 12539312 12539312
<__main__.Point object at 0x00C45150> <__main__.Point object at 0x00BF55B0> 12865872 12539312

12.1 面向对象技术
import copy
#自定义类
class Time:
    pass
    #类方法,其中参数 self,打印自己
    def printTime(self):
      print("%2d" % self.hours, ":", \
            "%2d" % self.minutes, ":", \
            "%2d" % self.seconds)
    #类方法,增加多少秒的时间
    def increment(self, seconds):
      self.seconds = seconds + self.seconds
      while self.seconds >= 60:
            self.seconds = self.seconds - 60
            self.minutes = self.minutes + 1
            while self.minutes >= 60:
                self.minutes = self.minutes - 60
                self.hours = self.hours + 1
    #类方法,判断时间先后
    def after(self,time):
      if self.hours > time.hours:
            return 1
      if self.hours < time.hours:
            return 0
      else:
            if self.minute > time.minute:
                return 1
            if self.minute < time.minute:
                return 0
            else:
                if self.second > time.second:
                  return 1
                else:
                  return 0
now = Time()
now.hours = 10
now.minutes = 30
now.seconds = 10
now.printTime()
after = copy.copy(now)
after.increment(60*30)
after.printTime()
print(now.after(after),after.after(now))

控制台打印信息:
10 : 30 : 10
11 :0 : 10
0 1

12.2 可选择的参数
#求和函数
def total(head,tail,step = 1):
    temp = 0
    while head <= tail:
      temp = temp + head
      head = head + step
    return temp
print(total(1,20),total(1,20,2))
控制台打印信息:
210 100

缺省参数的定义要符合以下规则:缺省参数全部位于参数表的后部,而且缺省参数之间不能在有非缺省参数。下面定义是不合法的:
total(head=1, tail, step) #错误的定义
total(head=1, tail, step=1) #错误的定义
total(head, tail=100, step) #错误的定义

12.3 构造函数
#__init__前后两个_的构造函数
class Time:
    def __init__(self,hours=0,minutes=0,seconds=0):
      self.hours = hours
      self.minutes = minutes
      self.seconds = seconds
    def printTime(self):
      print("%2d" % self.hours, ":", \
            "%2d" % self.minutes, ":", \
            "%2d" % self.seconds)
#缺省构造函数
now = Time()
#按照顺序,传递参数
t1 = Time(12,23,22)
#传递子集,可以不按照顺序了
t2 = Time(seconds=12,hours=11)
now.printTime()
t1.printTime()
t2.printTime()
控制台输出:
0 :0 :0
12 : 23 : 22
11 :0 : 12
页: [1]
查看完整版本: python入门(七)类与对象和类与方法