前言
hello,各位小伙伴,好久不见,前段时间由于事情繁多,这个面向对象的语法比较的重要,而且有一些小难度,所以小王就没有定期分享文章了,正所谓“一心一意才可以做好每一件事情”,希望大家在学习的时候也要这样哟。
Python本来就是一门简单的编程语言,但是大家也要有好的学习方法和态度哟,这样才可以真正的学懂这一门语言!希望大家在自己空闲的时间可以多去涉猎一些自己专业方面的知识,所谓天道酬勤。唯有不断的修炼自己,才能提高自己幸运值。
话不多说!我们开始学习吧!!!
面向对象的知识概念
类对象
class Myclass :
'''这是一个类'''
i= 123456
def f ( self) :
return "hello python"
x= Myclass( )
print ( x. i)
print ( x. f( ) )
class Smplclass :
def info ( self) :
print ( "这是我的类!" )
def sum ( self, x, y) :
return x* y
se= Smplclass( )
print ( se. sum ( 10 , 12 ) )
se. info( )
class Complex :
def __init__ ( self, name, age) :
self. name= name
self. age= age
x= Complex( "bob" , 18 )
print ( x)
print ( x. name, x. age)
'''调用类自身的方法和全局函数'''
def diao ( x, y) :
return ( abs ( x) , abs ( y) )
class Ant :
def __init__ ( self, x= 0 , y= 0 ) :
self. x= x
self. y= y
self. d_point( )
def yi ( self, x, y) :
x, y= diao( x, y)
self. e_point( x, y)
self.
d_point( )
def e_point ( self, x, y) :
self. x+= x
self. y+= y
def d_point ( self) :
print ( "你的当前位置是:(%d,%d)" % ( self. x, self. y) )
ant_a= Ant( )
ant_a. yi( 2 , 7 )
ant_a. yi( - 5 , 6 )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
'''模拟狗狗的日常'''
class Dog ( ) :
'''小狗狗'''
def __init__ ( self, name, age) :
'''初始化属性name和age'''
self. name= name
self. age= age
def wang ( self) :
'''模拟狗狗的汪汪叫'''
print ( self. name. title( ) + "汪汪!" )
def shen ( self) :
print ( self. name. title( ) + "伸舌头" )
my_dog= Dog( "junne" , 15 )
you_dog= Dog( "linux" , 16 )
print ( "我的爱犬的名字是:" + my_dog. name. title( ) + "." )
print ( "我的爱犬已经是:" + str ( my_dog. age) + "岁了!" )
my_dog. wang( )
my_dog. shen( )
print ( "\n你的爱犬的名字是:" + you_dog. name. title( ) + "." )
print ( "你的爱犬已经是:" + str ( you_dog. age) + "岁了!" )
you_dog. wang( )
you_dog. shen( )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
'''析构方法'''
class Newclass (
object ) :
num_count= 0
def __init__ ( self, name) :
self. name= name
Newclass. num_count += 1
print ( name, Newclass. num_count)
def __del__ ( self) :
Newclass. num_count-= 1
print ( "析构" , self. name, Newclass. num_count)
def test ( self) :
print ( "aa" )
aa= Newclass( "hello" )
bb= Newclass( "world" )
cc= Newclass( "aaaa" )
del aa
del bb
del cc
print ( "over!" )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
'''静态方法和类方法'''
'''静态方法没有隐含调用参数,应该使用@staticmethod
类方法隐含调用的参数是类,应该使用@classmethod
'''
class jing :
def __init__ ( self, x= 0 ) :
self. x= x
@staticmethod
def static_method ( ) :
print ( "此处调用了静态方法!" )
@classmethod
def class_method ( cls) :
print ( "此处调用了类方法!" )
jing. static_method( )
jing. class_method( )
dm= jing( )
dm. static_method( )
dm. class_method( )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
''' 类属性'''
class Car ( ) :
""""最爱奔驰!"""
def __init__ ( self, manufacturer, model, year) :
self. manufacturer= manufacturer
self. model= model
self. year= year
self. odometer_reading= 0
def get_descriptive_name ( self) :
long_name= str ( self. year) + " " + self. manufacturer+ " " + self.
model
return long_name. title( )
def update_obometer ( self, mileage) :
if mileage>= self. odometer_reading:
self. odometer_reading= mileage
print ( "这是一个合理的数据!" , str ( self. odometer_reading) + "千米!" )
else :
print ( "这是一个不合理的数据!" )
def increament_odometer ( self, miles) :
self. odometer_reading+= miles
def read_obometer ( self) :
print ( "这是一辆新车,目前仪表显示行程为" + str ( self. odometer_reading) + "千米!" )
my_new_car= Car( "Benz" , "E300L" , 2016 )
print ( my_new_car. get_descriptive_name( ) )
my_new_car. update_obometer( 15 )
my_new_car. read_obometer( )
my_new_car. increament_odometer( 2000 )
my_new_car. read_obometer( )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
'''继承'''
'''定义并使用子类'''
class Car ( ) :
"""汽车之家"""
def __init__ ( self, manufacturer, model, year) :
self. manufacturer= manufacturer
self. model= model
self. year= year
self. odometer_reading= 0
def get_descriptive_name ( self) :
long_name= str ( self. year) + " " + self. manufacturer+ " " + self. model
return long_name. title( )
def update_obometer ( self, mileage) :
if mileage>= self. odometer_reading:
self. odometer_reading= mileage
else :
print ( "这是一个不合理的数据!" )
def increament_odometer ( self, miles) :
self. odometer_reading+= miles
def read_obometer ( self) :
print ( "这是一辆新车,目前仪表显示行程为" + str ( self
. odometer_reading) + "千米!" )
class Bmw ( Car) :
'''这是一个子类,基类是Car'''
def __init__ ( self, manufacturer, model, year) :
super ( ) . __init__( manufacturer, model, year)
self. Motor= Motor( )
class Motor ( Bmw) :
def __init__ ( self, Motor_size= 60 ) :
self. Motor_size= Motor_size
def describe_motor ( self) :
print ( "这款车的发动机参数是" + str ( self. Motor_size) + "6缸,3.0T涡轮增压,225KW." )
my_tesla= Bmw( "宝马" , "535Li" , "2017款" )
print ( my_tesla. get_descriptive_name( ) )
my_tesla. update_obometer( 0 )
my_tesla. read_obometer( )
print ( "*******************************" )
my_tesla. increament_odometer( 10 )
my_tesla. read_obometer( )
my_tesla. Motor. describe_motor( )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
'''多重继承'''
class Wai :
def __init__ ( self, x= 0 , y= 0 , color= "black" ) :
self. x= x
self. y= y
self. color= color
def haijun ( self, x, y) :
self. x= x
self. y= y
print ( "发射鱼雷......." )
self. info( )
def info ( self) :
print ( "定位目标:(%d,%d)" % ( self. x, self. y) )
def gongji (
self) :
print ( "导弹发射!!!" )
class FlyWai ( Wai) :
def gongji ( self) :
print ( "飞船拦截!!!" )
def fly ( self, x, y) :
print ( "火箭军........" )
self. x= x
self. y= y
self. info( )
flyWai= FlyWai( color= "red" )
flyWai. haijun( 100 , 200 )
flyWai. fly( 12 , 15 )
flyWai. gongji( )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
总结
这些代码是我前段时间自己敲打出来的,知识点也比较全面,可以从一步一步的去按照这个节奏,自己写一写,这样有利于自己的认识和巩固。希望可以对大家有帮助,最后如果你们在实际运用中,有什么问题,欢迎留言私信我哟,尽我所能的帮助你!哈哈哈,我们下期文章再会!
满满的干货,都是小王在网络大数据的平台,细细挑选的,亲测试用,如果你不知道如何选择书籍,就快来领取福利吧!