温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python3.5面向对象与继承的示例分析

发布时间:2021-07-18 12:04:43 来源:亿速云 阅读:141 作者:小新 栏目:开发技术

这篇文章将为大家详细讲解有关Python3.5面向对象与继承的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

具体如下:

1、编程的方式

Python3.5面向对象与继承的示例分析

2、面向对象的基本概念

Python3.5面向对象与继承的示例分析

Python3.5面向对象与继承的示例分析

Python3.5面向对象与继承的示例分析

3、类的基本概念

Python3.5面向对象与继承的示例分析

Python3.5面向对象与继承的示例分析

Python3.5面向对象与继承的示例分析

4、类的定义与调用——简单代码举例

注:建议类名的开头字母用大写,在Python中,类内的函数称作方法,类外的函数还是称作函数。

Python3.5面向对象与继承的示例分析

Python3.5面向对象与继承的示例分析

Python3.5面向对象与继承的示例分析

Python3.5面向对象与继承的示例分析

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:ZhengzhengLiu

#类
class Person:
  i = 10   #属性(变量)
  def eat(self):   #方法(函数)
    print("eating...")
  pass

#类的调用
a = Person()
a.eat()

运行结果:

eating...

class Person():
  #对象属性 构造方法 在实例化对象时会自动调用
  # 实例化的对象就具有name和age两个属性
  #self是指当前的对象 self不是关键字可以被代替,但是习惯使用self指代当前对象
  def __init__(self,name,age):
    # 通过构造方法声明了两个对象属性
    #对象.name属性 = name参数
    self.name = name
    self.age = age

  #声明一个类方法
  def speak(self):
    print("Hello,my name is %s,and I'm %d years old" %(self.name,self.age))

#创建实例对象   会触发构造方法
people01 = Person("Jack",18)    #通过Person类实例化出一个people对象
print(people01)   #打印Person对象在内存中的地址

print(people01.name,people01.age)    #打印对象的属性

#给对象添加属性
people01.sex = "F"
print(people01.sex)

#类方法的调用
people01.speak()

运行结果:

<__main__.Person object at 0x0059C5B0>
Jack 18
F
Hello,my name is Jack,and I'm 18 years old

5、类的方法

Python3.5面向对象与继承的示例分析

Python3.5面向对象与继承的示例分析

示例代码:

#方法——静态方法
class S():
  #实例(对象)属性
  def __init__(self,name,age):     #self一般指实例对象
    self.name = name
    self.age = age

  @staticmethod    #用staticmethod装饰器修饰 表示test2为静态方法
  def test2():    #不能传入self 对象的引用
    print("test2...")

s1 = S("Joe",18)
s1.test2()   #通过实例调用静态方法
S.test2()    #通过类名调用静态方法


#方法——类方法
class C():
  #类属性
  country = "China"

  #实例(对象)属性
  def __init__(self,name,age):
    self.name = name
    self.age = age

  @classmethod    #用classmethod装饰器修饰 表示test3为类方法
  def test3(cls):   #cls指的是类
    print("test3...",cls.country)    #类方法调用类属性


c1 = C("Jack",18)
c1.test3()   #通过实例调用类方法
C.test3()    #通过类名调用类方法

运行结果:

test1...
test2...
test2...
test3... China
test3... China

Python3.5面向对象与继承的示例分析

(1)构造方法:构造方法不能重载(被覆盖)

在Python中内置,每一个类都有一个默认的不带参数的构造方法,不需要人为的单独调用,在调用类的同时就运行了构造方法。

构造方法的作用:初始化数据、创建对象(构造方法的调用)

Python3.5面向对象与继承的示例分析

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:ZhengzhengLiu

class Person:
  def __init__(self):
    print("构造方法")
  pass

Person()  #类的调用--创建对象

运行结果

构造方法

带参数的构造方法:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:ZhengzhengLiu

class Person:
  # def __init__(self):
  #   print("构造方法")
  def __init__(self,x):
    print("带参数的构造方法:",x)

  def add(self,x,y):
    print(x+y)

  pass

zs = Person("hello")  #类的调用--创建对象
zs.add(1,2)

 运行结果:

带参数的构造方法: hello
3

(2)面向对象的思路

Python3.5面向对象与继承的示例分析

(3)类方法

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:ZhengzhengLiu

#类方法
class Person:
  def eat(self):   #类方法
    print("eating...")
  pass

a = Person()    #类方法调用
a.eat()

运行结果:

eating...

(4)私有方法

只允许在类的内部使用,专门为类服务的。

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:ZhengzhengLiu

class Person:
  def eat(self): # 类方法
    print("eating...")
    self.__sleep() # 调用私有方法

  def __sleep(self):   #私有方法--类的外部不能使用
    print("sleeping...")

pass

b = Person()
b.eat()

运行结果:

eating...
sleeping...

6、属性

Python3.5面向对象与继承的示例分析

Python3.5面向对象与继承的示例分析

Python3.5面向对象与继承的示例分析

示例属性、类属性代码:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:ZhengzhengLiu

#属性

class a():
  def __init__(self,name,age):
    self.name = name    #实例属性
    self.age = age

  #类内部使用实例属性 self.实例属性名
  def get(self):
    print(self.name)

a1 = a("Jack",18)

#类内部使用实例属性 self.实例属性名
a1.get()

#类外部使用实例属性 对象名.实例属性名
print(a1.name)

#类属性  在__init__()方法外声明
#类内部使用 类名.属性名 调用
#类外部使用通过 类名.属性名 或者 对象名.属性名 方式调用

