day16

作者: 曹操贼6 | 来源:发表于2017-11-14 21:42 被阅读0次

                                                   定义__str__()方法

class    Car:

      def     __init__(self, newWheelNum, newColor):

             self.wheelNum = newWheelNum

             self.color = newColor

      def    __str__(self):

      msg ="嘿。。。我的颜色是"+ self.color +"我有"+ int(self.wheelNum) +"个轮胎..."

      return   msg

     def   move(self):

            print('车在跑,目标:夏威夷')

BMW = Car(4,"白色")

print(BMW)

                                                    总结

·在python中方法名如果是__xxxx__()的,那么就有特殊的功能,因此叫做“魔法”方法

当使用print输出对象的时候,只要自己定义了__str__(self)方法,那么就会打印从在这个方法中return的数据

                                                   self

#定义一个类

class   Animal:

        #方法

     def    __init__(self, name):

          self.name = name

     def    printName(self):

          print('名字为:%s'%self.name)

 #定义一个函数

     def   myPrint(animal):

          animal.printName()

dog1 = Animal('西西')

myPrint(dog1)

dog2 = Animal('北北')

myPrint(dog2)

运行结果:

名字为:西西

名字为:北北

                                                            总结

所谓的self,可以理解为自己

可以把self当做C++中类里面的this指针一样理解,就是对象自身的意思

某个对象调用其方法时,python解释器会把这个对象作为第一个参数传递给self,所以开发者只需要传递后面的参数即可

                                          分析“烤地瓜”的属性和方法

示例属性如下:

cookedLevel :这是数字;0~3表示还是生的,超过3表示半生不熟,超过5表示已经烤好了,超过8表示已经烤成木炭了!我们的地瓜开始时时生的

cookedString :这是字符串;描述地瓜的生熟程度

condiments :这是地瓜的配料列表,比如番茄酱、芥末酱等

示例方法如下:

cook():把地瓜烤一段时间

addCondiments():给地瓜添加配料

__init__():设置默认的属性

__str__():让print的结果看起来更好一些

                                       定义类,并且定义__init__()方法

#定义`地瓜`类

class   SweetPotato:

    '这是烤地瓜的类'

   #定义初始化方法

       def    __init__(self):

            self.cookedLevel = 0

            self.cookedString ="生的"

            self.condiments = []

                                         添加"烤地瓜"方法

#烤地瓜方法

    def    cook(self, time):

         self.cookedLevel += time

         if     self.cookedLevel >8:

                self.cookedString ="烤成灰了"

        elif   self.cookedLevel >5:

               self.cookedString ="烤好了"

        elif   self.cookedLevel >3:

               self.cookedString ="半生不熟"

      else:

              self.cookedString ="生的"

                     基本的功能已经有了一部分,赶紧测试一下

把上面2块代码合并为一个程序后,在代码的下面添加以下代码进行测试

mySweetPotato = SweetPotato()

print(mySweetPotato.cookedLevel)

print(mySweetPotato.cookedString)

print(mySweetPotato.condiments)

完整的代码为:

class   SweetPotato:

 '这是烤地瓜的类'

  #定义初始化方法

     def    __init__(self):

          self.cookedLevel =0

          self.cookedString ="生的"

          self.condiments = []

       #烤地瓜方法

    def    cook(self, time):

          self.cookedLevel += time

     if     self.cookedLevel >8:

          self.cookedString ="烤成灰了"

    elif    self.cookedLevel >5:

          self.cookedString ="烤好了"

     elif   self.cookedLevel >3:

          self.cookedString ="半生不熟"

    else:

          self.cookedString ="生的"

#用来进行测试

mySweetPotato = SweetPotato()

print(mySweetPotato.cookedLevel)

print(mySweetPotato.cookedString)

print(mySweetPotato.condiments)

                                      测试cook方法是否好用

在上面的代码最后面添加如下代码:

print("------接下来要进行烤地瓜了-----")

mySweetPotato.cook(4)#烤4分钟

nt(mySweetPotato.cookedLevel)

print(mySweetPotato.cookedString)

                             定义addCondiments()方法和__str__()方法

def    __str__(self):

     msg = self.cookedString +"地瓜"

     if     len(self.condiments) >0:

            msg = msg +"("

            for emp in self.condiments:

                    msg = msg + temp +", "

            msg = msg.strip(", ")

            msg = msg +")"


      return    msg

defaddCondiments(self, condiments):

self.condiments.append(condiments)

                                             再次测试

完整的代码如下:

class   SweetPotato:

  "这是烤地瓜的类"

  #定义初始化方法

    def   __init__(self):

        self.cookedLevel =0

        self.cookedString ="生的"

        self.condiments = []

#定制print时的显示内容

   def   __str__(self):

         msg = self.cookedString +"地瓜"

      if     len(self.condiments) >0:

             msg = msg +"("

       for  temp in self.condiments:

          msg = msg + temp +", "

           msg = msg.strip(", ")

        msg = msg +")"

  return   msg

#烤地瓜方法

def    cook(self, time):

        self.cookedLevel += time

    if    self.cookedLevel >8:

         self.cookedString ="烤成灰了"

   elif  self.cookedLevel >5:

         self.cookedString ="烤好了"

   elif   self.cookedLevel >3:

          self.cookedString ="半生不熟"

    else:

          self.cookedString ="生的"

#添加配料

def      addCondiments(self, condiments):

       self.condiments.append(condiments)

#用来进行测试

mySweetPotato = SweetPotato()

print("------有了一个地瓜,还没有烤-----")

print(mySweetPotato.cookedLevel)

print(mySweetPotato.cookedString)

print(mySweetPotato.condiments)

print("------接下来要进行烤地瓜了-----")

print("------地瓜经烤了4分钟-----")

mySweetPotato.cook(4)#烤4分钟

print(mySweetPotato)

print("------地瓜又经烤了3分钟-----")

mySweetPotato.cook(3)#又烤了3分钟

print(mySweetPotato)

print("------接下来要添加配料-番茄酱------")

mySweetPotato.addCondiments("番茄酱")

print(mySweetPotato)

print("------地瓜又经烤了5分钟-----")

mySweetPotato.cook(5)#又烤了5分钟

print(mySweetPotato)

print("------接下来要添加配料-芥末酱------")

mySweetPotato.addCondiments("芥末酱")

print(mySweetPotato)

                                      保护对象的属性(私有属性)

如果有一个对象,当需要对其进行修改属性时,有2种方法

         对象名.属性名=数据---->直接修改

         对象名.方法名() ---->间接修改

为了更好的保存属性安全,即不能随意修改,一般的处理方式为

          将属性定义为私有属性

          添加一个可以调用的方法,供调用



相关文章

网友评论

      本文标题:day16

      本文链接:https://www.haomeiwen.com/subject/vyfqvxtx.html