Python 入门教程 17 ---- Introduction to Classes
来源:互联网 发布:影视app源码 编辑:程序博客网 时间:2024/06/04 01:39
第一节
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 = namezebra = 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
6 如果子类要用到父类的实例化变量,那么在子类的初始化函数了里面我们比去调用父类的初始化函数
- Python 入门教程 17 ---- Introduction to Classes
- Python 入门教程 16 ---- Introduction to Bitwise Operators
- An Introduction to Interactive Programming in Python (Part 2) week 6 (Classes and Tied images)
- An Introduction to Interactive Programming in Python (Part 2) -- Week 7 More Classes & Sprites
- Introduction to unittest using python
- Introduction to unittest using python
- An Introduction to Python Lists
- Introduction to SQLite in Python
- An Introduction to Python Lists
- Introduction to Concurrent Programming with Stackless Python
- Decorators I: Introduction to Python Decorators
- introduction to computation and programming using python
- Topic 3:An Informal Introduction to Python
- An Introduction to Interactive Programming in Python
- Chapter 1: Introduction to MySQL Connector/Python
- An Introduction to Machine Learning with Python
- Introduction.to.Machine.Learning.with.Python 笔记
- class Introduction to Databases in Python
- Android JNI入门搭建环境(一)
- Oracle inline view 简介
- sigprocmask
- 冒泡法排序
- JBoss 系列五:JBoss 7/WildFly中配置使用Oracle数据库
- Python 入门教程 17 ---- Introduction to Classes
- [leetcode]Word Ladder
- 图像处理之一阶微分应用
- 沈阳研发一二三面
- java中的io系统详解
- Charm Bracelet 背包练习
- Android JNI中的常用方法
- 全排列问题&&组合问题
- [leetcode]Valid Palindrome