Hi Guest

More contents, please log on!

Bitmere.com 区块链技术 Content

View pure修饰符:
使用view与pure不需要gwei

view:不能修改链上数据只能访问

pure:不能访问链上数据,被修饰的方法里的变量只能在方法内使用

  1. contract ViewPureExample {
  2.     uint public age;
  3.     function increaseAge() public{
  4.       age ++;
  5. }

  6.     function GetViewAge() public view returns(uint){ //view 修饰
  7.         //age ++;// 不可修改变量
  8.         return age;
  9. }

  10. function GetPureAge(uint age) public pure returns(uint){ //因为pure方法 所以需要设置参数
  11.         age ++;
  12.         return age;//如果直接return会直接报错  因为pure修饰 所以不能访问方法外的变量
  13.     }
  14. }
Copy the Code




Public internal private external 方法修饰符:
public:任何地方都可以调用

internal:只有本sol文件调用

private:只有本合约能调用

external:只能从外部调用即合约外部



  1. contract PublicExternalPrivateInternal {
  2.     uint age = 100;
  3.     function GetAgeWithPublic() public view returns(uint){  //外部内部都可调用
  4.         return age;
  5.     }
  6.     function GetAgeWithInternal() internal view returns(uint){ // 本sol文件调用
  7.         return age;
  8.     }

  9.     function GetAgeWithPrivate() private view returns(uint){ //本合约调用
  10.         return ag;
  11.     }

  12. function GetAgeWithExternal() external view returns(uint){ //只能外部
  13. //调用
  14.         return age;
  15.     }

  16.     function Callfunction() public view returns(uint){
  17.         return GetAgeWithPrivate();//方法可以调用方法只要被调用的方法允许
  18.     }

  19. }
Copy the Code


可以自行编译后尝试调用查看输出



简单的合约调用:
  1. contract Salary {
  2.     uint public data;
  3.     function getData() external view returns(uint){
  4.         return data;
  5. }

  6.     function setData(uint _data) external {
  7.         data = _data;
  8.     }
  9. }
Copy the Code



  1. contract Employee {
  2.     Salary salart;
  3.     constructor(){
  4.         salary = new Salary();
  5.     }

  6.     function getSalary() external view returns(uint){
  7.         return salary.getData();
  8.     }

  9.     function setSalaty(uint _data) external {
  10.         salary.setData(_data);
  11.     }
  12. }
Copy the Code

BitMere.com is Information release platform,just provides information storage space services.
The opinions expressed are solely those of the author,Does not constitute advice, please treat with caution.
You have to log in before you can reply Login | 立即注册

Points Rules

Write the first review

李悔之2015 初中生
  • Follow

    1

  • Following

    0

  • Articles

    13

Promoted