学习Python类的心得
类的注意事项
1)命名规则
2)继承规则
python继承
1)子类继承父类
2)子类的init方法
class Car():
"""一次模拟汽车的简单尝试"""
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
self.odometer_reading = 0 def get_descriptive_name(self):
long_name = str(self.year) + ' ' + self.make + ' ' + self.model
return long_name.title() def read_odometer(self):
print("This car has " + str(self.odometer_reading) + " miles on it.") def update_odometer(self, mileage):
if mileage >= self.odometer_reading:
self.odometer_reading = mileage
else:
print("You can't roll back an odometer!") def increment_odometer(self, miles):
self.odometer_reading += miles class ElectricCar(Car):
"""电动汽车的独特之处"""
def __init__(self, make, model, year):
"""初始化父类的属性"""
super().__init__(make, model, year) my_tesla = ElectricCar('tesla', 'model s', 2016)
print(my_tesla.get_descriptive_name())

3) python2中的继承

4)给子类定义属性和方法
class Car():
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
self.odometer_reading = 0
-- snip --
class ElectricCar(Car):
"""Represent aspects of a car, specific to electric vehicles."""
def __init__(self, make, model, year):
"""
电动汽车的独特之处
初始化父类的属性,再初始化电动汽车特有的属性
"""
super().__init__(make, model, year)
self.battery_size = 70 # 1处 def describe_battery(self): # 2处
"""打印一条描述电瓶容量的消息"""
print("This car has a " + str(self.battery_size) + "-kWh battery.")

