// 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. javaweb基础(11)_cookie的会话管理

    一.会话的概念 会话可简单理解为:用户开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭浏览器,整个过程称之为一个会话. 有状态会话:一个同学来过教室,下次再来教室,我们会知道这个同学曾 ...

  2. java基础—方法重载(overload)

    一.方法的重载 方法名一样,但参数不一样,这就是重载(overload). 所谓的参数不一样,主要有两点:第一是参数的个数不一样,第二是参数的类型不一样.只要这两方面有其中的一方面不一样就可以构成方法 ...

  3. springmvc的第一个程序

    文中用的框架版本:spring 3,hibernate 3,没有的,自己上网下. web.xml配置: <?xml version="1.0" encoding=" ...

  4. iOS应用架构谈-part1概述

    当我们讨论客户端应用架构的时候,我们在讨论什么? 其实市面上大部分应用不外乎就是颠过来倒过去地做以下这些事情: --------------- --------------- ------------ ...

  5. C++ 学习笔记(一) cout 与printf 的不同之处

    作为一个嵌入式开发的猿,使用打印调试程序是必不可少的,拿到新的项目第一件事就是捣鼓打印.这次也不例外有打印才有耍下去的底气.在之前零零碎碎的C++学习中,还是一边学一边做项目的状态下能用printf解 ...

  6. Spring框架配置文件中有两个相同名字的bean,最后会覆盖掉一个bean

    问题容易出现在多个人合作的项目中,定义bean的名字的时候发生重复. 可以配置当bean定义重复的时候抛出异常,结束程序,强制提示更改重复的bean.

  7. python入门:in 的用法(它在不在这个字符串里面)

    #!/usr/bin/env python # -*- coding:utf-8 -*- #in 的用法(它在不在这个字符串里面) #ret(返回,译音:ruai特) #给s赋值为字符串“Alex S ...

  8. numpy 三个点的使用[...]

    numpy [...]语法简单使用 Python numpy中切片功能与列表切片类似,但功能更加强大 本文主讲numpy中[...]的简单使用,后续工作继续补充. import numpy >& ...

  9. Linux优化总结

    1)netstat (*****)查看网络状态lntup或an 1.[listening|-l] 2.[--numeric|-n] 3.[--tcp|-t] 4.[--udp|-u] 5[--prog ...

  10. 15年多校第一场七题hdu5294

    要做这题,先要明白图的割,说白了就是 为了让原点无法到汇点要删几条边(之所以叫割,就是在图面上切一刀,减掉最小的边是原点和汇点成为两个集合),想到了割先放着一会用. 题中说只有沿最短路走才有可能追上, ...