3 [面向对象]-组合,抽象类,接口

1、组合与重用性

软件重用的重要方式除了继承之外还有另外一种方式,即:组合

组合指的是,在一个类中以另外一个类的对象作为数据属性,称为类的组合

 

2、继承与组合

组合与继承都是有效地利用已有类的资源的重要方式。但是二者的概念和使用场景皆不同,

  1).继承的方式

通过继承建立了派生类与基类之间的关系,它是一种'是'的关系,比如白马是马,人是动物。

当类之间有很多相同的功能,提取这些共同的功能做成基类,用继承比较好,比如老师是人,学生是人

## 继承
class People(object):
    school = 'Luffycity'

    def __init__(self, name, gender, age):
        self.name = name
        self.gender = gender
        self.age = age


class Teacher(People):
    def __init__(self, name, gender, age, level, salary):
        super(Teacher, self).__init__(name, gender, age)
        self.level = level
        self.salary = salary


class Student(People):
    def __init__(self, name, gender, age, class_time):
        super().__init__(name, gender, age)
        self.class_time = class_time


tea1 = Teacher('alex', '', 22, 'SSS', 99999)
stu1 = Student('张三', '', 32, '8:45')

 

 

  2).组合的方式

用组合的方式建立了类与组合的类之间的关系,它是一种‘有’的关系,比如教授有生日,教授教python和linux课程,教授有学生s1、s2、s3...

示例:继承与组合

  (1)添加课程属性

## 一段代码如下

class People(object):
    school = 'Luffycity'

    def __init__(self, name, gender, age):
        self.name = name
        self.gender = gender
        self.age = age


class Teacher(People):
    def __init__(self, name, gender, age, level, salary, course_name, course_price, course_period):
        super(Teacher, self).__init__(name, gender, age)
        self.level = level
        self.salary = salary

        self.course_name = course_name
        self.course_price = course_price
        self.course_period = course_period


class Student(People):
    def __init__(self, name, gender, age, class_time):
        super().__init__(name, gender, age)
        self.class_time = class_time


tea1 = Teacher('alex', '', 22, 'SSS', 99999, 'python', 9999, '3mons')
stu1 = Student('张三', '', 32, '8:45')

print(tea1.course_name)

 

 

  (2)课程拆分成类

class People(object):
    school = 'Luffycity'

    def __init__(self, name, gender, age):
        self.name = name
        self.gender = gender
        self.age = age


class Teacher(People):
    def __init__(self, name, gender, age, level, salary):
        super(Teacher, self).__init__(name, gender, age)
        self.level = level
        self.salary = salary


class Course(object):
    def __init__(self, course_name, course_price, course_period):
        self.course_name = course_name
        self.course_price = course_price
        self.course_period = course_period


class Student(People):
    def __init__(self, name, gender, age, class_time):
        super().__init__(name, gender, age)
        self.class_time = class_time

 

 

doc1 = Teacher('alex', '', 22, 'SSS', 22)
doc2 = Teacher('alex2', '男2', 32, 'SSS', 22)

python = Course('python', 9999, '4mons')

doc1.course = python  # 组合
doc2.course = python   # # doc1.属性  = python对象

print(python)       # python对象
print(doc1.course)  # doc1的属性
print(doc2.course)

print(python.course_name)   # python对象的course_name属性
print(doc1.course.course_name)    

 

 

## 运行结果
<__main__.Course object at 0x01E424F0>
<__main__.Course object at 0x01E424F0>
<__main__.Course object at 0x01E424F0>
python
python

 

 

 

  (3)组合如何调用方法?

class People(object):
    school = 'Luffycity'
    def __init__(self, name, gender, age):
        self.name = name
        self.gender = gender
        self.age = age

class Teacher(People):
    def __init__(self, name, gender, age, level, salary):
        super(Teacher, self).__init__(name, gender, age)
        self.level = level
        self.salary = salary

class Course(object):
    def __init__(self, course_name, course_price, course_period):
        self.course_name = course_name
        self.course_price = course_price
        self.course_period = course_period
    def tell_info(self):
        print('课程:%s 价格:%s 周期%s ' % (self.course_name, self.course_price, self.course_period))

class Student(People):
    def __init__(self, name, gender, age, class_time):
        super().__init__(name, gender, age)
        self.class_time = class_time


