If you are hosting a party in your house, how many people can access your house? Only the ones you invite, and how many people will be allowed to go to the second floor? Only you and your family!
The same thing exist in development world, some times we just don’t want others to access our methods, so we protect them with an Access Modifier.
It exist 3 type of access modifiers:
Public: The type or member can be accessed by any other code in the same assembly or another assembly that references it.
Protected: The type or member can be accessed only by code in the same class or struct, or in a class that is derived from that class.
Private: The type or member can be accessed only by code in the same class or struct.
Internal : The type or member can be accessed by any code in the same assembly, but not from another assembly.
Let’s see how we implements them:
// public class:
public class Car {
// protected method:
protected void Run(){ }
// private field: private
int wheels = 4;
// protected internal property:
protected internal int Wheels { get { return wheels; } } }
No, any other type can create an instant of our Car, but only types deriving from the Car or the Car it self can start the engine. Our Wheels variable is private, no one can access it except the Car itself so we prevent the others from modifying it’s value, for example any one can change it to 10 or 20 and it won’t be a valid car! But what if we want to see how many wheels does our classes? We simply create a Property field that is read-only.
Properties are pretty easy to use, you just declare like a variable but you define a Get and Set methods for it (or just one, that depends on the scenario) , the getter will return the value of the variable we want, and the setter will set it’s value!
private int energy=100;
public int Energy {get{return energy;}
set{if(value=<100){energy=value}}};
Now we have an energy field, that can’t be more than 100, what we did, we wrote a property that will check if the value is less or equal to 100 it will change the energy variable, otherwise nothing happens. (we used an If clause, we will explain it in an other tutorial).
The same thing exist in development world, some times we just don’t want others to access our methods, so we protect them with an Access Modifier.
It exist 3 type of access modifiers:
Public: The type or member can be accessed by any other code in the same assembly or another assembly that references it.
Protected: The type or member can be accessed only by code in the same class or struct, or in a class that is derived from that class.
Private: The type or member can be accessed only by code in the same class or struct.
Internal : The type or member can be accessed by any code in the same assembly, but not from another assembly.
Let’s see how we implements them:
// public class:
public class Car {
// protected method:
protected void Run(){ }
// private field: private
int wheels = 4;
// protected internal property:
protected internal int Wheels { get { return wheels; } } }
No, any other type can create an instant of our Car, but only types deriving from the Car or the Car it self can start the engine. Our Wheels variable is private, no one can access it except the Car itself so we prevent the others from modifying it’s value, for example any one can change it to 10 or 20 and it won’t be a valid car! But what if we want to see how many wheels does our classes? We simply create a Property field that is read-only.
Properties are pretty easy to use, you just declare like a variable but you define a Get and Set methods for it (or just one, that depends on the scenario) , the getter will return the value of the variable we want, and the setter will set it’s value!
private int energy=100;
public int Energy {get{return energy;}
set{if(value=<100){energy=value}}};
Now we have an energy field, that can’t be more than 100, what we did, we wrote a property that will check if the value is less or equal to 100 it will change the energy variable, otherwise nothing happens. (we used an If clause, we will explain it in an other tutorial).
No comments:
Post a Comment