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 ...
随机推荐
- B - Space Ant
The most exciting space discovery occurred at the end of the 20th century. In 1999, scientists trace ...
- 在AJAX里 使用【 JSON 】 返回数据类型 实现简单的下拉菜单数据
在AJAX里 使用JSON返回数据类型 实现简单的下拉菜单数据 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//E ...
- 理解 CALayer ContentsCenter 属性
http://aaronzjp.cn/2016/12/01/iOS-CALayer/ 这个属性和android 的 .9 文件类似,定义了图片的拉伸范围:例子中明显是四个角不拉伸,对于需要做背景,co ...
- weblogic初学笔记2-在Linux上部署项目
一.这两天在做部署项目到Linux服务器上. 网上有用war包部署的,也有把war包解压之后部署的.比如:http://www.cnblogs.com/xdp-gacl/p/4143413.html ...
- inotifywait实现目录监控--http://man.linuxde.net/inotifywait
sudo apt install inotify-tools while inotifywait -q -r -e create,delete,modify,move,attrib --exclude ...
- DEV获取GridControl当前行
//直接通过gridView获取当前行dr=this.gridView1.GetDataRow(this.gridView1.FocusedRowHandle);//通过DataSet获取数据,需要转 ...
- 如何在windows下安装Python(Python入门教程)
第一步:下载Python安装包 在Python的官网 www.python.org 中找到最新版本的Python安装包,点击进行下载,请注意,当你的电脑是32位的机器,请选择32位的安装包,如果是64 ...
- JQuery is()与hasClass()方法的对比
is()和hasClass()方法都可以用以检查匹配的所有元素里是否含有指定类名,虽说hasClass(className)函数等价于is(“.className”) 但is()方法比hasClass ...
- 用promise和async/await分别实现红绿灯
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 转载:caffe中的Reshape层
http://blog.csdn.net/terrenceyuu/article/details/76228317 #作用:在不改变数据的情况下,改变输入的维度 layer { name: " ...