doc1 = Teacher('alex', '', 22, 'SSS', 22)
doc2 = Teacher('alex2', '男2', 32, 'SSS', 22)

python = Course('python', 9999, '4mons')
linux = Course('linux', 9999, '4mons')
go = Course('go', 9999, '4mons')


doc1.course = python  # 组合
doc2.course = python

print(python)
print(doc1.course)
print(doc2.course)

print(python.course_name)
print(doc1.course.course_name)

doc1.course.tell_info()  # #可以使用组合的类产生的对象所持有的方法

 

# 运行结果
<__main__.Course object at 0x006425B0>
<__main__.Course object at 0x006425B0>
<__main__.Course object at 0x006425B0>
python
python
课程:python 价格:9999 周期4mons 

 

  

 

  (4)多个对象添加到第一个对象的属性  [ ]

class People(object):
    school = 'Luffycity'

    def __init__(self, name, gender, age):
        self.name = name
        self.gender = gender
        self.age = age


class Teacher(People):
    def __init__(self, name, gender, age, level, salary):
        super(Teacher, self).__init__(name, gender, age)
        self.level = level
        self.salary = salary


class Course(object):
    def __init__(self, course_name, course_price, course_period):
        self.course_name = course_name
        self.course_price = course_price
        self.course_period = course_period

    def tell_info(self):
        print('课程:%s 价格:%s 周期%s ' % (self.course_name, self.course_price, self.course_period))


class Student(People):
    def __init__(self, name, gender, age, class_time):
        super().__init__(name, gender, age)
        self.class_time = class_time


doc1 = Teacher('alex', '', 22, 'SSS', 22)
doc2 = Teacher('alex2', '男2', 32, 'SSS', 22)

python = Course('python', 9999, '4mons')
linux = Course('linux', 9999, '4mons')
go = Course('go', 9999, '4mons')


# doc1.course1 = python  # 组合
# doc1.course2 = linux        # doc1.属性  = python对象
doc1.courses = []
doc1.courses.append(python)
doc1.courses.append(linux)

for item in doc1.courses:
    item.tell_info()

 

 

  (5)添加Date类

class People(object):
    school = 'Luffycity'

    def __init__(self, name, gender, age):
        self.name = name
        self.gender = gender
        self.age = age


class Teacher(People):
    def __init__(self, name, gender, age, level, salary):
        super(Teacher, self).__init__(name, gender, age)
        self.level = level
        self.salary = salary


class Course(object):
    def __init__(self, course_name, course_price, course_period):
        self.course_name = course_name
        self.course_price = course_price
        self.course_period = course_period

    def tell_info(self):
        print('课程:%s 价格:%s 周期%s ' % (self.course_name, self.course_price, self.course_period))


class Student(People):
    def __init__(self, name, gender, age, class_time):
        super().__init__(name, gender, age)
        self.class_time = class_time


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

    def tell_info(self):
        print('生日:%s-%s-%s' % (self.year, self.month, self.day))


stu1 = Student('stu1', 'nv', 11, 2012)
stu1.date = Date(1994,9,9)
# stu1.date 是stu1对象的属性
# Date() 实例化个对象

stu1.date.tell_info()

 

 

 

总结:

当类之间有显著不同,并且较小的类是较大的类所需要的组件时,用组合比较好

# 代码重用的2种方式
#
#  组合:
# 
#    学生有课程
#
#   学生有生日
#
# 继承
#
#   人是动物

  

3、接口与归一化设计

  1)什么是接口

  hi boy,给我开个查询接口。。。此时的接口指的是:自己提供给使用者来调用自己功能的方式\方法\入口,

 

java中的interface使用如下

=================第一部分:Java 语言中的接口很好的展现了接口的含义: IAnimal.java
/*
* Java的Interface接口的特征:
* 1)是一组功能的集合,而不是一个功能
* 2)接口的功能用于交互,所有的功能都是public,即别的对象可操作
* 3)接口只定义函数,但不涉及函数实现
* 4)这些功能是相关的,都是动物相关的功能,但光合作用就不适宜放到IAnimal里面了 */

package com.oo.demo;
public interface IAnimal {
    public void eat();
    public void run(); 
    public void sleep(); 
    public void speak();
}

=================第二部分:Pig.java:猪”的类设计,实现了IAnnimal接口 
package com.oo.demo;
public class Pig implements IAnimal{ //如下每个函数都需要详细实现
    public void eat(){
        System.out.println("Pig like to eat grass");
    }

