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 ...
随机推荐
- day_6.8 py 网络编程
2018-6-8 18:20:30 OSI模型:就是七层物理层 ICMP 我ping你的时候要用,不仅要知道ip地址和网卡号mac地址 ARP 在我和你通讯前不知道的mac地址需要广播一下,当我说的 ...
- Maven、SpringBoot框架结构优化
一.创建maven项目,名为test-parent,pom文件如下: ... <artifactId>test-parent</artifactId> <version& ...
- [No0000134]C#中的委托,匿名方法和Lambda表达式
简介 在.NET中,委托,匿名方法和Lambda表达式很容易发生混淆.我想下面的代码能证实这点.下面哪一个First会被编译?哪一个会返回我们需要的结果?即Customer.ID=5.答案是6个Fir ...
- 利用Python的collections包下Counter的类统计每个数据出现的个数
from collections import Counter a = [1, 2, 3, 1, 1, 2] result = Counter(a) print result 输出: {1: 3, 2 ...
- Ubuntu16.04LTS卸载软件的命令
写在前面:本博客为本人原创,严禁任何形式的转载!本博客只允许放在博客园(.cnblogs.com),如果您在其他网站看到这篇博文,请通过下面这个唯一的合法链接转到原文! 本博客全网唯一合法URL:ht ...
- 一种历史详细记录表,完整实现:CommonOperateLog 详细记录某用户、某时间、对某表、某主键、某字段的修改(新旧值
一种历史详细记录表,完整实现:CommonOperateLog 详细记录某用户.某时间.对某表.某主键.某字段的修改(新旧值). 特别适用于订单历史记录.重要财务记录.审批流记录 表设计: names ...
- python3实现字符串的全排列的方法(无重复字符)
https://www.jb51.net/article/143357.htm 抛出问题 求任意一个字符串的全排列组合,例如a='123',输出 123,132,213,231,312,321.(暂时 ...
- 图->连通性->最小生成树(普里姆算法)
文字描述 用连通网来表示n个城市及n个城市间可能设置的通信线路,其中网的顶点表示城市,边表示两城市之间的线路,赋于边的权值表示相应的代价.对于n个定点的连通网可以建立许多不同的生成树,每一棵生成树都可 ...
- LeetCode 500 Keyboard Row 解题报告
题目要求 Given a List of words, return the words that can be typed using letters of alphabet on only one ...
- scala-高阶函数
//1类似于lambda表达式的函数直接量====================== var get = (name: String) => { println(123 + name) } g ...