class b():
  name = "Janne"   #类属性

  #类内部使用类属性——类名.属性名
  def get(self):
    print(b.name)

#类外部使用类属性 通过 类名.属性名
print(b.name)

#类外部使用类属性 通过 对象名.属性名
b1 = b()
print(b1.name)

#类内部使用类属性——类名.属性名
b1.get()

运行结果:

Jack
Jack
Janne
Janne
Janne

(1)类属性/类变量:在类的外部可以调用

(2)私有变量/私有属性:只能在类的内部,通过self使用

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:ZhengzhengLiu

#属性/变量
class Person:
  i = 10     #类属性/类变量
  __j = 20    #私有属性/私有变量

  def eat(self): # 类方法
    print("eating...")
    print(self.__j) # 调用私有变量

pass

b = Person()
print(b.i)     #通过引用调用(建议)
print(Person.i)   #可通过类名调用
b.eat()

运行结果:

10
10
eating...
20

Python3.5面向对象与继承的示例分析

class GirlFriend():
  #声明对象属性 通过构造方法
  def __init__(self,name,age,phone,pwd):
    #给对象的属性(变量名)前面加上 __ 成为了私有的属性
    self.__name = name
    self.__age = age
    self.__phone = phone
    self.__pwd = pwd

  #通过预留的接口 对私有属性名进行访问或修改
  def getInfo(self,pwd):
    if pwd == "1234":
      print("My girlfriend is %s,and she's %d years old,Her telephone number is %d"%(self.__name,self.__age,self.__phone))
    else:
      print("you failed...")

  def setName(self,name):
    self.__name = name   #类内修改私有属性

gf = GirlFriend("Janne",18,13511112222,"1234")
gf.setName("Malianna")
gf.getInfo("1234")

运行结果:

My girlfriend is Malianna,and she's 18 years old,Her telephone number is 13511112222

Python3.5面向对象与继承的示例分析

(3)特殊的类属性

Python3.5面向对象与继承的示例分析

7、继承

Python中支持多继承,作用:复用,不建议使用多继承(类对象爆炸)、

Python3.5面向对象与继承的示例分析

继承示例代码:

#继承
#父类
class Animal():
  def __init__(self,name,food,drinks):
    self.name = name
    self.food = food
    self.drinks = drinks

  def eat(self):
    print("%s 爱吃 %s" %(self.name,self.food))

  def drink(self):
    print("%s 爱喝 %s" %(self.name,self.drinks))


#子类
class Dog(Animal):

  def sound(self):
    print("wonf wonf...")

class Cat(Animal):

  def sound(self):
    print("miao miao...")

dogs = Dog("哮天犬","骨头","雪碧")
dogs.eat()
dogs.drink()
dogs.sound()

print("========================")
cats = Cat("波斯猫","鱼","可乐")
cats.eat()
cats.drink()
cats.sound()

运行结果:

哮天犬 爱吃 骨头
哮天犬 爱喝 雪碧
wonf wonf...
========================
波斯猫 爱吃 鱼
波斯猫 爱喝 可乐
miao miao...

示例一:

多继承

Python3.5面向对象与继承的示例分析

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:ZhengzhengLiu

#多继承

class Run3000:
  def run(self):
    print("run 3000")

class Jump3:
  def jump(self):
    print("jump 3")

class Sport(Run3000,Jump3):   #继承
  pass

sport = Sport()
sport.run()
sport.jump()

运行结果:

run 3000
jump 3

示例二:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:ZhengzhengLiu

class Father:
  def __init__(self):
    print("father 构造")
  def teach(self):
    print("father teaching")

class Child(Father):
  pass

zs = Child()    #子类继承与父类,创建子类前先创建父类
zs.teach()

运行结果:

father 构造
father teaching

子类中重写父类的方法:重写体现多态

Python3.5面向对象与继承的示例分析

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:ZhengzhengLiu

class Father:
  def __init__(self):
    print("father 构造")
  def teach(self):
    print("father teaching")

class Child(Father):
  def teach(self):    #方法重写
    print("child teaching")

zs = Child()    #子类继承与父类,创建子类前先创建父类
zs.teach()

运行结果:

father 构造
child teaching

新式类:

如果父类的构造方法带参数,则需要子类通过super操作去完成调用。

Python3.5面向对象与继承的示例分析

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:ZhengzhengLiu

#新式类
class Father(object):
  def __init__(self,i):
    print("father 构造"+ i)
  def teach(self):
    print("father teaching")

class Child(Father):
  def __init__(self):
    super(Child,self).__init__("hello")
  def teach(self):    #方法重写
    print("child teaching")

zs = Child()    #子类继承与父类,创建子类前先创建父类
zs.teach()

#运行结果:
father 构造hello
child teaching

运行结果:

father 构造hello
child teaching

多继承又不完全,父类都有构造方法时,当子类多继承时,只有一个父类的构造方法被调用。

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:ZhengzhengLiu

class Run3000:
  def __init__(self):
    print("run 3000 构造方法")
  def run(self):
    print("run 3000")

class Jump3:
  def __init__(self):
    print("jump 3 构造方法")
  def jump(self):
    print("jump 3")

class Sport(Run3000,Jump3):   #继承
  pass

sport = Sport()
sport.run()
sport.jump()

运行结果:

run 3000 构造方法
run 3000
jump 3

8、面向对象编程

(1)定义

Python3.5面向对象与继承的示例分析

(2)示例代码——人开车

Python3.5面向对象与继承的示例分析

Python3.5面向对象与继承的示例分析

关于“Python3.5面向对象与继承的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI