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

Python Base Five的更多相关文章

  1. Python Base of Scientific Stack(Python基础之科学栈)

    Python Base of Scientific Stack(Python基础之科学栈) 1. Python的科学栈(Scientific Stack) NumPy NumPy提供度多维数组对象,以 ...

  2. Python Base Four

    35. In python, file operation syntax is similar to c. open(file,'r',……) //the first parameters is ne ...

  3. Python Base One

    //this is my first day to study python, in order to review, every day i will make notes (2016/7/31) ...

  4. Python Base Three

    //sixth day to study python(2016/8/7) 32. In python , there are have an special type dictionary , it ...

  5. Python Base Two

    //fourth day to study python 24. In python , how to create funcation. we can use def to define funca ...

  6. 2019-04-18 Python Base 1

    C:\Users\Jeffery1u>python Python 3.7.3 (default, Mar 27 2019, 17:13:21) [MSC v.1915 64 bit (AMD64 ...

  7. python base 64

    python中base64编码与解码   引言: 在一些项目中,接口的报文是通过base64加密传输的,所以在进行接口自动化时,需要对所传的参数进行base64编码,对拿到的响应报文进行解码: Bas ...

  8. Python Base HTTP Server

    import BaseHTTPServer import cgi, random, sys MESSAGES = [ "That's as maybe, it's still a frog. ...

  9. 基于Python+协程+多进程的通用弱密码扫描器

    听说不想扯淡的程序猿,不是一只好猿.所以今天来扯扯淡,不贴代码,只讲设计思想. 0x00 起 - 初始设计 我们的目标是设计一枚通用的弱密码扫描器,基本功能是针对不同类型的弱密码,可方便的扩展,比如添 ...

随机推荐

  1. Vue-Quill-Editor 修改配置,和图片上传

    1.富文本编辑器中的图片上传是将图片转为base64格式的,如果需要上传图片到自己的服务器,需要修改配置. 创建一个quill-config文件 /*富文本编辑图片上传配置*/ /*富文本编辑图片上传 ...

  2. NSStream实现发送和接受数据

    一.基本概念在iOS中以NSStream(流)来发送和接收数据,可以设置流的代理,对流状态的变化做出相应.1连接建立2接收到数据3连接关闭NSStream:数据流的父类,用于定义抽象特性,例如:打开. ...

  3. xmpp 协议详解

    XMPP(可扩展消息处理现场协议)是基于可扩展标记语言(XML)的协议,它用于即时消息(IM)以及在线现场探测.它在促进服务器之间的准即时操作.这个协议可能最终允许因特网用户向因特网上的其他任何人发送 ...

  4. comboBox 下拉宽度自适应

    ///适用combobox绑定datatable private void comboBox_DataSourceChanged(object sender, EventArgs e) { Combo ...

  5. React组件自适应窗口宽高

    很多时候我们需要组件能够根据窗口变化改变宽高,有时候可以使用css,有时候需要随数据调整则使用js计算. 比如说,当我们在页面中放置一个iframe时,我们希望它的宽高随着其父元素or窗口的变化而变化 ...

  6. 27. Remove Element@python

    Given an array nums and a value val, remove all instances of that value in-place and return the new ...

  7. luogu4608 [FJOI2016]所有公共子序列问题

    题目描述: luogu loj 题解: 序列自动机(?)+高精+普及dp. 这个是猫老师的序列自动机(字符串从1开始): ]) { memset(t[n],-,sizeof(t[n])); ;i> ...

  8. Spring Security 与 OAuth2(介绍)

    https://www.jianshu.com/p/68f22f9a00ee Spring Security 与 OAuth2(介绍) 林塬 2018.01.23 11:14* 字数 3097 阅读 ...

  9. XML 转 fastJSON

      import java.util.List; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.Doc ...

  10. C++:100阶乘数组输出

    #include <iostream> using namespace std; int main(){ int i =1; int a[2048]={0}; while(i !=101) ...