• The language feature most often associated with object-oriented programming is inheritance. Inheritance is the ability to define a new class that is a modified version of an existing class. It is...

The language feature most often associated with object-oriented programming is inheritance. Inheritance is the ability to define a new class that is a modified version of an existing class. It is called ‘inheritance’ because the new class inherits the methods of the existing class. Extending this metaphor, the existing class is called the parent class and the new class is called the child.
As an example, let’s say we want a class to represent a ‘hand’, that is, the set of cards held by one player. A hand is similar to a deck: both are made up of a set of cards, and both require operations like adding and removing cards. A hand is also different from a deck; there are operations we want for hands don’t make sense for a deck. For example, in poker we might compare two hands to see which wins. In bridge, we might compute a score for a hand in order to make a bid. This relationship between classes – similar, but different – lends itself to inheritance.
The definition of a child class is like other class definitions, but the name of the parent class appears in parentheses. The definition indicates that Hand inherits from Deck; that means we can use methods like pop_card and add_card for Hands as well as Decks. Hand also inherits the init method from Deck, but it doesn’t really do what we want: instead of populating the hand with 52 new cards, the init method for hands should initialize cards with an empty list. If we provide an init method in the Hand class, it overrides the one in the Deck class:

class Hand(Deck):
"""represents a hand of playing cards"""

def __init__(self,label=''):
self.cards = []
self.label = label

The next natural step is to encapsulate this code in a method called move_cards inside class Deck:

def move_card(self,hand,num):
for i in range(num):

move_cards takes two arguments, a Hand object and the number of cards to deal. It modifies both self and hand, and returns None.
In some games, cards are moved from one hand to another, or form a hand back to the deck. You can use move_cards for any of these operations: self can be either a Deck or a Hand, and hand, despite the name, can also be a Deck.
Write a Deck method called deal hands that takes two parameters, the number of hands and the number of cards per hand, and that creates new Hand objects, deals the appropriate number of cards per hand, and returns a list of Hand objects.

def deal_hands(self,num_hands,num_cards):
hands = list()
for i in range(num_hands):
hand = Hand()
self.move_card(hand,num_cards)
hands.append(hand)
return hands

from Thinking in Python

转载于:https://www.cnblogs.com/ryansunyu/p/4008521.html
展开全文
• 用c语言编写的遗传算法程序包，无需解压密码
• Inheritance
• Finish header file of the Inheritance class. Hints All function is finished. You need write inheritance relation between 5 class so that it can print right message. A Sample Input 6590 Output ...
Task
Finish header file of the Inheritance class.
Hints
All function is finished.You need write inheritance relation between 5 class so that it can print right message.
A Sample
Input
6590

Output
test token: 6590

creat base A
base A

creat base B
base B

creat derived A
base A
derived A

creat derived B
base A
base B
derived B

creat C
base A
derived A
base A
base B
derived B
C

destructor
~ C
~ derived B
~ base B
~ base A
~ derived A
~ base A
~ derived B
~ base B
~ base A
~ derived A
~ base A
~ base B
~ base A

出题人： 李天培
main.cpp
//
//  main.cpp
//  C++
//
//  Created by 李天培 on 16/2/25.
//
#include <iostream>
#include "Inheritance.hpp"
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main() {
int token;
cin >> token;
cout << "test token: " << token << endl;
cout << "\ncreat base A" << endl;
baseA ba;
cout << "\ncreat base B" << endl;
baseB bb;
cout << "\ncreat derived A" << endl;
derivedA da;
cout << "\ncreat derived B" << endl;
derivedB db;
cout << "\ncreat C" << endl;
C c;
cout << "\ndestructor" << endl;
return 0;
}

Inheritance.cpp
//
//  Inheritance.cpp
//  C++
//
//  Created by 李天培 on 16/3/24.
//
#include "Inheritance.hpp"
baseA::baseA() {
cout << "base A" << endl;
}
baseA::~baseA() {
cout << "~ base A" << endl;
}
baseB::baseB() {
cout << "base B" << endl;
}
baseB::~baseB() {
cout << "~ base B" << endl;
}
derivedA::derivedA() {
cout << "derived A" << endl;
}
derivedA::~derivedA() {
cout << "~ derived A" << endl;
}
derivedB::derivedB() {
cout << "derived B" << endl;
}
derivedB::~derivedB() {
cout << "~ derived B" << endl;
}
C::C() {
cout << "C" << endl;
}
C::~C() {
cout << "~ C" << endl;
}


Inheritance.cpp
#ifndef INHERITANCE_HPP
#define INHERITANCE_HPP
#include<iostream>

using namespace std;