class Thread:
"""
xx1, xx2
"""
def __init__(self, xx1, xx2):
self.xx1 = xx1
self.xx2 = xx2
class MyThread(Thread):
def __init__(self, xx1, xx2):
super().__init__(xx1, xx2) # 等价于super(MyThread, self).__init__()
self.xx3 = "新增的属性3"
self.xx4 = "新增的属性4"
5)重写父类的方法
class ElectricCar(Car):
-- snip --
def fill_gas_tank():
"""电动汽车没有油箱"""
print("This car doesn't need a gas tank!")
class Father:
def __init__(self, name, age, sex):
self.name = name
self.age = age
self.sex = sex
def say(self):
print("父亲会说话,我叫%s, 我的年龄是%d, 我的性别是%s" % (self.name, self.age, self.sex))
class Child(Father):
def __init__(self, name, age, sex, grade, play):
super().__init__(name, age, sex)
self.grade = grade
self.play = play
def say(self):
print("孩子也会说话,我叫%s, 我的年龄是%d, 我的性别是%s, 我读%s, 我会玩%s" % (self.name, self.age, self.sex,
self.grade, self.play))
class Father:
def __init__(self, name='', age=0, sex=''):
self.name = name
self.age = age
self.sex = sex
def say(self):
print("父亲会说话,我叫%s, 我的年龄是%d, 我的性别是%s" % (self.name, self.age, self.sex))
class Child(Father):
def __init__(self, grade, play):
super().__init__()
self.grade = grade
self.play = play
def say(self):
print("孩子也会说话,我叫%s, 我的年龄是%d, 我的性别是%s, 我读%s, 我会玩%s" % (self.name, self.age, self.sex,
self.grade, self.play))
if __name__ == '__main__':
C = Child('二年级', 'play games')
C.say()
"""
孩子也会说话,我叫, 我的年龄是0, 我的性别是, 我读二年级, 我会玩play games
"""
class Father:
def __init__(self, name='', age=0, sex=''):
self.name = name
self.age = age
self.sex = sex
def say(self):
print("父亲会说话,我叫%s, 我的年龄是%d, 我的性别是%s" % (self.name, self.age, self.sex))
class Child(Father):
def __init__(self, grade, play):
super().__init__()
self.grade = grade
self.play = play
def say(self):
print("孩子也会说话,我叫%s, 我的年龄是%d, 我的性别是%s, 我读%s, 我会玩%s" % (self.name, self.age, self.sex,
self.grade, self.play))
if __name__ == '__main__':
C = Child('二年级', 'play games')
C.name = '小明'
C.age = 12
C.sex = 'boy'
C.say()
# 孩子也会说话,我叫小明, 我的年龄是12, 我的性别是boy, 我读二年级, 我会玩play games
学习Python类的心得的更多相关文章
- 学习python类
类:Python 类提供了面向对象编程的所有基本特征: 允许多继承的类继承机制, 派生类可以重写它父类的任何方法, 一个方法可以调用父类中重名的方法. 对象可以包含任意数量和类型的数据成员. 作为模块 ...
- 重新学习python类
之前觉得有java和c++的底子,对python的类没有认真研习,后面到了写项目的时候各种懵逼.终于决定彻底解决python的类问题. 一.python的类定义: class cls_name: de ...
- 关于学习Python的一些心得
1.关注函数参数的类型,如列表,字符串,int,而不是关注函数的功能 2.导入模块numpy,dir(numpy) 查看所有属性 3.''.join(列表) 将列表拆成字符串
- python学习的一点点心得
好久没发博客了,不解释....接下来写一点自己最近学习python的一点心得. 想要学习python的初衷,是看<软件测试技术大全>一书时,了解到像perl.python.ruby等脚本类 ...
- Python学习:类和实例
Python学习:类和实例 本文作者: 玄魂工作室--热热的蚂蚁 类,在学习面向对象我们可以把类当成一种规范,这个思想就我个人的体会,感觉很重要,除了封装的功能外,类作为一种规范,我们自己可以定制的规 ...
- python类学习以及mro--多继承属性查找机制
版权声明:本文为博主原创文章,未经博主允许不得转载. 还记得什么是新式类和旧式类吗? Python中,一个class继承于object,或其bases class里面任意一个继承于object,这个c ...
- 《python基础教程(第二版)》学习笔记 类和对象(第7章)
<python基础教程(第二版)>学习笔记 类和对象(第7章) 定义类class Person: def setName(self,name): self.name=n ...
- 零基础入门学习Python(36)--类和对象:给大家介绍对象
知识点 Python3 面向对象 Python从设计之初就已经是一门面向对象的语言,正因为如此,在Python中创建一个类和对象是很容易的.本章节我们将详细介绍Python的面向对象编程. 如果你以前 ...
- Python 类学习的一些Tips
这里不详细介绍类,只总结一些小萌新在学习python 类时会有的一些疑点. 类的私有性 在python中,属性和方法的访问权限只有两种,公开的,和私有的.在给属性命名时用两个“__”下划线作为开头,就 ...
随机推荐
- Python基础之可接受任意数量参数的函数
1. 可接受任意数量位置参数的函数 为了能让一个函数接受任意数量的位置参数,可以在参数部分使用“*”. def avg(first, *rest): return (first + sum(rest) ...
- 使用python2 对服务器监控(监控内存,CPU,网卡流量等)
#!/usr/bin/env python # -*- coding:utf- -*- #create at -- 'this is a system monitor scripts' __autho ...
- ElasticSearch及其插件安装配置
elasticsearch安装使用 .安装步骤: 1.下载elasticsearch的rpm包: wget https://artifacts.elastic.co/downloads/elastic ...
- java1.8 lambda进行并行运算
parallelStream()支持并行运算: package com.roocon.thread.t2; import java.util.Arrays; import java.util.List ...
- Linux 安装软件报错 Sub-process /usr/bin/dpkg returned an error code (1)
Linux 通过 apt-get 安装软件时报错,换一个软件安装也一样. Errors were encountered while processing: blueman E: Sub-proces ...
- 实现一个微信小程序组件:文字跑马灯效果
marquee.json { "component": true, "usingComponents": {} } marquee.wxml <!--co ...
- 获取用户当前位置信息的两种方法——H5、微信
在之前的 调用百度地图API的总结 中获取当前位置信息我用的是 H5 ,其实微信也提供了获取用户地理位置的方法,现将这两种方法都贴出来,看情况选择使用. 一.H5 获取当前地理位置得到经纬度 // H ...
- OpenJudge计算概论-骑车与走路
/*============================================================ 骑车与走路 总时间限制: 1000ms 内存限制: 65536kB 描述 ...
- 你的Node应用,对接分布式链路跟踪系统了吗?(一) 原创: 金炳 Node全栈进阶 4天前 戳蓝字「Node全栈进阶」关注我们哦
你的Node应用,对接分布式链路跟踪系统了吗?(一) 原创: 金炳 Node全栈进阶 4天前 戳蓝字「Node全栈进阶」关注我们哦
- 技术干货丨如何在VIPKID中构建MQ服务
小结: 1. https://mp.weixin.qq.com/s/FQ-DKvQZSP061kqG_qeRjA 文 |李伟 VIPKID数据中间件架构师 交流微信 | datapipeline201 ...