26-Python3 面向对象

'''
面向对象技术简介
''' '''
类定义
''' '''
类对象
'''
class MyClass:
i = 12345
def f(self):
return 'hello,runoob' x = MyClass() #实例化类
print('类的属性为:',x.i) #访问类的属性
print('类的方法为:',x.f()) #访问类的方法 class Complex:
def __init__(self,realpart,imagpart):
self.r = realpart
self.i = imagpart
x = Complex(3.0,-4.5)
print(x.r,x.i) '''
self代表类的实例,而非类
'''
class Test:
def prt(self):
print(self)
print(self.__class__)
t = Test()
t.prt() '''
类的方法
'''
class people:
name = ''
age = 0
__weight =0
def __init__(self,n,a,w):
self.name = n
self.age = a
self.__weight = w
def speak(self):
print('{}说:我{}岁了!'.format(self.name,self.age)) p = people('Runoob',10,30)
p.speak() '''
继承
'''
class People:
name = ''
age = 0
__weight = 0
def __init__(self,n,a,w):
self.name = n
self.age = a
self.__weight = w
def speak(self):
print('{}说:我今年{}岁了'.format(self.name,self.age)) class Student(People):
grade = ''
def __init__(self,n,a,w,g):
People.__init__(self,n,a,w)
self.grade = g
def speak(self):
print('{}说:我今年{}岁了,在读{}年级'.format(self.name,self.age,self.grade)) s = Student('Joo',23,90,'')
s.speak() '''
多继承,困了,累累,写遍下面读代码
'''
class people3:
name = ''
age = 0
__weight = 0
def __init__(self,n,a,w):
self.name = n
self.age = a
self.__weight = w
def speak(self):
print('{}说:我{}了'.format(self.name,self.age)) #单继承
class student3(people3):
grade = ''
def __init__(self,n,a,w,g):
people3.__init__(self,n,a,w)
self.grade = g
def speak(self):
print('{}说:我{}岁了,在读{}年级'.format(self.name,self.age,self.grade)) #另外一个类
class speaker:
name = ''
toptic = ''
def __init__(self,n,t):
self.name = n
self.toptic = t
def speak(self):
print('我叫{},我是一个演说家,我今天演讲的主题为{}'.format(self.name,self.toptic)) #多重继承
class sample(speaker,student3):
a = ''
def __init__(self,n,a,w,g,t):
student3.__init__(self,n,a,w,g)
speaker.__init__(self,n,t) # class sample(student3,speaker):
# a = ''
# def __init__(self,n,a,w,g,t):
# student3.__init__(self,n,a,w,g)
# speaker.__init__(self,n,t) test = sample('Tim',25,80,4,'Python')
test.speak()#方法名相同,默认调用的是在括号中排前的父类的方法 ##再重新敲一遍下面的代码
class people:
name = ''
age = 0
__weight = 0
def __init__(self,n,a,w):
self.name = n
self.age = a
self.__weight = w
def speak(self):
print('{}说:我今年{}'.format(self.name,self.age)) class student(people):
grade = ''
def __init__(self,n,a,w,g):
people.__init__(self,n,a,w)
self.grade = g
def speak(self):
print('{}说,我{}岁了,在读{}年级'.format(self.name,self.age,self.grade))
class speaker:
name = ''
toptic = ''
def __init__(self,n,t):
self.name = n
self.toptic = t
def speak(self):
print('{}说,我是一个演说家,我演讲读主题为{}'.format(self.name,self.toptic)) class sample(speaker,student):
a = ''
def __init__(self,n,a,w,g,t):
student.__init__(self,n,a,w,g)
speaker.__init__(self,n,t) test = sample('Tim',25,80,4,'Python')
test.speak() '''
方法重写
'''
class Parent:
def myMethod(self):
print('调用父类方法') class Child(Parent):
def myMethod(self):
print('调用子类方法') c = Child()
c.myMethod()
super(Child,c).myMethod()
'''
类属性与方法
'''
##类的私有属性
class JustCounter:
__secretCount = 0
publicCount = 0
def count(self):
self.__secretCount +=1
self.publicCount +=1
print(self.__secretCount)
counter = JustCounter()
counter.count()
counter.count()
print(counter.publicCount)
# print(counter.__secretCount) #报错,实例不能访问私有变量 ##类的私有方法 class Site:
def __init__(self,name,url):
self.name = name
self.url = url
def who(self):
print('name:',self.name)
print('url:',self.url)
def __foo(self):
print('这是私有方法')
def foo(self):
print('这是公有方法')
self.__foo()
x = Site('菜鸟教程','www.runoob.com')
x.who()
x.foo()
# x.__foo() #会报错的 ##类的专有方法
pass ##运算符重载 class Vector:
def __init__(self,a,b):
self.a = a
self.b = b
def __str__(self):
return 'Vector({},{})'.format(self.a,self.b)
def __add__(self, other):
return Vector(self.a+other.a,self.b+other.b) v1 = Vector(2,10)
v2 = Vector(5,-2)
print(v1 + v2)

