美文网首页
solidity-4.可见性

solidity-4.可见性

作者: jection | 来源:发表于2018-11-19 16:26 被阅读0次

可见性或权限控制(Visibility And Accessors)

可见性

  • external 外部可见性
    可以被其它合约或发起交易的方式调用
    在合约内部调用要使用this.f()
  • public 公开可见性
    public类型的状态变量,会自动创建一个访问器
  • internal 内部可见性
  • private 私有可见性

| | 合约内部 | 继承合约 | 外部合约 | 发起交易
| ------------ | ------------ | ------------ | ------------
| external | Y | Y | Y | Y
| public | Y | Y | Y | N
| internal | Y | Y | N | N
| private | Y | N | N | N

函数默认是public
状态变量默认的可见性是internal

访问函数(Getter Functions)

public的状态变量,编译器会默认创建访问函数。

contract C{
    uint public data = 10;
}

变量data为public,所以创建访问函数如下:

function data() view return (uint data)

外部可以使用C.data()访问它。

注意:在外部函数中的状态变量访问,只能通过this.data()的方式。

比如:

contract C{
    uint public c = 10;
    function accessInternal() internal returns (uint){
        return c;
    }
    
    function accessExternal() external returns (uint){
        return this.c();
    }
}

注意:public的mapping默认访问参数是需要参数的,并不是之前说的访问函数都是无参的。
如下:

mapping (address => uint256) public balanceOf;
function balanceOf(address _owner) view returns (uint256 balance)

函数修改器(Function Modifiers)

http://www.tryblockchain.org/Solidity-FunctionModifiers-%E5%87%BD%E6%95%B0%E4%BF%AE%E6%94%B9%E5%99%A8.html
修改器(Modifiers)可以用来轻易的改变一个函数的行为。比如用于在函数执行前检查某种前置条件。

  • 修改器(Modifiers)可以被继承
  • 修改器(Modifiers)可以被重写(override)
  • 修改器(Modifiers)可以接收参数的
  • _表示修改器(Modifiers)修饰的函数体替换位置
pragma solidity ^0.4.0;

contract owned {
    function owned() { owner = msg.sender; }
    address owner;

    // This contract only defines a modifier but does not use
    // it - it will be used in derived contracts.
    // The function body is inserted where the special symbol
    // "_;" in the definition of a modifier appears.
    // This means that if the owner calls this function, the
    // function is executed and otherwise, an exception is
    // thrown.
    modifier onlyOwner {
        if (msg.sender != owner)
            throw;
        _;
    }
}


contract mortal is owned {
    // This contract inherits the "onlyOwner"-modifier from
    // "owned" and applies it to the "close"-function, which
    // causes that calls to "close" only have an effect if
    // they are made by the stored owner.
    function close() onlyOwner {
        selfdestruct(owner);
    }
}


contract priced {
    // Modifiers can receive arguments:
    modifier costs(uint price) {
        if (msg.value >= price) {
            _;
        }
    }
}


contract Register is priced, owned {
    mapping (address => bool) registeredAddresses;
    uint price;

    function Register(uint initialPrice) { price = initialPrice; }

    // It is important to also provide the
    // "payable" keyword here, otherwise the function will
    // automatically reject all Ether sent to it.
    function register() payable costs(price) {
        registeredAddresses[msg.sender] = true;
    }

    function changePrice(uint _price) onlyOwner {
        price = _price;
    }
}

例子:使用修改器实现的一个防重复进入锁

pragma solidity ^0.4.0;
contract Mutex {
    bool locked;
    modifier noReentrancy() {
        if (locked) throw;
        locked = true;
        _;
        locked = false;
    }

    /// This function is protected by a mutex, which means that
    /// reentrant calls from within msg.sender.call cannot call f again.
    /// The `return 7` statement assigns 7 to the return value but still
    /// executes the statement `locked = false` in the modifier.
    function f() noReentrancy returns (uint) {
        if (!msg.sender.call()) throw;
        return 7;
    }
}

相关文章

  • solidity-4.可见性

    可见性或权限控制(Visibility And Accessors) 可见性 external 外部可见性可以被其...

  • 见性见性见性

    人活着活着就迷失了忘记了自己是谁迷失了本性,活着活着想死了。 其实是没有见性,人如何见性,见什么性得通过人生经历来...

  • 多线程 | Volatile到底有什么用?

    Volatile的作用: 保持内存可见性.内存可见性:多个线程操作同一个变量,可确保写线程更新变量,其他读线程可以...

  • 美好生活排行榜|酒桌即中国

    酒精不燃烧,不算搞社交。 喝酒可大俗可大雅,可论国是可谈风月,可攀交情可见性情,可怡情可乱性,可养生可伤身,可豪饮...

  • 并发编程-synchronized关键字大总结

    0、synchronized 的特点: 可以保证代码的原子性和可见性。 1、synchronized 的性质: 可...

  • 第三章——对象的共享

    3.1 可见性 3.1.3 加锁与可见性 内置锁可以用于确保某个线程以一种可预测的方法来查看另一个线程的执行结果。...

  • 《自我边界》之可预见性

    对某些人来说,“边界”让他们明确了路标,而对另一些人而言,“边界”让他们在之前提到的树上爬得更高,对自己和周围的世...

  • 02|Java内存模型

    可见性、原子性、有序性是并发问题的三个关键因素。 可见性:一个线程对共享变量的修改,另外一个线程能够立刻看到。 可...

  • 何为明心见性?如何塑梦圆梦?

    何为明心见性?如何塑梦圆梦? ----明心见性,自性具足;净心圆梦,觉悟人民。 何为明心见性?如何明心见性? 明心...

  • 【Java进阶】并发编程

    . 概述 三种性质 可见性:一个线程对共享变量的修改,另一个线程能立刻看到。缓存可导致可见性问题。 原子性:一个或...

网友评论

      本文标题:solidity-4.可见性

      本文链接:https://www.haomeiwen.com/subject/pgsxqqtx.html