26-Python3 面向对象
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 面向对象的更多相关文章
- python022 Python3 面向对象
Python3 面向对象 Python从设计之初就已经是一门面向对象的语言,正因为如此,在Python中创建一个类和对象是很容易的.本章节我们将详细介绍Python的面向对象编程. 如果你以前没有接触 ...
- python3面向对象注意事项
一.面向对象super的作用: class parent(object): def __init__(self): self.test() def test(self): print('parent- ...
- Python3 面向对象编程
小案例: #!/usr/bin/env python # _*_ coding:utf-8 _*_ # Author:Bert import sys class Role(object): n=&qu ...
- Python3 面向对象(1)
面向.概述 面向过程: 根据业务逻辑从上到下写垒代码面向过程的设计的核心是过程,过程即解决问题的步骤, 面向过程的设计就好比精心设计好一条流水线,考虑周全什么时候处理什么东西 优点: 极大降低了程序的 ...
- Python3 面向对象之:单继承
一:什么面向对象的继承? 比较官方的说法就是: 继承(英语:inheritance)是面向对象软件技术当中的一个概念.如果一个类别A“继承自”另一个类别B,就把这个A称为“B的子类别”,而把B称为“A ...
- Python3 面向对象(基础篇)
面向对象 关于面向对象的标准定义网上有很多,不再讲述,现在我们来通俗点理解: 面向对象编程相对于面向过程编程和函数式编程来说,看的更长远,实现功能相对更简单. 面向对象:对象就是物体,这种编程思想就是 ...
- Python3 面向对象 高级编程
正常情况下,当我们定义了一个class,创建了一个class的实例后,我们可以给该实例绑定任何属性和方法,这就是动态语言的灵活性. class Student(object): pass 然后,尝试 ...
- Python3 面向对象
Class 在Python中,定义类是通过class关键字: class Student(object): pass class后面紧接着是类名,即Student,类名通常是大写开头的单词,紧接着是( ...
- Python3面向对象基础
面向对象概述 面向对象 面向对象的世界,引入了对象的概念,对象具有属性:数据,对象具有过程或者方法:成员函数.成员函数的作用就是处理属性. 例子 对象:Car 属性:fuel_level, isSed ...
- python3 面向对象编程--类的封装和继承
#python3import refrom urllib import requestimport os class PosterSpider(object): def __init__(se ...
随机推荐
- nohup 同时实现记录日志和屏幕输出
nohup nohup命令:如果你正在运行一个进程,而且你觉得在退出帐户时该进程还不会结束,那么可以使用nohup命令.该命令可以在你退出帐户/关闭终端之后继续运行相应的进程.nohup就是不挂断 ...
- 移动端H5混合开发设置复盘与总结
此篇接上一篇: 移动端H5混合开发,Touch触控,拖拽,长按, 滑屏 实现方案 https://www.cnblogs.com/buoge/p/9346699.html app 场布设置已经上线了, ...
- [No0000125]WCF安全体系
WCF的安全体系主要包括三个方面:传输安全(Transfer Security).授权或者访问控制(Authorization OR Access Control)以及审核(Auditing).而传输 ...
- 创建ReactNative的iOS项目
http://reactnative.cn/docs/integration-with-existing-apps/ 1.安装好ReactNative开发环境 2.安装好CocoaPods 3.创建项 ...
- Exception 03 : org.hibernate.MappingException: Unknown entity: org.hibernate.cfg.Configuration
异常名称 org.hibernate.MappingException: Unknown entity: org.hibernate.cfg.Configuration 异常详细信息 org.hibe ...
- arcengine新建要素类
ArcGIS里面新建数据集,看起来简单,平时都是默认创建,实际上好多细节问题我们都没注意到 一.在数据集上新建要素类: How to create a feature class within a f ...
- VS在解决方案中添加一个别人给的项目,我自己的项目主窗体中不能调用
提示缺少Using引用,我在主窗体中已经写了Using XX,还是提示“未能找到类型或命名空间名“ XX”(是否缺少Using指令或程序集引用?)”,以前只要Using 一下就好了,后来想了一下,要在 ...
- Scaleform 中的 3D视角相关研究
参考文献: 1.D3D中的第一人称视角 2.透视投影的原理和实现 http://blog.csdn.net/ww51xh/article/details/2910 3.深入探索透视投影变换 http: ...
- iOS将excel转plist
iOS将excel转plist 先把excel用Numbers打开,转换成CSV,然后新建一个工程,写下面的代码: - (void)viewDidLoad { [super viewDidLoad]; ...
- oracle 11g/12c 密码复杂度验证设置
############################################################################### ###### 11g ###### ## ...