26-Python3 面向对象的更多相关文章

  1. python022 Python3 面向对象

    Python3 面向对象 Python从设计之初就已经是一门面向对象的语言,正因为如此,在Python中创建一个类和对象是很容易的.本章节我们将详细介绍Python的面向对象编程. 如果你以前没有接触 ...

  2. python3面向对象注意事项

    一.面向对象super的作用: class parent(object): def __init__(self): self.test() def test(self): print('parent- ...

  3. Python3 面向对象编程

    小案例: #!/usr/bin/env python # _*_ coding:utf-8 _*_ # Author:Bert import sys class Role(object): n=&qu ...

  4. Python3 面向对象(1)

    面向.概述 面向过程: 根据业务逻辑从上到下写垒代码面向过程的设计的核心是过程,过程即解决问题的步骤, 面向过程的设计就好比精心设计好一条流水线,考虑周全什么时候处理什么东西 优点: 极大降低了程序的 ...

  5. Python3 面向对象之:单继承

    一:什么面向对象的继承? 比较官方的说法就是: 继承(英语:inheritance)是面向对象软件技术当中的一个概念.如果一个类别A“继承自”另一个类别B,就把这个A称为“B的子类别”,而把B称为“A ...

  6. Python3 面向对象(基础篇)

    面向对象 关于面向对象的标准定义网上有很多,不再讲述,现在我们来通俗点理解: 面向对象编程相对于面向过程编程和函数式编程来说,看的更长远,实现功能相对更简单. 面向对象:对象就是物体,这种编程思想就是 ...

  7. Python3 面向对象 高级编程

    正常情况下,当我们定义了一个class,创建了一个class的实例后,我们可以给该实例绑定任何属性和方法,这就是动态语言的灵活性.  class Student(object): pass 然后,尝试 ...

  8. Python3 面向对象

    Class 在Python中,定义类是通过class关键字: class Student(object): pass class后面紧接着是类名,即Student,类名通常是大写开头的单词,紧接着是( ...

  9. Python3面向对象基础

    面向对象概述 面向对象 面向对象的世界,引入了对象的概念,对象具有属性:数据,对象具有过程或者方法:成员函数.成员函数的作用就是处理属性. 例子 对象:Car 属性:fuel_level, isSed ...

  10. python3 面向对象编程--类的封装和继承

    #python3import refrom urllib import requestimport os class PosterSpider(object):     def __init__(se ...

随机推荐

  1. Android最全开发资源(申明:来源于网络)

    Android最全开发资源(申明:来源于网络) 地址:http://www.jianshu.com/p/0c36302e0ed0?ref=myread

  2. eclipse启动报错:Could not create the java virtual machine

    用maven.springboot开发时,安装了当时最新版的eclipse(3.5.5).eclipse解压版的非常方便,想先安装了看看.暂时没有升级其他软件. 打开的时候报错: 原因其实就是还木有升 ...

  3. [转载]Invalid bound statement (not found): com.taotao.mapper.TbItemMapper.selectByExample: 错误

    因碰到同样的问题,使用该方法对我有效,为方便以后查找,所以做了转载,原文请查看:https://www.cnblogs.com/fifiyong/p/5795365.html 在Maven工程下,想通 ...

  4. Django:视图views(一)

    1.环境搭建 在django中,视图负责与web请求进行交互 视图本质上是一个Python函数,定义在booktest/views.py.通过django1/urls.py路由到该视图中. 首先经过创 ...

  5. Web开发——JavaScript基础

    参考学习: MDN JavaScript:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript ECMAScript 6入门(阮一峰):htt ...

  6. Jenkins 忘记admin用户名以及密码

    1.进入 如果安装的war包,路劲如下: C:\Users\LENOVO\.jenkins\ 2. 1)方式一: 打开config.xml  ->将useSecurity设置为false 2)方 ...

  7. CF1103D Professional layer dp

    正解:dp 解题报告: 传送门! 首先不难想到求个gcd,然后把gcd质因数分解成p1w1*p2w2*p3w3*...*pmwm 显然只要满足对每个p有一个ai%pj!=0就好,也就是说对每个pj找出 ...

  8. ansible源码安装

    一.升级python 笔者系统为centos6.5,系统默认安装python2.6,虽然ansible官方文档要求python版本为2.6或2.7,然而许多人都说使用2.6可能出现一系列问题,所以作者 ...

  9. InnoDB Next-Key Lock

    InnoDB有三种行锁的算法: 1,Record Lock:单个行记录上的锁 2,Gap Lock:间隙锁,锁定一个范围,但不包括记录本身 3,Next-Key Lock:Record Lock + ...

  10. TCP/IP具体解释--TCP首部的TimeStamp时间戳选项

    TCP应该是以太网协议族中被应用最为广泛的协议之中的一个,这里就聊一聊TCP协议中的TimeStamp选项.这个选项是由RFC 1323引入的,该C建议提交于1992年.到今天已经足足有20个年头.只 ...