码迷,mamicode.com
首页 > 编程语言 > 详细

python3 面向对象、类、继承、组合、派生、接口、子类重用父类方法

时间:2017-07-05 01:18:01      阅读:288      评论:0      收藏:0      [点我收藏+]

标签:span   name   重用   ldb   子类   interface   mic   方法   父类   

对象是特征(变量)与技能(函数)的结合体
是一系列对象共同的特征与技能的集合体

class teacher:

    lesson = "python"

    def __init__(self,name,color,age):  # 只干初始化的活
        self.name = name
        if not isinstance(name,str):
            raise TypeError
        self.color = color
        self.age = age

    def jineng(self):
        print(新技能)

t1 = teacher(as,pink,13)   # t1可以成为teacher类的一个实例,或对象
class teacher:
    # 特征
    school = oldboy
    lesson_default = python

    # 技能
    def __init__(self,name,age,sex,lesson2):
        self.name = name
        self.age = age
        self.sex = sex
        self.lesson2 = lesson2
    def speak(self):
        print(讲课)
    def eat(self):
        print(吃饭)

t1 = teacher(alex,23,,运维)
t2 = teacher(egon,24,,运维)


类的用法一:实例化产生对象
类的用法二:属性引用
print(teacher.__dict__)


# 对象的属性
# 类的属性

# 属性,就是一个字典
# 对象查看一个属性,先从对象属性找,如果没有,就从类找



继承:
继承是用来创建新的类的一种方式,好处是可以减少重复代码
继承是类与类之间的关系,是一种什么是什么的关系

class People:
    def __init__(self,name,age,sex):
        self.name = name
        self.age = age
        self.sex = sex
    def walk(self):
        print(%s is walking %self.name)
    def foo(self):
        print(from father %s %self.name)

class Teacher(People):
    school = oldboy
    def __init__(self,name,age,sex,level,salary):
        People.__init__(self,name,age,sex)
        self.level = level
        self.salary = salary
    def bar(self):
        People.foo(self)
        print(from teacher)

class Student(People):
    pass

t = Teacher(egon,13,male,10,3000)
# print(t.name,t.age)
print(t.__dict__)
t.walk()
t.bar()

组合
组合是一种什么有什么的关系

class People:
    def __init__(self, name, age, year, mon, day):
        self.name = name
        self.age = age
        self.birth = Date(year, mon, day)   # 组合

    def walk(self):
        print(%s is walking % self.name)


class Date:
    def __init__(self, year, mon, day):
        self.year = year
        self.mon = mon
        self.day = day

    def tell_birth(self):
        print(出生于<%s>年 <%s>月 <%s>日 % (self.year, self.mon, self.day))


class Teacher(People):
    def __init__(self, name, age, year, mon, day, level, salary):
        People.__init__(self, name, age, year, mon, day)
        self.level = level
        self.salary = salary

    def teach(self):
        print(%s is teaching % self.name)


class Student(People):
    def __init__(self, name, age, year, mon, day, group):
        People.__init__(self, name, age, year, mon, day)
        self.group = group

    def study(self):
        print(%s is studying % self.name)

t = Teacher(egon, 12, 1991, 12, 12, 10, 1000)
t.teach()
t.walk()
print(t.level)
print(t.birth.tell_birth())


接口
1.子类必须要有父类的方法
2.子类实现的方法必须跟父类的方法的名字一样

class File:#定义接口Interface类来模仿接口的概念,python中压根就没有interface关键字来定义一个接口。
    def read(self): #定接口函数read
        pass

    def write(self): #定义接口函数write
        pass

class Txt(File): #文本,具体实现read和write
    def du(self):
        print(文本数据的读取方法)

    def xie(self):
        print(文本数据的读取方法)

class Sata(File): #磁盘,具体实现read和write
    def read(self):
        print(硬盘数据的读取方法)

    def write(self):
        print(硬盘数据的读取方法)

class Process(File):
    def read(self):
        print(进程数据的读取方法)

    def write(self):
        print(进程数据的读取方法)

txt=Txt()
disk=Sata()
process=Process()

txt.du()
# disk.read()
# process.read()


子类实现的方法必须跟父类的方法的名字一样
import abc

class File(metaclass=abc.ABCMeta):
    @abc.abstractmethod
    def read(self):
        pass

    @abc.abstractmethod
    def write(self):
        pass

class Txt(File):  # 文本,具体实现read和write
    def read(self):
        pass

    def write(self):
        pass

t = Txt()

 

 
 

python3 面向对象、类、继承、组合、派生、接口、子类重用父类方法

标签:span   name   重用   ldb   子类   interface   mic   方法   父类   

原文地址:http://www.cnblogs.com/lucaq/p/7119185.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!