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 ...
随机推荐
- Intersection Observer API 可以让你知道被观察元素何时进入或退出浏览器的视口
google 文档 https://developers.google.cn/web/updates/2016/04/intersectionobserver MDN 文档 https://devel ...
- vue 实现聊天框滚动到底
在需要出现滚动条的 DOM上添加 v-scroll 属性: <div class="chat-box" v-scroll="{auto: true}"&g ...
- 依赖注入容器之Castle Windsor
一.Windsor的使用 Windsor的作为依赖注入的容器的一种,使用起来比较方便,我们直接在Nuget中添加Castle Windsor,将会自动引入Castle.Core 和 Castle.Wi ...
- LeetCode 112 Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- ubuntu16.04编译安装imu_tk
imu_tk代码地址 https://bitbucket.org/alberto_pretto/imu_tk 安装依赖项 sudo apt-get install build-essential cm ...
- 信1705-2 软工作业最大重复词查询思路: (1)将文章(一个字符串存储)按空格进行拆分(split)后,存储到一个字符串(单词)数组中。 (2)定义一个Map,key是字符串类型,保存单词;value是数字类型,保存该单词出现的次数。 (3)遍历(1)中得到的字符串数组,对于每一个单词,考察Map的key中是否出现过该单词,如果没出现过,map中增加一个元素,key为该单词,value为1(
通过学习学会了文本的访问,了解一点哈希表用途.经过网上查找做成了下面查询文章重复词的JAVA程序. 1 思 思路: (1)将文章(一个字符串存储)按空格进行拆分(split)后,存储到一个字符串(单词 ...
- Spark安装部署(local和standalone模式)
Spark运行的4中模式: Local Standalone Yarn Mesos 一.安装spark前期准备 1.安装java $ sudo tar -zxvf jdk-7u67-linux-x64 ...
- HTML5:一些部件
在表单里经常会用到很多部件 <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...
- samba速度调优
本来windows上传到板子上的速度很慢 增加 socket options = TCP_NODELAY 明显上传下载速度都快了 参考: https://superuser.com/questions ...
- JavaScript面向对象之闭包的理解
首先了解一下什么是闭包,闭包是一个函数,通常被称为闭包函数或者绑定函数,该函数运行在一个特殊的环境里,该环境定义了一些本地变量,当该函数被调用时,仍可以使用这些本地变量. 当一个函数在不位于它所处的环 ...