Breaking down methods
A method is a behavior for an object, whereas properties are the attributes of an object. If I have a class called "Dog," I could insert a method called "Bark." There are four parts to the header of a method:
<Access Specifier> <Return Type> <Method Name>(<Parameters>)
Example: public void Bark()
The "Access Specifier" allows you to determine what has access to the method from outside of the class. This allows us to keep control of our data, and not have to worry about other classes affecting it without our knowledge. Here are the following levels of access:
public: Access is not restricted. Any class can use the method
protected: Access is limited to the containing class or types derived from the containing class.
internal: Access is limited to the current assembly.
protected internal: Access is limited to the current assembly or types derived from the containing class.
private: Access is limited to the containing type.
The "Return Type" is what data we receive from the method. If I pass in two number and want to multiply them, I may want to return the product. In this case, I would specify an "int," or maybe a "double" type.
"Method Name" is mostly self-explanatory, but it's recommended that you create a name that resembles an action. If I want to multiply two number, I may call the method "MultiplyNumbers."
"Parameters" is the data that you pass into the method. In the "MultiplyNumbers" example, this is where I would input two numbers into the method, so that I can multiply them. In the parameters section, you can input many parameters and data types. The key thing is to specify what type of data you're passing into it. Also, the parameters must have names unique to the method. Here is an example of this is the "MultiplyNumbers" method:
int numberOne = 5;
int numberTwo = 2;
int product = 0;
public int MultiplyNumbers(int numberOneMN, int numberTwoMN)
{
int productMN = 0;
productMN = numberOneMN * numberTwoMN;
return productMN;
)
product = MultiplyNumbers(numberOne, numberTwo);
In the method, I pass in "numberOne" and "numberTwo." Personally, I find it helpful to name the parameters the same as my variables, and then add the initials of the method name onto the end. Within the method, I multiply those two numbers together, and return the product. I then set my variable "product" to whatever is returned from the method "MultiplyThem."
In addition to this, you can also create multiple methods with the same name, but perform different functions. This is called "overloading." Let's say I want to also be able to multiply two "double" data types together. Well, I'd create the same exact method, but specify that I'm passing in "double" variables, instead of "int" variables. When I call the method and pass in two variable, it will execute one or the other, depending on what I'm passing in. Here is my second method, nearly identical, but with two different sets of parameters I can pass in:
double numberOne = 5;
double numberTwo = 2;
double product = 0;
public double MultiplyNumbers(double numberOneMN, double numberTwoMN)
{
double productMN = 0;
productMN = numberOneMN * numberTwoMN;
return productMN;
)
product = MultiplyNumbers(numberOne, numberTwo);
With this, if I pass in "int" variables, it will execute my first method. If I pass in "double" variables, it will instead execute my second method!
<Access Specifier> <Return Type> <Method Name>(<Parameters>)
Example: public void Bark()
The "Access Specifier" allows you to determine what has access to the method from outside of the class. This allows us to keep control of our data, and not have to worry about other classes affecting it without our knowledge. Here are the following levels of access:
public: Access is not restricted. Any class can use the method
protected: Access is limited to the containing class or types derived from the containing class.
internal: Access is limited to the current assembly.
protected internal: Access is limited to the current assembly or types derived from the containing class.
private: Access is limited to the containing type.
The "Return Type" is what data we receive from the method. If I pass in two number and want to multiply them, I may want to return the product. In this case, I would specify an "int," or maybe a "double" type.
"Method Name" is mostly self-explanatory, but it's recommended that you create a name that resembles an action. If I want to multiply two number, I may call the method "MultiplyNumbers."
"Parameters" is the data that you pass into the method. In the "MultiplyNumbers" example, this is where I would input two numbers into the method, so that I can multiply them. In the parameters section, you can input many parameters and data types. The key thing is to specify what type of data you're passing into it. Also, the parameters must have names unique to the method. Here is an example of this is the "MultiplyNumbers" method:
int numberOne = 5;
int numberTwo = 2;
int product = 0;
public int MultiplyNumbers(int numberOneMN, int numberTwoMN)
{
int productMN = 0;
productMN = numberOneMN * numberTwoMN;
return productMN;
)
product = MultiplyNumbers(numberOne, numberTwo);
In the method, I pass in "numberOne" and "numberTwo." Personally, I find it helpful to name the parameters the same as my variables, and then add the initials of the method name onto the end. Within the method, I multiply those two numbers together, and return the product. I then set my variable "product" to whatever is returned from the method "MultiplyThem."
In addition to this, you can also create multiple methods with the same name, but perform different functions. This is called "overloading." Let's say I want to also be able to multiply two "double" data types together. Well, I'd create the same exact method, but specify that I'm passing in "double" variables, instead of "int" variables. When I call the method and pass in two variable, it will execute one or the other, depending on what I'm passing in. Here is my second method, nearly identical, but with two different sets of parameters I can pass in:
double numberOne = 5;
double numberTwo = 2;
double product = 0;
public double MultiplyNumbers(double numberOneMN, double numberTwoMN)
{
double productMN = 0;
productMN = numberOneMN * numberTwoMN;
return productMN;
)
product = MultiplyNumbers(numberOne, numberTwo);
With this, if I pass in "int" variables, it will execute my first method. If I pass in "double" variables, it will instead execute my second method!
Comments
Post a Comment