本文实例讲述了Python的面向对象思想。分享给大家供大家参考。具体分析如下:
面向对象的基本思想是封装,继承,多态。
首先是继承:
定义一个类:
代码如下:
class Bird(object):
have_feather = True
way_of_reproduction = 'egg'
调用这个类:
代码如下:
summer = Bird()
print summer.way_of_reproduction
与Java不同是,Python是不需要new来实例化类的。
同样,Python的类下面是可以定方法的:
代码如下:
class Bird(object):
have_feather = True
way_of_reproduction = 'egg'
def say(self, word='hi hi'):
print 'i say :' + word
注意一点,所有类的函数,必须至少带有一个参数,这个参数必须是self。
类以外的函数没有这一个限制。
代码如下:
chk = Chicken()
print chk.have_feather
print chk.sat('hello')
__init__()方法
__init__()是一个特殊方法(special method)。Python里会有一些特殊方法,Python会以特别的方式处理它们。特殊方法的名字的特点是前后都有两个下划线。
__init__()方法的特殊在于,如果你在类中定义了这个方法,一旦你根据这个类建立对象,Python就会自动调用这个方法(这个过程也叫初始化)。
如:
代码如下:
class happyBird(Bird):
def __init__(self,more_words):
print 'We are happy birds.',more_words
hb = happyBird('Happy,Happy!')
父类方法的重载:
代码如下:
class Hello(object):
name = 'hello'
def __init__(self):
self.name='my name is hello'
#类中的参数必须带有self参数
def sayhi(self):
print 'hi you'
class World(Hello):
def __init__(self):
#这里访问的是父类初始化的变量名
print 'before:',Hello.name
super(World,self).__init__()
#由于调用了父类的初始化构造函数,继承了父类的变量的改变
print 'after:',self.name
#近似于方法重载
def sayhi(self,word='baby'):
#调用父类sayhi方法
super(World,self).sayhi()
print 'hi '+word
def sayWorld(self):
print 'hi,hello world'
if __name__ == '__main__':
c = World()
c.sayhi()
c.sayWorld()
另外,python是允许多继承的,但是这个是个非常危险的操作,建议不要随便使用。
关于Python的多态,就像JavaScript一样,直接访问对象的属性,不需要使用接口,没有类型转换。
对于类型的判断,有抓们的type()函数,和isinstance()函数判断是否某个函数的子类。
代码如下:
isinstance(object, classinfo)
判断实例是否是这个类或者object是变量
classinfo 是类型(tuple,dict,int,float)
判断变量是否是这个类型
代码如下:
class objA:
pass
A = objA()
B = 'a','v'
C = 'a string'
print isinstance(A, objA)
print isinstance(B, tuple)
print isinstance(C, basestring)
输出结果:
True
True
True
希望本文所述对大家的Python程序设计有所帮助。

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.