第一节
1 介绍了Python中类的结构
class NewClass(object):
stratement
第二节
1 介绍了类的初始化函数__init__(self)
2 Python中所有的类的初始化函数都是__init__(self),第一个参数表示的是本身的对象,就像C++的this指针
3 练习:写一个类名为Animal,初始化函数的函数体内容为pass
class Animal(object):
def __init__(self):
pass
第三节
1 介绍了怎么样在Python中的初始化函数来初始化一个值
2 比如__int__(self , nam): self.name = name,我们可以这样来初始化类的变量name的值
3 介绍了怎么样实例化一个类的对象
4 练习:实例化类Animal的对象,并输出它的name
class Animal(object):
def __init__(self , name):
self.name = name
zebra = Animal("Jeffrey")
print zebra.name
第四节
1 介绍了类的变量
2 对于类的变量,我们可以指定它的值,不用在初始化函数中赋值
3 比如以下类的变量is_alive
class Animal(object):
is_alive = True
statement
第五节
1 介绍了Python的类的继承
2 类的继承如下class DerivedClass(BaseClass): ,表示DerivedClass继承父类BaseClass
3 练习:在已有的类的基础上,写一个子类Triangle继承自父类Shape,并在子类中的初始化函数中有四个参数self,side1,side2,side3
class Shape(object):
"""Makes shapes!"""
def __init__(self, number_of_sides):
self.number_of_sides = number_of_sides
# Add your Triangle class below!
class Triangle(Shape):
def __init__(self,side1,side2,side3):
self.side1 = side1
self.side2 = side2
self.side3 = side3
第六节
1 介绍了类的继承之后我们可以对父类的方法进行重写(覆盖)
2 练习:写一个类名为PartTimeEmployee,继承Employee,重写函数calculate_wage,返回值乘上12
class Employee(object):
"""Models real-life employees!"""
def __init__(self, employee_name):
self.employee_name = employee_name
def calculate_wage(self, hours):
self.hours = hours
return hours * 20.00
# Add your code below!
class PartTimeEmployee(Employee):
def calculate_wage(self, hours):
self.hours = hours
return hours*12.00
第七节
1 类名应该严格是以大写字母开头
2 对于类的所有成员函数来说,至少都要有一个参数是self
3 对于类的变量来说值应该是事先就确定的,就像C++的静态成员变量,我们可以通过类名直接访问到,不用实例化
4 对于类的实例化变量来说我们是需要实例化出对象后才能访问到
5 在类的所有成员函数中,无论是访问类的变量还是类的实例化变量我们都是通过self.variable
分享到:
相关推荐
3-python快速入门3-python快速入门3-python快速入门3-python快速入门3-python快速入门3-python快速入门3-python快速入门3-python快速入门3-python快速入门3-python快速入门3-python快速入门3-python快速入门3-python...
Python入门-基础知识-背记手册-使用说明Python入门-基础知识-背记手册-使用说明Python入门-基础知识-背记手册-使用说明Python入门-基础知识-背记手册-使用说明Python入门-基础知识-背记手册-使用说明Python入门-基础...
Python入门教程-超详细1小时学会Python.pdfPython入门教程-超详细1小时学会Python.pdfPython入门教程-超详细1小时学会Python.pdfPython入门教程-超详细1小时学会Python.pdfPython入门教程-超详细1小时学会Python.pdf...
安装 sudo aptitude install python-gtk2 文档 http://www.pygtk.org/pygtk2tutorial/ch-Introduction.html#sec-ExploringPygtk python入门基础教程:python-gtk全文共15页,当前为第2页。 第一个例子 python入门...
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
推荐书名 No Starch--Python for Kids - A Playful Introduction to Programming - J. Briggs-2013.pdf 2.下载地址 欢迎下载,请访问我的csdn资源本书分享下载页⾯: (友情提⽰:请点击右下的 "联通下载" 或者 ...
清华大学出品的Python课件,非常适合Python新手,也适合老鸟复习回顾,完全可用于自学入门 清华大学精品Python学习PPT课件-第1章 Python3概述.pptx 清华大学精品Python学习PPT课件-第2章 Python基本语法.pptx 清华...
python入门与实践--作业11
Programming-for-Computations-Python-A-Gentle-Introduction-to-Numerical-Simulations-with-Python (1).pdf
Python----入门手册及教程 Python----入门手册及教程 Python----入门手册及教程 Python----入门手册及教程 Python----入门手册及教程 Python----入门手册及教程
Python编程入门-2019-11-06 Python编程入门-2019-11-06
Deep-Time-Series-Forecasting-with-Python-An-Intuitive-Introduction-to-Deep-Learning-for-Applied-Time-Series-Modeling.pdf
python入门游戏源码-中国象棋
python入门教程-09-状态码介绍.ev4.rar
An-Introduction-to-Statistics-with-Python-With-Applications-in-the-Life-Sciences
python入门教程-29-添加学员功能.ev4.rar