我之前已经问过这个问题并且因为我无法提供它所写的语言而关闭了。目前我正在查看这些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));
}
}
}
答案 0 :(得分:2)
我这样做有两个原因:
private
。以下是一个例子:
private var _thing:String = "hello";
public function get thing():String
{
return _thing;
}
正如您所看到的,_thing
是只读的,可通过更好的class.thing
访问 - 它也更易读,即您可以轻松查看哪些变量与哪些getter和setter相关。
回答你问题的后半部分;不,你没有需要在私有财产/方法的前面添加下划线,虽然我建议你养成这个习惯 - 你会感谢你自己在轨道上,合作者也是如此任何
答案 1 :(得分:1)
这是一种命名约定,通常表示私有类级变量。它应该通过允许您确定变量的范围来使代码更容易阅读。
答案 2 :(得分:1)
考虑:
..
public class SomeClass{
private var _param1:uint;
private var _param2:string;
private var _param3:displayObject
protected var __param4:int;
public function SomeClass(param1:uint, param2:string, param3:displayObject):void{
super();
_param1 = param1;
_param2 = param2;
_param3 = param3;
__param4 = 123;
//etc...
不添加任何新内容,只是说明了在使用下划线私有时可以开发的非常有用的语义关系,尤其是与从外部源派生的方法参数相关。
另请注意,有时会为__
变量使用双下划线protected
。
答案 3 :(得分:0)
它的目的是使它们成为实例变量的方法更清晰。
在某些环境中,C ++中的约定是为了相同的原因添加前缀“m”或“m_”(对于“member”)。