class baseA
{
public:
baseA();
~baseA();
};
class baseB
{
public:
baseB();
~baseB();
};
class derivedA:public baseA
{
public:
derivedA();
~derivedA();
};
class derivedB:public baseA,public baseB
{
public:
derivedB();
~derivedB();
};
class C:public derivedA,public derivedB
{
public:
C();
~C();
};

#endif // INHERITANCE_HPP


展开全文
• Inheritance
overriddenmember accessor  Inheritancepublicprotectedprivatepublicpublicprotectedprivateprotectedprotectedprotectedprivateprivateprivateprivateprivate

*The overridden accessors are for parent members in the child.


展开全文
• Obviously the inheritance of colors from IFCGroups is not implemented here. <p><a href=...
• 5.9. Inheritance 5.9.继承 PostgreSQL implements table inheritance, which can be a useful tool for database designers.(SQL:1999 and later define a type inheritance feature, which differs in many ...

5.9. Inheritance

5.9.继承

PostgreSQL implements table inheritance, which can be a useful tool for database designers.
(SQL:1999 and later define a type inheritance feature, which differs in many respects from the features

described here.)

PostgreSQL实现了表的继承，这对于数据库设计者会是一个有用的工具。（SQL:1999及以后版本定义了一个继承类型特性，这与此处描述的继承不同。）

Let's start with an example: suppose we are trying to build a data model for cities. Each state has many
cities, but only one capital. We want to be able to quickly retrieve the capital city for any particular
state. This can be done by creating two tables, one for state capitals and one for cities that are not
capitals. However, what happens when we want to ask for data about a city, regardless of whether it is
a capital or not? The inheritance feature can help to resolve this problem. We define the
capitals
table so that it inherits from
cities
:

让我们从一个示例开始：假设我们要建立一个城市数据模型。每个州有许多城市，但只有一个首府。我们需要尽快查到任一指定州的首府。这可以通过创建两个表来实现，其中一个州首府表，一个没有首府的表。然而，如果我们想要查询一个城市的信息，而不管它是不是首府，这会怎么样呢？继承特性可以帮助解决此问题。我们创建继承自cities表的capitals表：

CREATE TABLE cities (

name text,

population float,

elevation int -- in feet

);

CREATE TABLE capitals (

state char(2)

) INHERITS (cities);

In this case, the
capitals
table
inherits
all the columns of its parent table,
cities
. State capitals
also have an extra column,
state
, that shows their state.

上例中，capitals表继承其父表cities的所有列。其有一个额外的列state，以显示首府所在的州。

In PostgreSQL, a table can inherit from zero or more other tables, and a query can reference either
all rows of a table or all rows of a table plus all of its descendant tables. The latter behavior is the
default. For example, the following query finds the names of all cities, including state capitals, that
are located at an altitude over 500 feet:

PostgreSQL中，一个表可以继承自零到多个表，一个查询可以查询一个表的所有行或者该表及其所有继承表的所有行。后一种为默认行为。例如，以下查询检索所有海拔高于500英尺的城市，包含州首府：

SELECT name,elevation

FROM cities

WHERE elevation> 500;

Given the sample data from the PostgreSQL tutorial (see Section 2.1), this returns:

假设数据来自PostgreSQL基础教程（
第2.1节
），以上查询会返回：

name |elevation

-----------+----------

Las Vegas | 2174

Mariposa | 1953

On the other hand, the following query finds all the cities that are not state capitals and are situated
at an altitude over 500 feet:

另一方面，以下查询检索非州首府的所有海拔高于500英尺的城市：

SELECT name,elevation

FROM ONLY cities

WHERE elevation> 500;

name |elevation

-----------+----------

Las Vegas | 2174

Mariposa | 1953

Here the
ONLY
keyword indicates that the query should apply only to
cities
, and not any tables
below
cities
in the inheritance hierarchy. Many of the commands that we have already discussed
—
SELECT
,
UPDATE
and
DELETE
— support the
ONLY
keyword.

此处的关键词ONLY意味着查询仅检索cities表中的数据，而不会检索其继承表。我们之前讨论的许多命令都支持ONLY关键词，例如SELECT，UPDATE和DELETE命令。

You can also write the table name with a trailing
*
to explicitly specify that descendant tables are
included:

也可以在表名后加星号以显式指定查询包含其继承表：

SELECT name,elevation

FROM cities*

WHERE elevation > 500;

Writing
*
is not necessary, since this behavior is always the default. However, this syntax is still
supported for compatibility with older releases where the default could be changed.

添加星号是非必须的，因为此行为为默认行为。然而，为了兼容旧的版本（旧版本中默认行为可以被修改），此语法仍然被支持。

In some cases you might wish to know which table a particular row originated from. There is a system
column called
tableoid
in each table which can tell you the originating table:

在有些场景下，你可能想要知道特定行具体属于哪个表。可以通过系统列tableoid以区分：

SELECT c.tableoid, c.name, c.elevation

FROM cities c

WHERE c.elevation> 500;

which returns:

返回：

tableoid | name |elevation

----------+-----------+----------

139793 | Las Vegas | 2174

139793 | Mariposa | 1953

139798 | Madison | 845

(If you try to reproduce this example, you will probably get different numeric OIDs.) By doing a join
with
pg_class
you can see the actual table names:

（实际执行中，OID会有所不同。）通过与pg_class表关联，可以看到实际的表名：

SELECT p.relname, c.name, c.elevation

FROM cities c, pg_class p

WHERE c.elevation> 500 AND c.tableoid = p.oid;

which returns:

返回：

relname | name |elevation

----------+-----------+----------

cities | Las Vegas | 2174

cities | Mariposa | 1953

capitals | Madison | 845

Another way to get the same effect is to use the
regclass
alias type, which will print the table OID
symbolically:

另一种实现的方法是使用regclasss别名类，这可以根据OID打印出表名：

SELECT c.tableoid::regclass, c.name, c.elevation

FROM cities c

WHERE c.elevation> 500;

Inheritance does not automatically propagate data from
INSERT
or
COPY
commands to other tables
in the inheritance hierarchy. In our example, the following
INSERT
statement will fail:

继承并不会自动的将insert或copy命令中的数据分发到继承关系中的其他表。示例中，以下的insert语句会执行失败：

INSERT INTO cities (name, population,elevation, state)

VALUES ('Albany', NULL, NULL, 'NY');

We might hope that the data would somehow be routed to the
capitals
table, but this does not
happen:
INSERT
always inserts into exactly the table specified. In some cases it is possible to redirect
the insertion using a rule (see Chapter 41). However that does not help for the above case because
the
cities
table does not contain the column
state
, and so the command will be rejected before
the rule can be applied.

我们可能希望数据可以被分发到capitals表中，但这不会发生：insert语句只会将数据插入到指定的表中。在一些场景下，可以使用规则来重定向插入操作（参考
第40章
）。
但是，这对上述情况却没有帮助，因为citys表不包含列state，因此在应用规则之前，该命令就已经被拒绝。

All check constraints and not-null constraints on a parent table are automatically inherited by its children,
unless explicitly specified otherwise with
NO INHERIT
clauses. Other types of constraints
(unique, primary key, and foreign key constraints) are not inherited.

如果没有显式的指定NO INHERIT子句，子表会自动继承父表中的检查约束和非空约束。其他类型的约束（唯一、主键和外键约束）则不会被继承。

A table can inherit from more than one parent table, in which case it has the union of the columns
defined by the parent tables. Any columns declared in the child table's definition are added to these.
If the same column name appears in multiple parent tables, or in both a parent table and the child's
definition, then these columns are “merged” so that there is only one such column in the child table. To
be merged, columns must have the same data types, else an error is raised. Inheritable check constraints
and not-null constraints are merged in a similar fashion. Thus, for example, a merged column will be
marked not-null if any one of the column definitions it came from is marked not-null. Check constraints
are merged if they have the same name, and the merge will fail if their conditions are different.

一个表可以继承自多个表，在这种情形下，它为所有父表列的集合加子表中定义的列。如果多父表中有相同列名，或者父表中的列名与子表定义列名相同，那么这些列名会结合到一起，这样子表中会只有一个列。当然，相同列名能够结合是有条件的，那就是需要有相同的数据类型，否则就会报错。检查约束和非空约束继承与此类似。因此，例如，合并前，只要有一列有非空约束，则合并之后的列即会有非空约束。如果检查约束名称相同，则会合并，但是如果检查条件不同，在合并时则会报错。

Table inheritance is typically established when the child table is created, using the
INHERITS
clause
of the CREATE TABLE statement. Alternatively, a table which is already defined in a compatible
way can have a new parent relationship added, using the
INHERIT
variant of ALTER TABLE. To do
this the new child table must already include columns with the same names and types as the columns
of the parent. It must also include check constraints with the same names and check expressions as
those of the parent. Similarly an inheritance link can be removed from a child using the
NO INHERIT
variant of
ALTER TABLE
. Dynamically adding and removing inheritance links like this can be useful
when the inheritance relationship is being used for table partitioning (see Section 5.10).

表继承在使用
CREATE TABLE
语句创建子表时，通过指定INHERITS子句建立。当然，对于已经存在的表，可以使用
ALTER TABLE
的INHERIT子句为其添加父表继承关系。要想使该操作成功，那么现有表必须与父表具有相同的列名且具有相同的数据类型。子表中也必须具有与父表相同的检查约束名称及检查表达式。当然，也可以使用
ALTER TABLE
的NO INHERIT子句以删除继承关系。能够动态的添加和删除继承关系，对于表分区会特别有用（参见
第5.11节
）。

One convenient way to create a compatible table that will later be made a new child is to use the
LIKE
clause in
CREATE TABLE
. This creates a new table with the same columns as the source table.
If there are any
CHECK
constraints defined on the source table, the
INCLUDING CONSTRAINTS
option to
LIKE
should be specified, as the new child must have constraints matching the parent to
be considered compatible.

使用
CREATE TABLE
中的LIKE子句，可以方便的创建稍后成为新子表的兼容表。该语句会创建一个与原表相同列的新表。如果原表中定义了检查约束，为了兼容起见，需要在LIKE子句中指定INCLUDING CONSTRAINTS选项，以使新子表与父表具有相同的约束。

A parent table cannot be dropped while any of its children remain. Neither can columns or check
constraints of child tables be dropped or altered if they are inherited from any parent tables. If you
wish to remove a table and all of its descendants, one easy way is to drop the parent table with the
option (see Section 5.13).

5.14节
）。

ALTER TABLE will propagate any changes in column data definitions and check constraints down
the inheritance hierarchy. Again, dropping columns that are depended on by other tables is only possible
when using the
option.
ALTER TABLE
follows the same rules for duplicate column
merging and rejection that apply during
CREATE TABLE
.

ALTER TABLE
CREATE TABLE
期间应用的重复列合并和拒绝方式，
ALTER TABLE
遵循相同的规则。

Inherited queries perform access permission checks on the parent table only. Thus, for example, granting
UPDATE
permission on the
cities
table implies permission to update rows in the
capitals
table as well, when they are accessed through
cities
. This preserves the appearance that the data
is (also) in the parent table. But the
capitals
table could not be updated directly without an addi
tional grant. In a similar way, the parent table's row security policies (see Section 5.7) are applied to
rows coming from child tables during an inherited query. A child table's policies, if any, are applied
only when it is the table explicitly named in the query; and in that case, any policies attached to its
parent(s) are ignored.

继承查询仅检查父表上的访问权限。因此，例如，授予cities表的update权限意味着通过cities表访问的时候，也可以更新capitals表中的行。但是，如果没有进一步的授权，则不可以直接对表capitals进行修改。类似的，在继承查询中，父表的行级安全策略（参见
5.8节
）会应用到来自子表的行。而对于子表中，如果存在策略，那么仅当该子表表名被显式引用的时候才有效，此时，会忽略父表的策略。

Foreign tables (see Section 5.11) can also be part of inheritance hierarchies, either as parent or child
tables, just as regular tables can be. If a foreign table is part of an inheritance hierarchy then any
operations not supported by the foreign table are not supported on the whole hierarchy either.

外表（参见第5.12节）也可以
像普通表那样，
作为父表或子表
作为继承的一部分。如果外表作为继承层次的一部分，那么整个继承层次中，任何为外表所不允许的操作均不受支持。

展开全文
• 5.9. Inheritance 5.9.继承 PostgreSQL implements table inheritance, which can be a useful tool for database designers.(SQL:1999 and later define a type inheritance feature, which differs in many ...
• template-inheritance
• Demo_Inheritance
• ES6_Inheritance
• Inheritance: An Is-a Relationship A banana is a fruit, but a lunch has a banana. The Need for Virtual Destructors If the destructors are not not virtual, then just the destructor corresponding to the ...
• 关于继承 http://blog.vjeux.com/2011/javascript/how-prototypal-inheritance-really-works.html
• A kingdom consists of a king, his children, his grandchildren, and so on....The kingdom has a well-defined order of inheritance that consists of the king as the first member. Let's define
• Topic 10 Inheritance.md
• • Many C++ derivative languages do away with multiple inheritance given issues like property & method name clashes • However, if you define abstract classes as interfaces (all pure virtual ...
• 3.6. Inheritance 3.6.继承 Inheritance is a concept from object-oriented databases. It opens up interesting new possibilities ofdatabase design. 继承是来自于面向对象数据库的一个概念。它为数据库设计...
• http://blog.tangcs.com/2008/07/19/javascript-inheritance/ 转载于:https://www.cnblogs.com/WarrenTang/archive/2008/07/19/1246758.html
• inheritance_art.zip
• Inheritance ---->>> 成员变量 >> Java只支持单继承，不支持多继承；但，支持多重继承； 也可以通过多实现的方式体现； >> 子父类中的同名成员变量，子类对象调用时，调用子类中的该同名...

...