带下划线前缀的变量
我之前问过这个问题,但它被关闭了,因为我无法提供它所用的语言。目前我正在查看这些 AS3 代码,并且变量以 _ 为前缀,我可以知道为什么吗?是因为约定吗?如果是,为什么要加_?为什么你甚至需要放一个_?
/**
* Enemy AI - Random movement
* ---------------------
* VERSION: 1.0
* DATE: 1/25/2011
* AS3
* UPDATES AND DOCUMENTATION AT: http://www.FreeActionScript.com
**/
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
public class Main extends MovieClip
{
// player settings
private var _moveSpeedMax:Number = 1000;
private var _rotateSpeedMax:Number = 15;
private var _decay:Number = .98;
private var _destinationX:int = 150;
private var _destinationY:int = 150;
private var _minX:Number = 0;
private var _minY:Number = 0;
private var _maxX:Number = 550;
private var _maxY:Number = 400;
// player
private var _player:MovieClip;
// global
private var _dx:Number = 0;
private var _dy:Number = 0;
private var _vx:Number = 0;
private var _vy:Number = 0;
private var _trueRotation:Number = 0;
/**
* Constructor
*/
public function Main()
{
// create player object
createPlayer();
// add listeners
stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
/**
* Creates player
*/
private function createPlayer():void
{
_player = new Player();
_player.x = stage.stageWidth / 2;
_player.y = stage.stageHeight / 2;
stage.addChild(_player);
}
/**
* EnterFrame Handlers
*/
private function enterFrameHandler(event:Event):void
{
updateCollision();
updatePosition();
updateRotation();
}
/**
* Calculate Rotation
*/
private function updateRotation():void
{
// calculate rotation
_dx = _player.x - _destinationX;
_dy = _player.y - _destinationY;
// which way to rotate
var rotateTo:Number = getDegrees(getRadians(_dx, _dy));
// keep rotation positive, between 0 and 360 degrees
if (rotateTo > _player.rotation + 180) rotateTo -= 360;
if (rotateTo < _player.rotation - 180) rotateTo += 360;
// ease rotation
_trueRotation = (rotateTo - _player.rotation) / _rotateSpeedMax;
// update rotation
_player.rotation += _trueRotation;
}
/**
* Calculate Position
*/
private function updatePosition():void
{
// update velocity
_vx += (_destinationX - _player.x) / _moveSpeedMax;
_vy += (_destinationY - _player.y) / _moveSpeedMax;
// if close to target
if (getDistance(_dx, _dy) < 50)
{
getRandomDestination();
}
// apply decay (drag)
_vx *= _decay;
_vy *= _decay;
// update position
_player.x += _vx;
_player.y += _vy;
}
/**
* updateCollision
*/
protected function updateCollision():void
{
// Check X
// Check if hit top
if (((_player.x - _player.width / 2) < _minX) && (_vx < 0))
{
_vx = -_vx;
}
// Check if hit bottom
if ((_player.x + _player.width / 2) > _maxX && (_vx > 0))
{
_vx = -_vx;
}
// Check Y
// Check if hit left side
if (((_player.y - _player.height / 2) < _minY) && (_vy < 0))
{
_vy = -_vy
}
// Check if hit right side
if (((_player.y + _player.height / 2) > _maxY) && (_vy > 0))
{
_vy = -_vy;
}
}
/**
* Calculates a random destination based on stage size
*/
private function getRandomDestination():void
{
_destinationX = Math.random() * (_maxX - _player.width) + _player.width / 2;
_destinationY = Math.random() * (_maxY - _player.height) + _player.height / 2;
}
/**
* Get distance
* @param delta_x
* @param delta_y
* @return
*/
public function getDistance(delta_x:Number, delta_y:Number):Number
{
return Math.sqrt((delta_x*delta_x)+(delta_y*delta_y));
}
/**
* Get radians
* @param delta_x
* @param delta_y
* @return
*/
public function getRadians(delta_x:Number, delta_y:Number):Number
{
var r:Number = Math.atan2(delta_y, delta_x);
if (delta_y < 0)
{
r += (2 * Math.PI);
}
return r;
}
/**
* Get degrees
* @param radians
* @return
*/
public function getDegrees(radians:Number):Number
{
return Math.floor(radians/(Math.PI/180));
}
}
}
i have asked this question before and it was closed because i was unable to provide the language it was written in. currently i am looking at these AS3 codes and the variables are prefixed with _ , may i know why? is it because of a convention, if so , why put a _? why do you even need to put a _?
/**
* Enemy AI - Random movement
* ---------------------
* VERSION: 1.0
* DATE: 1/25/2011
* AS3
* UPDATES AND DOCUMENTATION AT: http://www.FreeActionScript.com
**/
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
public class Main extends MovieClip
{
// player settings
private var _moveSpeedMax:Number = 1000;
private var _rotateSpeedMax:Number = 15;
private var _decay:Number = .98;
private var _destinationX:int = 150;
private var _destinationY:int = 150;
private var _minX:Number = 0;
private var _minY:Number = 0;
private var _maxX:Number = 550;
private var _maxY:Number = 400;
// player
private var _player:MovieClip;
// global
private var _dx:Number = 0;
private var _dy:Number = 0;
private var _vx:Number = 0;
private var _vy:Number = 0;
private var _trueRotation:Number = 0;
/**
* Constructor
*/
public function Main()
{
// create player object
createPlayer();
// add listeners
stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
/**
* Creates player
*/
private function createPlayer():void
{
_player = new Player();
_player.x = stage.stageWidth / 2;
_player.y = stage.stageHeight / 2;
stage.addChild(_player);
}
/**
* EnterFrame Handlers
*/
private function enterFrameHandler(event:Event):void
{
updateCollision();
updatePosition();
updateRotation();
}
/**
* Calculate Rotation
*/
private function updateRotation():void
{
// calculate rotation
_dx = _player.x - _destinationX;
_dy = _player.y - _destinationY;
// which way to rotate
var rotateTo:Number = getDegrees(getRadians(_dx, _dy));
// keep rotation positive, between 0 and 360 degrees
if (rotateTo > _player.rotation + 180) rotateTo -= 360;
if (rotateTo < _player.rotation - 180) rotateTo += 360;
// ease rotation
_trueRotation = (rotateTo - _player.rotation) / _rotateSpeedMax;
// update rotation
_player.rotation += _trueRotation;
}
/**
* Calculate Position
*/
private function updatePosition():void
{
// update velocity
_vx += (_destinationX - _player.x) / _moveSpeedMax;
_vy += (_destinationY - _player.y) / _moveSpeedMax;
// if close to target
if (getDistance(_dx, _dy) < 50)
{
getRandomDestination();
}
// apply decay (drag)
_vx *= _decay;
_vy *= _decay;
// update position
_player.x += _vx;
_player.y += _vy;
}
/**
* updateCollision
*/
protected function updateCollision():void
{
// Check X
// Check if hit top
if (((_player.x - _player.width / 2) < _minX) && (_vx < 0))
{
_vx = -_vx;
}
// Check if hit bottom
if ((_player.x + _player.width / 2) > _maxX && (_vx > 0))
{
_vx = -_vx;
}
// Check Y
// Check if hit left side
if (((_player.y - _player.height / 2) < _minY) && (_vy < 0))
{
_vy = -_vy
}
// Check if hit right side
if (((_player.y + _player.height / 2) > _maxY) && (_vy > 0))
{
_vy = -_vy;
}
}
/**
* Calculates a random destination based on stage size
*/
private function getRandomDestination():void
{
_destinationX = Math.random() * (_maxX - _player.width) + _player.width / 2;
_destinationY = Math.random() * (_maxY - _player.height) + _player.height / 2;
}
/**
* Get distance
* @param delta_x
* @param delta_y
* @return
*/
public function getDistance(delta_x:Number, delta_y:Number):Number
{
return Math.sqrt((delta_x*delta_x)+(delta_y*delta_y));
}
/**
* Get radians
* @param delta_x
* @param delta_y
* @return
*/
public function getRadians(delta_x:Number, delta_y:Number):Number
{
var r:Number = Math.atan2(delta_y, delta_x);
if (delta_y < 0)
{
r += (2 * Math.PI);
}
return r;
}
/**
* Get degrees
* @param radians
* @return
*/
public function getDegrees(radians:Number):Number
{
return Math.floor(radians/(Math.PI/180));
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我这样做的两个原因:
私有
。这是一个示例:
如您所见,
_thing
是只读的,可以通过更好的class.thing
进行访问 - 它也更具可读性,即您可以轻松查看哪个变量与 getter 和 setter 相关。回答你问题的后半部分;不,你不需要需要在私有属性/方法的前面添加下划线,尽管我确实建议养成这种习惯 - 你会为此感谢自己,合作者也会感谢如果任何。
Two reasons that I do this:
private
.Here's an example:
As you can see,
_thing
is read-only and is accessed via the much nicerclass.thing
- it's also much more readable i.e. you can easily see which variables are related to which getters and setters.To answer the latter part of your question; no you do not need to add the underscore at the front of a private property/method, though I do recommend making this a habit - you'll thank yourself for it down the track, as will collaborators if any.
这是一种命名约定,通常表示私有类级别变量。它允许您确定变量的范围,从而使代码更易于阅读。
It's a naming convention that typically denotes a private class level variable. It's supposed to make the code easier to read by allowing you to be able to determine the scope of the variable.
考虑:
不添加任何新内容,只是说明使用下划线私有时可以开发的非常有用的语义关系,特别是与从外部源派生的方法参数相关的语义关系。
另请注意,有时双下划线
__
用于受保护的变量。Consider:
Not adding anything new, just illustrating the very useful semantic relationship that could be developed when using underscore privates, especially as related to method parameters derived from external sources.
Note also that sometimes a double underscore
__
is used forprotected
variables.其目的是在方法中更清楚地表明它们是实例变量。
在某些环境中,出于同样的原因,C++ 中的约定是添加前缀“m”或“m_”(表示“成员”)。
It's purpose is to make it clearer in methods that they are instance variables.
In some environments, the convention in C++ is to add the prefix "m" or "m_" (for "member") for the same reason.