Exercise 44: Inheritance Vs. Composition

class Parent(object):
    def __init__(self, **kwargs):
        if kwargs.has_key(‘age‘):
            self.__age = kwargs[‘age‘]
        if kwargs.has_key(‘sex‘):
            self.sex = kwargs[‘sex‘]
    def implicit(self):
        print "PARENT implicit()"
    def get_age(self):
        print self.__age
class Child(Parent):
    pass

dad = Parent(sex = ‘male‘, age = 45)
son = Child()
dad.implicit()
son.implicit()

解析**kwargs: http://stackoverflow.com/questions/5624912/kwargs-parsing-best-practice

两个下划线__开头为私有变量或私有函数。

class Parent(object):
    def altered(self):
        print "PARENT altered()"
class Child(Parent):
    def altered(self):
        print "CHILD, BEFORE PARENT altered()"
        super(Child, self).altered()
        print "CHILD, AFTER PARENT altered()"
dad = Parent()
son = Child()
dad.altered()
son.altered()

上面是更改继承。

class Other(object):
    def override(self):
        print "OTHER override()"
def implicit(self):
    print "OTHER implicit()"
def altered(self):
    print "OTHER altered()"
class Child(object):
    def __init__(self):
self.other = Other()
def implicit(self):
    self.other.implicit()
def override(self):
    print "CHILD override()"
def altered(self):
    print "CHILD, BEFORE OTHER altered()"
    self.other.altered()
    print "CHILD, AFTER OTHER altered()"
son = Child()
son.implicit()
son.override()
son.altered()

合成composition

时间: 2024-10-14 08:04:53

Exercise 44: Inheritance Vs. Composition的相关文章

Inheritance versus composition

One of the fundamental activities of an object-oriented design is establishing relationships between classes. Two fundamental ways to relate classes are inheritance and composition. Although the compiler and Java virtual machine (JVM) will do a lot o

Inheritance vs. Composition in Java

This article illustrates the concepts of inheritance vs. composition in Java. It first shows an example of inheritance, and then shows how to improve the inheritance design by using composition. How to choose between them is summarized at the end. 1.

Python Index

Table Of ContentsThe Hard Way Is EasierExercise 0: The SetupExercise 1: A Good First ProgramExercise 2: Comments And Pound CharactersExercise 3: Numbers And MathExercise 4: Variables And NamesExercise 5: More Variables And PrintingExercise 6: Strings

Object Oriented Programming python

new concepts of the object oriented programming : class encapsulation inheritance polymorphism the three features of an object are : identity, state and behaviora class is an abstraction which regroup objects who have the same attributes and the same

Java Interview Reference Guide--reference

Part 1 http://techmytalk.com/2014/01/24/java-interview-reference-guide-part-1/ Posted on January 24, 2014 by Nitin Kumar JAVA Object Oriented Concepts Java in based on Object Oriented concepts, which permits higher level of abstraction to solve any p

Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(八)之Reusing Classes

The trick is to use the classes without soiling the existing code. 1. composition--simply create objects of your existing class inside the new class. simply reusing the functionality of the code, not its form 2.inheritance--creates a new class as a t

GeekBand第二周学习笔记

Copy Ctor(拷贝构造),copy assignment operator(拷贝赋值) Copy Ctor(拷贝构造):默认的拷贝构造函数会将其引用类型的引用拷贝到新实例,若用指针指向两实例的同一引用类型则两指针的值是相同的即指向同一内存地址. 1 String::String(const String& str) 2 { 3 m_data=new char[strlen(str.m_data)+1]; 4 strcopy(m_data,str.m_data); 5 } Copy assi

TIJ英文原版书籍阅读之旅——Chapter Seven:Reusing Classes

Reusing Classes 有两种常用方式实现类的重用,组件(在新类中创建存在类的对象)和继承. Composition syntax Every non-primitive object has a toString() method, and it’s called in special situations when the compiler wants a String but it has an object. Inheritance syntax You’re always do

菜鸟译文(一)——Java中的继承和组合

阅读英文的能力对于程序员来说,是很重要的.这几年也一直在学习英文,今天心血来潮,就在网上找了一篇简短的博文翻译一下.水平一般,能力有限,还请各位看官多多指点. 译文: 本文将会举例说明Java中继承和组合的概念.首先举一个继承的例子,然后展示一下如何用组合来改善继承的设计.最后概括一下如何在它们之间做出选择. 1. 继承 假设我们有一个Insect类.这个类包含两个方法:一个是move(),一个是attack(). class Insect { private int size; private