📜  Solidity——继承

📅  最后修改于: 2022-05-13 01:54:19.856000             🧑  作者: Mango

Solidity——继承

继承是面向对象编程语言最重要的特性之一。它是一种扩展程序功能的方式,用于分离代码,减少依赖,增加现有代码的可重用性。 Solidity 支持智能合约之间的继承,多个合约可以被继承为一个合约。其他合约继承特征的合约称为基础合约,而继承这些特征的合约称为派生合约。简单地说,它们被称为父子合同。 Solidity 中的继承范围仅限于公共和内部修饰符。 Solidity 的一些主要亮点是:

  • 派生合约可以访问所有非私有成员,包括状态变量和内部方法。但不允许使用它。
  • 如果函数签名保持不变,则允许函数覆盖。如果输出参数不同,编译会失败。
  • 我们可以使用 super 关键字或使用超级合约名称来调用超级合约的函数。
  • 在多重继承的情况下,使用 super 的函数调用优先于大多数派生合约。

Solidity 提供了不同类型的继承。

1. 单一继承

在单级或单级继承中,一个基础合约的函数和变量仅被继承到一个派生合约。

示例:在下面的示例中,合同父级由合同子级继承,以演示单继承。

Solidity
// Solidity program to
// demonstrate
// Single Inheritance
pragma solidity >=0.4.22 <0.6.0; 
 
// Defining contract 
contract parent{ 
 
    // Declaring internal
    // state variable 
    uint internal sum; 
       
    // Defining external function
    // to set value of internal
    // state variable sum
    function setValue() external { 
        uint a = 10;
        uint b = 20;
        sum = a + b;
    } 
} 
 
// Defining child contract 
contract child is parent{ 
      
    // Defining external function
    // to return value of
    // internal state variable sum
    function getValue(
    ) external view returns(uint) { 
        return sum; 
    } 
} 
   
// Defining calling contract
contract caller {
 
    // Creating child contract object
    child cc = new child();  
     
    // Defining function to call
    // setValue and getValue functions
    function testInheritance(
    ) public returns (uint) { 
        cc.setValue(); 
        return cc.getValue(); 
    } 
}


Solidity
// Solidity program to
// demonstrate Multi-Level
//  Inheritance
pragma solidity >=0.4.22 <0.6.0; 
 
// Defining parent contract A
contract A { 
 
     // Declaring state variables
     string internal x;
     string a = "Geeks" ;
     string b = "For";
 
    // Defining external function
    // to return concatenated string
    function getA() external{ 
        x = string(abi.encodePacked(a, b));
    } 
} 
 
// Defining child contract B
// inheriting parent contract A
contract B is A { 
 
      // Declaring state variables
      // of child contract B
      string public y;
      string c = "Geeks";
 
    // Defining external function to
    // return concatenated string
    function getB() external payable returns(
      string memory){ 
        y = string(abi.encodePacked(x, c));
    } 
} 
 
// Defining child contract C
// inheriting parent contract A
contract C is B { 
      
    // Defining external function 
    // returning concatenated string
    // generated in child contract B
    function getC() external view returns(
      string memory){ 
        return y; 
    } 
} 
   
// Defining calling contract 
contract caller {
 
    // Creating object of child C
    C cc = new C(); 
 
    // Defining public function to
    // return final concatenated string 
    function testInheritance(
    ) public returns (
      string memory) { 
        cc.getA();
        cc.getB();
        return cc.getC(); 
    } 
}


Solidity
// Solidity program to demonstrate
// Hierarchical Inheritance
pragma solidity >=0.4.22 <0.6.0; 
 
// Defining parent contract A 
contract A { 
 
    // Declaring internal
    // state variable
    string internal x;
    
    // Defining external function
    // to set value of
    // internalstate variable
    function getA() external {
        x = "GeeksForGeeks";
    } 
     
    // Declaring internal
    // state variable
    uint internal sum; 
     
    // Defining external function
    // to set the value of
    // internal state variable sum 
    function setA() external { 
        uint a = 10;
        uint b = 20;
        sum = a + b;
    }
}
 
// Defining child contract B
// inheriting parent contract A
contract B is A { 
 
    // Defining external function to
    // return state variable x
    function getAstr(
    ) external view returns(string memory){ 
        return x; 
    } 
     
}
 
 
// Defining child contract C
// inheriting parent contract A
contract C is A { 
 
      // Defining external function to
      // return state variable sum
      function getAValue(
      ) external view returns(uint){ 
        return sum; 
    } 
}
 
// Defining calling contract   
contract caller {
 
    // Creating object of contract B      
    B contractB = new B(); 
 
    // Creating object of contract C
    C contractC = new C();  
     
    // Defining public function to
    // return values of state variables
    // x and sum
    function testInheritance(
    ) public returns (
      string memory, uint) { 
        return (
          contractB.getAstr(), contractC.getAValue()); 
    } 
}


Solidity
// Solidity program to
// demonstrate
// Multiple Inheritance
pragma solidity >=0.4.22 <0.6.0; 
 
// Defining contract A 
contract A {
 
    // Declaring internal
    // state variable
    string internal x;
 
    // Defining external function
    // to set value of
    // internal state variable x
    function setA() external {
        x = "GeeksForGeeks";
    }
}
 
// Defining contract B 
contract B { 
 
    // Declaring internal
   // state variable
    uint internal pow; 
 
    // Defining external function
    // to set value of internal
    // state variable pow
    function setB() external { 
        uint a = 2;
        uint b = 20;
        pow = a ** b;
         
    }
} 
 
