• Python Base Five


    // 8 day(2016/8/11)

    38. In python , it is oop.

         class Baskball:
             def setName(self, name):
                    self.name = name
             def kick(self):
                    print('my name is %s' % self.name)
          baskball = Baskball()
          baskball.setName('baskball')
          baskball.kick()

         -> my name is baskball

         class Ball:
             def __init__(self, name):
                  self.name = name
             def kick(self):
                  print('my name is %s' % self.name)
           b = Ball('tom')
           b.kick()

           -> my name is tom

    39. In python ,how to define private variable,

         such as:

         class Person:
              name = 'roy'
         p = Person()
         print(p.name)

         -> roy

         if you use:

        class Person:
              __name = 'roy'
         p = Person()
         print(p.__name) || print(p.name)

         -> error

         if you use __ before variable ,you can access it direct.

         class Person:
               __name = 'roy'
               def getName(self):
                    return self.__name
         p = Person()
         print(p.getName())

         -> roy   

        class Person:
           __name = 'roy'
        p  = Person()
        print(p._Person__name)

        -> roy

    40. inheritance mechanism

          class SubClassName:(ParentClassName):

                    ……

         class Parent:
              def hello(self):
                    print('write code change world')

         class Child(Parent):
              pass

         p = Parent()
         p.hello()

         c = Child()
         c.hello()

         ->

         write code change world

         write code change world

         if subclass methon is same with parent , it will cover parent method, such as:

         class Child(Parent):

              def hello(self):

                  print('believe youself')

         c = Child()

         c.hello()

         -> believe youself

         now we will study a simple example:

         import random as r
         class Fish:
              def __init__(self):
                  self.x = r.randint(0,10)
                  self.y = r.randint(0,10)
              def move(self):
                 self.x -= 1
                 print('my position is:',self.x, self.y)

         class Shark(Fish):
             def __init__(self):
                #Fish.__init__(self)
                super().__init__()
                self.hungry = True

            def eat(self):
                 if self.hungry:
                    print('eat eat eat')
                    self.hungry = False
                else:
                    print('not hungry')

           1,Fish.__init__(self)
           2,super().__init__() 

           1 and 2 is same ,if you not add this ,you invoke move in Shark ,it will error, because ,__init__ will cover parent method, you call move() ,it will not found x and y. if you use  1 and 2, it will solve this question

          multiply parent class:

          class subClassName:(parent1ClassName, parent2ClassName):

               ……

        
         class Base1:
             def fool1(self):
                   print('it is fool1')

         class Base2:
            def fool2(self):
                  print('it is fool2')

         class c(Base1, Base2):
               pass

         c = c()
         c.fool1()
         c.fool2()

         -> it is fool1

         -> it is fool2

  • 相关阅读:
    201771030125-王英雪 实验一 软件工程准备一<构建之法与博客首秀>
    201771010102-常惠琢 实验三 结对项目—《西北师范大学疫情防控信息系统》项目报告
    201771010102-常惠琢 实验二 个人项目—《西北师范大学学生疫情上报系统》项目报告
    软件工程第四次作业
    软件工程第三次作业
    软件工程第二次作业
    软件工程第一次作业
    201771030104-道彤阳 实验四 软件项目案例分析
    201771030104-道彤阳 实验三 结对项目—《西北师范大学疫情防控信息系统》项目报告
    201771030104-道彤阳 实验二 个人项目—《西北师范大学学生疫情上报系统》项目报告
  • 原文地址:https://www.cnblogs.com/wangyaoguo/p/5762904.html
Copyright © 2020-2023  润新知