mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge pull request #6841 from ethereum/style-fixes-inheritance
[DOCS] Bring inheritance doc inline with style guide
This commit is contained in:
commit
9d00f21cfb
@ -27,21 +27,24 @@ Details are given in the following example.
|
|||||||
|
|
||||||
pragma solidity >=0.5.0 <0.7.0;
|
pragma solidity >=0.5.0 <0.7.0;
|
||||||
|
|
||||||
contract owned {
|
|
||||||
|
contract Owned {
|
||||||
constructor() public { owner = msg.sender; }
|
constructor() public { owner = msg.sender; }
|
||||||
address payable owner;
|
address payable owner;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Use `is` to derive from another contract. Derived
|
// Use `is` to derive from another contract. Derived
|
||||||
// contracts can access all non-private members including
|
// contracts can access all non-private members including
|
||||||
// internal functions and state variables. These cannot be
|
// internal functions and state variables. These cannot be
|
||||||
// accessed externally via `this`, though.
|
// accessed externally via `this`, though.
|
||||||
contract mortal is owned {
|
contract Mortal is Owned {
|
||||||
function kill() public {
|
function kill() public {
|
||||||
if (msg.sender == owner) selfdestruct(owner);
|
if (msg.sender == owner) selfdestruct(owner);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// These abstract contracts are only provided to make the
|
// These abstract contracts are only provided to make the
|
||||||
// interface known to the compiler. Note the function
|
// interface known to the compiler. Note the function
|
||||||
// without body. If a contract does not implement all
|
// without body. If a contract does not implement all
|
||||||
@ -50,15 +53,17 @@ Details are given in the following example.
|
|||||||
function lookup(uint id) public returns (address adr);
|
function lookup(uint id) public returns (address adr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
contract NameReg {
|
contract NameReg {
|
||||||
function register(bytes32 name) public;
|
function register(bytes32 name) public;
|
||||||
function unregister() public;
|
function unregister() public;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Multiple inheritance is possible. Note that `owned` is
|
// Multiple inheritance is possible. Note that `owned` is
|
||||||
// also a base class of `mortal`, yet there is only a single
|
// also a base class of `mortal`, yet there is only a single
|
||||||
// instance of `owned` (as for virtual inheritance in C++).
|
// instance of `owned` (as for virtual inheritance in C++).
|
||||||
contract named is owned, mortal {
|
contract Named is Owned, Mortal {
|
||||||
constructor(bytes32 name) public {
|
constructor(bytes32 name) public {
|
||||||
Config config = Config(0xD5f9D8D94886E70b06E474c3fB14Fd43E2f23970);
|
Config config = Config(0xD5f9D8D94886E70b06E474c3fB14Fd43E2f23970);
|
||||||
NameReg(config.lookup(1)).register(name);
|
NameReg(config.lookup(1)).register(name);
|
||||||
@ -75,15 +80,16 @@ Details are given in the following example.
|
|||||||
NameReg(config.lookup(1)).unregister();
|
NameReg(config.lookup(1)).unregister();
|
||||||
// It is still possible to call a specific
|
// It is still possible to call a specific
|
||||||
// overridden function.
|
// overridden function.
|
||||||
mortal.kill();
|
Mortal.kill();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// If a constructor takes an argument, it needs to be
|
// If a constructor takes an argument, it needs to be
|
||||||
// provided in the header (or modifier-invocation-style at
|
// provided in the header (or modifier-invocation-style at
|
||||||
// the constructor of the derived contract (see below)).
|
// the constructor of the derived contract (see below)).
|
||||||
contract PriceFeed is owned, mortal, named("GoldFeed") {
|
contract PriceFeed is Owned, Mortal, Named("GoldFeed") {
|
||||||
function updateInfo(uint newInfo) public {
|
function updateInfo(uint newInfo) public {
|
||||||
if (msg.sender == owner) info = newInfo;
|
if (msg.sender == owner) info = newInfo;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user