在什么条件下,我们应该使用它们,最好使用哪种方法?
在以下两种方法中,
在我们应该分别使用两种方法或应分别使用这两种方法的情况下?
这两种方法之间有什么区别?
最好使用哪种方法?
说我们有2个合同SUU和FOO
suu合同:
contract suu{
uint public value;
function sums( uint a,uint b) public view returns(uint){
uint result=a+b;
return result;
}
foo合同:
contract foo {
// Empty Contract
//Yet to write a function in this contract
}
方法-1 < /strong>
contract foo is suu{
function mul(uint a , uint b ) public view returns(uint){
uint res=sums(a,b) // sums function Inherited from Suu contract;
return res*10;
}
方法-2
import "./Suu.sol"
contract foo {
Suu suuContract = Suu("CONTRACT_ADDRESS_OF_SUU")
function mul(uint a , uint b ) public view returns(uint){
uint res=suuContract.sums(a,b); //sums function from suu Contract
return res*10;
}
在上述2种方法中,它给出了相同的答案,
- 哪种方法最好使用哪种方法?
- 在我们应该分别使用这两种方法下
In the following 2 methods,
Under what situation should we use the 2 methods respectively or when should we use these 2 methods respectively?
What is the difference between these two methods?
Which method is best to use?
Lets say we have 2 contracts suu and foo
Suu Contract:
contract suu{
uint public value;
function sums( uint a,uint b) public view returns(uint){
uint result=a+b;
return result;
}
Foo Contract:
contract foo {
// Empty Contract
//Yet to write a function in this contract
}
Method - 1
contract foo is suu{
function mul(uint a , uint b ) public view returns(uint){
uint res=sums(a,b) // sums function Inherited from Suu contract;
return res*10;
}
Method - 2
import "./Suu.sol"
contract foo {
Suu suuContract = Suu("CONTRACT_ADDRESS_OF_SUU")
function mul(uint a , uint b ) public view returns(uint){
uint res=suuContract.sums(a,b); //sums function from suu Contract
return res*10;
}
In the above 2 methods, it gives the same answer,
- which method is best to use
- Under what conditions should we use these 2 methods respectively?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您使用继承的第一个方法中,您是在告诉FOO像Suu一样行事,因此SUU的方法和存储变量将属于Foo,就像您在Foo中写的那样。
您使用此方法将代码分为块,最好的做法是不要编写很长的智能合约以使安全审核更简单。
在第二种方法中,您正在调用已经部署的合同(SUU)并使用其功能之一。在这种情况下,函数设置为查看,但是可以编写状态更改函数并更改SUU状态。您基本上是在调用Suu,就像您直接从您的帐户中拨打任何其他合同一样。
有时,您需要调用已部署的合同方法(例如在方法2中)(例如,要制作Flashloan),而不是导入整个合同,而只需导入接口,这是一个包括该方法签名的抽象合同。
要注意的事情:
而不是公开。公众意味着该方法可以从一个
EOA或智能合约。
没有从存储中读取。
In the first method you're using inheritance, you are then telling foo to behave like suu so methods and storage variables from suu will belong to foo as you have written them in foo.
You use this method to divide your code into chunks and it is best practice to not write very long smart contracts to make security auditing much simpler.
In the second method you are calling an already deployed contract (suu) and using one of it's functions. In that case the functions is set to view, but it is possible to write a state changing function and change the state of suu. You are basically calling suu just like you call any other contract directly from your account.
Sometimes you need to call a deployed contract's method (like in Method 2) ( to make a flashloan for example ), instead of importing the whole contract, you just import the interface, which is an abstract contract that includes the method's signature.
Things to note:
instead of public. Public means the the method can be called from an
EOA or a smart contract.
doesn't read from storage.