实体度中新变量的推理与合同类型



假设一个契约C继承了一个接口IC c = new C()I c = new C()之间有什么区别?在这两种情况下,c都指向新部署的C实例,不是吗?(我在看这里。(

更普遍地说,I c;C c;之间有什么区别?

假设契约实现了一些扩展接口的方法,那么您也可以使用C类型变量调用这些(扩展的(方法。

pragma solidity ^0.8;
interface I {
function foo() external;
}
contract C is I {
function foo() override external {
// implements the `foo()` function of the `I` interface
}
function otherFunction() external {
}
}
contract Factory {
function deploy() external {
// the `I` type variable `i` can only invoke the `foo()` function
I i = new C();
i.foo();
// the `C` type variable `c` can invoke the `otherFunction()` as well
C c = new C();
c.foo();
c.otherFunction();
}
}

如果契约和接口都定义了相同的方法集(例如,从示例中删除otherFunction()(,那么两个代码段(I i = new C();C c = new C();(实际上具有相同的功能。

最新更新