// Defining child contract C
// inheriting parent contract
// A and B
contract C is A, B { 
 
  // Defining external function
  // to return state variable x
  function getStr(
  ) external returns(string memory) { 
        return x; 
    }
   
    // Defining external function
    // to return state variable pow
    function getPow(
    ) external returns(uint) { 
        return pow; 
    } 
} 
 
// Defining calling contract
contract caller { 
 
    // Creating object of contract C
    C contractC = new C(); 
   
    // Defining public function to
    // return values from functions
   // getStr and getPow
    function testInheritance(
    ) public returns(string memory, uint) { 
        contractC.setA(); 
        contractC.setB(); 
        return (
          contractC.getStr(), contractC.getPow()); 
    } 
}


输出 :

单一继承

2.多级继承

它与单继承非常相似,但不同之处在于它具有父子关系的层次。从父级派生的子合同也充当从它派生的合同的父级。

示例:在下面的示例中,合约 A合约 B 继承,合约 B合约 C 继承,以演示多级继承。

坚固性

// Solidity program to
// demonstrate Multi-Level
//  Inheritance
pragma solidity >=0.4.22 <0.6.0; 
 
// Defining parent contract A
contract A { 
 
     // Declaring state variables
     string internal x;
     string a = "Geeks" ;
     string b = "For";
 
    // Defining external function
    // to return concatenated string
    function getA() external{ 
        x = string(abi.encodePacked(a, b));
    } 
} 
 
// Defining child contract B
// inheriting parent contract A
contract B is A { 
 
      // Declaring state variables
      // of child contract B
      string public y;
      string c = "Geeks";
 
    // Defining external function to
    // return concatenated string
    function getB() external payable returns(
      string memory){ 
        y = string(abi.encodePacked(x, c));
    } 
} 
 
// Defining child contract C
// inheriting parent contract A
contract C is B { 
      
    // Defining external function 
    // returning concatenated string
    // generated in child contract B
    function getC() external view returns(
      string memory){ 
        return y; 
    } 
} 
   
// Defining calling contract 
contract caller {
 
    // Creating object of child C
    C cc = new C(); 
 
    // Defining public function to
    // return final concatenated string 
    function testInheritance(
    ) public returns (
      string memory) { 
        cc.getA();
        cc.getB();
        return cc.getC(); 
    } 
}

输出 :

多级继承示例

3.分层继承

在分层继承中,父合约有多个子合约。它主要用于在不同地方使用通用功能时。

示例:在下面的示例中,合约 A合约 B 继承,合约 A合约 C 继承,从而证明了分层继承。

坚固性

// Solidity program to demonstrate
// Hierarchical Inheritance
pragma solidity >=0.4.22 <0.6.0; 
 
// Defining parent contract A 
contract A { 
 
    // Declaring internal
    // state variable
    string internal x;
    
    // Defining external function
    // to set value of
    // internalstate variable
    function getA() external {
        x = "GeeksForGeeks";
    } 
     
    // Declaring internal
    // state variable
    uint internal sum; 
     
    // Defining external function
    // to set the value of
    // internal state variable sum 
    function setA() external { 
        uint a = 10;
        uint b = 20;
        sum = a + b;
    }
}
 
// Defining child contract B
// inheriting parent contract A
contract B is A { 
 
    // Defining external function to
    // return state variable x
    function getAstr(
    ) external view returns(string memory){ 
        return x; 
    } 
     
}
 
 
// Defining child contract C
// inheriting parent contract A
contract C is A { 
 
      // Defining external function to
      // return state variable sum
      function getAValue(
      ) external view returns(uint){ 
        return sum; 
    } 
}
 
// Defining calling contract   
contract caller {
 
    // Creating object of contract B      
    B contractB = new B(); 
 
    // Creating object of contract C
    C contractC = new C();  
     
    // Defining public function to
    // return values of state variables
    // x and sum
    function testInheritance(
    ) public returns (
      string memory, uint) { 
        return (
          contractB.getAstr(), contractC.getAValue()); 
    } 
}

输出 :

分层继承示例

4.多重继承

在多重继承中,可以从多个合约继承单个合约。父合同可以有多个孩子,而子合同可以有多个父母。

示例:在下面的示例中,合约 A合约 B 继承,合约 C继承合约 A和合约 B,从而证明了多重继承。

坚固性

// Solidity program to
// demonstrate
// Multiple Inheritance
pragma solidity >=0.4.22 <0.6.0; 
 
// Defining contract A 
contract A {
 
    // Declaring internal
    // state variable
    string internal x;
 
    // Defining external function
    // to set value of
    // internal state variable x
    function setA() external {
        x = "GeeksForGeeks";
    }
}
 
// Defining contract B 
contract B { 
 
    // Declaring internal
   // state variable
    uint internal pow; 
 
    // Defining external function
    // to set value of internal
    // state variable pow
    function setB() external { 
        uint a = 2;
        uint b = 20;
        pow = a ** b;
         
    }
} 
 
// Defining child contract C
// inheriting parent contract
// A and B
contract C is A, B { 
 
  // Defining external function
  // to return state variable x
  function getStr(
  ) external returns(string memory) { 
        return x; 
    }
   
    // Defining external function
    // to return state variable pow
    function getPow(
    ) external returns(uint) { 
        return pow; 
    } 
} 
 
// Defining calling contract
contract caller { 
 
    // Creating object of contract C
    C contractC = new C(); 
   
    // Defining public function to
    // return values from functions
   // getStr and getPow
    function testInheritance(
    ) public returns(string memory, uint) { 
        contractC.setA(); 
        contractC.setB(); 
        return (
          contractC.getStr(), contractC.getPow()); 
    } 
}

输出 :

多重继承