sublime学习 - 利用snippet创建getter和setter

来源:互联网 发布:mac c4d怎么卸载 编辑:程序博客网 时间:2024/06/14 22:45

getter/setter

这是两个在OO设计中常见的两个方法,是为了让类的属性不直接暴露在外面的解决方法。

那么我们会有很多属性的时候,getter和setter的写也是很累的。
在IDE里,例如(Eclipse)是可以直接Source->getter and setter这样子创建的。

那么不是IDE怎么办?

Snippet

sublime的snippet非常好用,类似vim的map操作,所以我们在snippet的语法的时候,可以看我另外一篇转载的博客:snippet语法

语法整体是比较简单的。下面我们创建一个snippet来实现快速的创建getter/setter

代码

Tool->new snippet里把下面代码拷贝进去覆盖。保存成getset.sublime-snippet然后就可以尝试了。

<snippet>    <content><![CDATA[/** * Getter for ${1:$SELECTION} * @return */public function get${2:property}(){    return \$this->$1;}/** * Setter for $1 * @param $1 value to set * @return self */public function set${2:property}(\$$1){    \$this->$1 = \$${1/_(w)/U$1/g$1};    return \$this;}]]></content>    <!-- Optional: Set a tabTrigger to define how to trigger the snippet -->    <tabTrigger>getset</tabTrigger>    <!-- Optional: Set a scope to limit where the snippet will trigger -->    <scope>source.php</scope>    <!-- Optional: Description to show in the menu -->    <description>Create getter and setter methods</description></snippet>

上面的source.php意思是在什么类型的代码的时候才触发。假如是python就改成source.pyhon

使用方法

在要插入代码的地方输入geset然后点击一下tab键,就可以了,输入类属性名字,然后再点击一下tab就可以输入get和set的名字了。

以上。

0 0
原创粉丝点击