博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python Base Five
阅读量:5313 次
发布时间:2019-06-14

本文共 2262 字,大约阅读时间需要 7 分钟。

// 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

 

转载于:https://www.cnblogs.com/wangyaoguo/p/5762904.html

你可能感兴趣的文章
bzoj2257
查看>>
http://www.bootcss.com/
查看>>
20145308 《网络对抗》 注入shellcode+Return-to-libc攻击 学习总结
查看>>
将多张图片和文字合成一张图片
查看>>
自己动手写ORM(01):解析表达式树生成Sql碎片
查看>>
如何使用USBWebserver在本机快速建立网站测试环境
查看>>
变量提升
查看>>
[Flex] flex手机项目如何限制横竖屏?只允许横屏?
查看>>
tensorflow的graph和session
查看>>
JavaScript动画打开半透明提示层
查看>>
jquery-jqzoom 插件 用例
查看>>
1007. Maximum Subsequence Sum (25)
查看>>
查看oracle数据库的连接数以及用户
查看>>
【数据结构】栈结构操作示例
查看>>
三.野指针和free
查看>>
activemq5.14+zookeeper3.4.9实现高可用
查看>>
TCP/IP详解学习笔记(3)IP协议ARP协议和RARP协议
查看>>
简单【用户输入验证】
查看>>
python tkinter GUI绘制,以及点击更新显示图片
查看>>
20130330java基础学习笔记-语句_for循环嵌套练习2
查看>>