    public void run(){
        System.out.println("Pig run: front legs, back legs");
    }

    public void sleep(){
        System.out.println("Pig sleep 16 hours every day");
    }

    public void speak(){
        System.out.println("Pig can not speak"); }
}

=================第三部分:Person2.java
/*
*实现了IAnimal的“人”,有几点说明一下: 
* 1)同样都实现了IAnimal的接口,但“人”和“猪”的实现不一样,为了避免太多代码导致影响阅读,这里的代码简化成一行,但输出的内容不一样,实际项目中同一接口的同一功能点,不同的类实现完全不一样
* 2)这里同样是“人”这个类,但和前面介绍类时给的类“Person”完全不一样,这是因为同样的逻辑概念,在不同的应用场景下,具备的属性和功能是完全不一样的 */

package com.oo.demo;
public class Person2 implements IAnimal { 
    public void eat(){
        System.out.println("Person like to eat meat");
    }

    public void run(){
        System.out.println("Person run: left leg, right leg");
    }

    public void sleep(){
        System.out.println("Person sleep 8 hours every dat"); 
    }

    public void speak(){
        System.out.println("Hellow world, I am a person");
    } 
}

=================第四部分:Tester03.java
package com.oo.demo;

public class Tester03 {
    public static void main(String[] args) {
        System.out.println("===This is a person==="); 
        IAnimal person = new Person2();
        person.eat();
        person.run();
        person.sleep();
        person.speak();

        System.out.println("\n===This is a pig===");
        IAnimal pig = new Pig();
        pig.eat();
        pig.run();
        pig.sleep();
        pig.speak();
    } 
}

 java中的interface
java中的interface

 

  2)为何要用接口

接口提取了一群类共同的函数,可以把接口当做一个函数的集合。然后让子类去实现接口中的函数。

这么做的意义在于归一化,什么叫归一化,就是只要是基于同一个接口实现的类,那么所有的这些类产生的对象在使用时,从用法上来说都一样。

归一化的好处在于:

  1. 归一化让使用者无需关心对象的类是什么,只需要的知道这些对象都具备某些功能就可以了,这极大地降低了使用者的使用难度。
  2. 归一化使得高层的外部使用者可以不加区分的处理所有接口兼容的对象集合
    1. 就好象linux的泛文件概念一样,所有东西都可以当文件处理,不必关心它是内存、磁盘、网络还是屏幕(当然,对底层设计者,当然也可以区分出“字符设备”和“块设备”,然后做出针对性的设计:细致到什么程度,视需求而定)。
    2. 再比如:我们有一个汽车接口,里面定义了汽车所有的功能,然后由本田汽车的类,奥迪汽车的类,大众汽车的类,他们都实现了汽车接口,这样就好办了,大家只需要学会了怎么开汽车,那么无论是本田,还是奥迪,还是大众我们都会开了,开的时候根本无需关心我开的是哪一类车,操作手法(函数调用)都一样

  3). 模仿interface

在python中根本就没有一个叫做interface的关键字,如果非要去模仿接口的概念

可以借助第三方模块:http://pypi.python.org/pypi/zope.interface

也可以使用继承,其实继承有两种用途

一:继承基类的方法,并且做出自己的改变或者扩展(代码重用):实践中,继承的这种用途意义并不很大,甚至常常是有害的。因为它使得子类与基类出现强耦合。

二:声明某个子类兼容于某基类,定义一个接口类(模仿java的Interface),接口类中定义了一些接口名(就是函数名)且并未实现接口的功能,子类继承接口类,并且实现接口中的功能

 

  (1)人,猪,狗都是run

class People:
    def walk(self):
        print('People is  walking')


class Pig:
    def go(self):
        print('Pig is  walking')


class Dog:
    def run(self):
        print('Dog is  walking')

p1 = People()
p2 = Pig()
p3 = Dog()

p1.walk()
p2.go()
p3.run()

 

 

  (2)实现接口

class Animal(object):
    def run(self):
        print(' is runing')
    def eat(self):
        print('is eating')


class People(Animal):
    def walk(self):
        print('People is  walking')
    def eat(self):
        print('People is eating')

class Pig(Animal):
    def go(self):
        print('Pig is  walking')
    def eat(self):
        print('pig is eating')

