I’m beginner to C# and wrote this code and want know this code is a Encapsulation?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControl : MonoBehaviour
{
Player player;
float speed = 5f;
// Use this for initialization
void Start ()
{
player = new Player ();
}
// Update is called once per frame
void Update ()
{
player.x = transform.position.x;
player.y = transform.position.y;
PlayerMovement ();
transform.position = new Vector2 (player.x, player.y);
}
//================================================================= Functions =================================================================
void PlayerMovement()
{
// Move to horizontal
if (Input.GetKey (KeyCode.RightArrow))
{
player.x += speed * Time.deltaTime;
}
else if (Input.GetKey (KeyCode.LeftArrow))
{
player.x -= speed * Time.deltaTime;
}
// move to vertical
if (Input.GetKey(KeyCode.UpArrow))
{
player.y += speed * Time.deltaTime;
}
else if (Input.GetKey(KeyCode.DownArrow))
{
player.y -= speed * Time.deltaTime;
}
}
}
public class Player
{
public float x;
public float y;
}
I made a new class and gave it the name of the Player. In the Visual Basic language, they say that User Data Type.
now i need know this code is a Encapsulation or it has another name.
Encapsulation refers to hiding a class member behind accessor methods or properties. This is usually done so that you can perform some logic when the member is set. For example:
// Not encapsulated
public class SomeClass {
public float aValue;
}
//Encapsulated
public class SomeClass {
public float aValue {
get {
return _aValue;
}
set {
_aValue = Mathf.Clamp(value, 0f, 1f);
}
}
private float _aValue = 0f;
}
In this example, using encapsulation allows you to guarantee that aValue will always be between 0 and 1.
Just to clarify, encapsulation is hiding anything. Access modifiers for class members are the most common form of encapsulation. For instance, a Facade can hide an entire design.
exempli gratia…
public class PublicThingy {
private PrivateThingy1 thingy1;
private PrivateThingy2 thingy2;
public PublicThingy() {
thingy1 = new PrivateThingy1();
thingy2 = new PrivateThingy2();
}
public void DoSomething() {
thingy1.Connect();
thingy2.SaveData(thingy1);
thingy1.Disconnect();
}
}
In the above code, PublicThingy hides not only that it has references to PrivateThingy1 and PrivateThingy2 via fields of the same name but also the following:
The fact that PrivateThingy1 and PrivateThingy2 are part of solving the problem addressed by DoSomething().
The algorithm required in order to fill the need addressed by DoSomething().
As far as clients to PublicThingy are concerned, it even hides the fact that PrivateThingy1 and PrivateThingy2 exist at all, along with the relationship between the two of them.
That last one can be hard to enforce, if enforcement is a problem (like if you are on a large team and not everyone agrees that encapsulation is valuable). However, encapsulation of design can be a very powerful tool to prevent ripple effects.