FuzzDuck
FuzzDuck
Class
FuzzDuck
Class

Class

Like a blueprint is used for mass production, a class is used for production of objects.

In a quick comparison, classes are like blueprints. When you want to create something you usually have a plan or schematics showing the different components of the creation. Inside the classes you define the construction of the class itself, the accessibility to the class infrastructure, and the behavior of the class. You can create different parameters, which makes the class a container of different information. Through methods, you can alter these informations, make calculations, interact with other classes or whatever your fantasy can come up with.

Code Example - Declaring empty class
// --- Declares an empty class --- //
public class Duck {

}
// --- Declares an empty class --- //
public class Duck {

}
// --- Declares an empty class --- //
class Duck {

}

Methods / Functions

Sometimes we need an object to be able to make different behaviors. Theese behaviors are called methods in C#. When a class' methods are called upon the code inside is executed. The method can be declared with parameters, which must be entered into the method. The method itself can have its type set to return values like string, int and boolean or to return nothing indicated by the word "void".

Code Example - Declaring class with variables and methods / functions
// --- Declares the beginning of the class --- //
public class Duck {

 // --- Declares the variables inside the class --- //
 public int age = 5;
 public String name = "Ducky";

 // --- Declares the fly method --- //
 public void fly() {
  Console.WriteLine("Duck flies");
 }

 // --- Declares the walk method --- //
 public int walk() {
  Console.WriteLine("Duck walks 5 steps");
  return 5;
 }

 // --- Declares the makeSound method --- //
 public String makeSound() {
  Console.WriteLine("Duck says Quack");
  return "Quack";
 }

}
// --- Declares the beginning of the class --- //
public class Duck {

 // --- Declares the variables inside the class --- //
 public int age = 5;
 public String name = "Ducky";

 // --- Declares the fly method --- //
 public void fly() {
  System.out.println("Duck flies");
 }

 // --- Declares the walk method --- //
 public int walk() {
  System.out.println("Duck walks 5 steps");
  return 5;
 }

 // --- Declares the makeSound method --- //
 public String makeSound() {
  System.out.println("Duck says Quack");
  return "Quack";
 }

}
// --- Declares the beginning of the class --- //
class Duck {

 // --- Declares the variables inside the class --- //
 public $age = 5;
 public $name = "Ducky";

 // --- Declares the fly function --- //
 public function fly() {
  echo "Duck flies";
 }

 // --- Declares the walk function --- //
 public function walk() {
  echo "Duck walks 5 steps";
  return 5;
 }

 // --- Declares the makeSound function --- //
 public function makeSound() {
  echo "Duck says Quack";
  return "Quack";
 }

}
Code Example - Instantiating the class above, retrieving variables and calling methods / functions
// --- Declaring starting point of the program run with "main" method --- //
public static void Main(string[] args){

 // --- Instantiating a Duck instance called 'ducky' --- //
 Duck ducky = new Duck();

 // --- Printing ducky's age --- //
 Console.WriteLine(ducky.age);

 // --- Printing ducky's name --- //
 Console.WriteLine(ducky.name);

 // --- Calling ducky's method 'fly()' --- //
 ducky.fly();

 // --- Calling ducky's method 'walk()' --- //
 ducky.walk();

 // --- Calling ducky's method 'makeSound()' --- //
 ducky.makeSound();

 // --- Invoking 'ReadKey()' to make time to read the output --- //
 Console.ReadKey();

}
// --- Declaring starting point of the program run with "main" method --- //
public static void main(String [ ] args){

 // --- Instantiating a Duck instance called 'ducky' --- //
 Duck ducky = new Duck();

 // --- Printing out ducky's age --- //
 System.out.println(ducky.age);

 // --- Printing out ducky's name --- //
 System.out.println(ducky.name);

 // --- Calling ducky's method 'fly()'. Which in the method body prints 'Duck flies' --- //
 ducky.fly();

 // --- Calling ducky's method 'walk()' printing out the returned int --- //
 ducky.walk();

 // --- Calling ducky's method 'makeSound()' printing out the returned String --- //
 ducky.makeSound();

}
// --- Instantiating a Duck instance called 'ducky' --- //
$ducky = new Duck();

// --- Retrieve ducky's age --- //
echo $ducky->age;

// --- Retrieve ducky's name --- //
echo $ducky->name;

// --- Calling ducky's method 'fly()' --- //
$ducky->fly();

// --- Calling ducky's method 'walk()' --- //
$ducky->walk();

// --- Calling ducky's method 'makeSound()' --- //
$ducky->makeSound();