class Dog:
    def run(self):
        print('Dog is  walking')
    def eat(self):
        print('Dog is eating')

p1 = People()
p2 = Pig()
p3 = Dog()

# p1.walk()
# p2.go()
# p3.run()

p1.eat()
p2.eat()
p3.eat()

 

  上面的代码只是看起来像接口,其实并没有起到接口的作用,子类完全可以不用去实现接口 ,这就用到了抽象类

 

4、抽象类

  1 、什么是抽象类

与java一样,python也有抽象类的概念但是同样需要借助模块实现,抽象类是一个特殊的类,它的特殊之处在于只能被继承,不能被实例化

  

  2 、为什么要有抽象类

如果说类是从一堆对象中抽取相同的内容而来的,那么抽象类就是从一堆类中抽取相同的内容而来的,内容包括数据属性和函数属性。

  比如我们有香蕉的类,有苹果的类,有桃子的类,从这些类抽取相同的内容就是水果这个抽象的类,你吃水果时,要么是吃一个具体的香蕉,要么是吃一个具体的桃子。。。。。。你永远无法吃到一个叫做水果的东西。

从设计角度去看,如果类是从现实对象抽象而来的,那么抽象类就是基于类抽象而来的。

  从实现角度来看,抽象类与普通类的不同之处在于:抽象类中只能有抽象方法(没有实现功能),该类不能被实例化,只能被继承,且子类必须实现抽象方法。这一点与接口有点类似,但其实是不同的,即将揭晓答案

 

  

  3. 在python中实现抽象类

import abc

class Animal(object, metaclass=abc.ABCMeta):  # 只能被继承,不能被实例化
    all_type = 'animal'
    @abc.abstractmethod
    def run(self):
        pass

    @abc.abstractmethod
    def eat(self):
        pass

# animal = Animal()  # 只能被继承,不能被实例化

class People(Animal):
    def run(self):                  # run方法名称和aniaml相同
        print('People is  walking')
    def eat(self):                  # eat方法名称和aniaml相同
        print('People is eating')

class Pig(Animal):
    def run(self):
        print('Pig is  walking')
    def eat(self):
        print('Pig is eating')

class Dog:
    def run(self):
        print('Dog is  walking')
    def eat(self):
        print('Dog is eating')

p1 = People()
p2 = Pig()
p3 = Dog()

# p1.walk()
# p2.go()
# p3.run()

p1.eat()
p2.eat()
p3.eat()

 

 

  4、练习

  (1)代码

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

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


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

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

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

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

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

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

 

  (2)抽象类实现:只能被继承不能被,实例化

#一切皆文件
import abc #利用abc模块实现抽象类

class All_file(metaclass=abc.ABCMeta):
    all_type='file'
    @abc.abstractmethod #定义抽象方法,无需实现功能
    def read(self):
        '子类必须定义读功能'
        pass

    @abc.abstractmethod #定义抽象方法,无需实现功能
    def write(self):
        '子类必须定义写功能'
        pass

# class Txt(All_file):
#     pass
#
# t1=Txt() #报错,子类没有定义抽象方法

class Txt(All_file): #子类继承抽象类,但是必须定义read和write方法
    def read(self):
        print('文本数据的读取方法')

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

class Sata(All_file): #子类继承抽象类,但是必须定义read和write方法
    def read(self):
        print('硬盘数据的读取方法')

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

class Process(All_file): #子类继承抽象类,但是必须定义read和write方法
    def read(self):
        print('进程数据的读取方法')

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

wenbenwenjian=Txt()

yingpanwenjian=Sata()

jinchengwenjian=Process()

#这样大家都是被归一化了,也就是一切皆文件的思想
wenbenwenjian.read()
yingpanwenjian.write()
jinchengwenjian.read()

print(wenbenwenjian.all_type)
print(yingpanwenjian.all_type)
print(jinchengwenjian.all_type)

 

 

 

5、 抽象类与接口

抽象类的本质还是类,指的是一组类的相似性,包括数据属性(如all_type)和函数属性(如read、write),而接口只强调函数属性的相似性。

抽象类是一个介于类和接口直接的一个概念,同时具备类和接口的部分特性,可以用来实现归一化设计

 

posted @ 2018-03-19 22:21  venicid  阅读(227)  评论(0编辑  收藏  举报