692 lines
11 KiB
Markdown
692 lines
11 KiB
Markdown
# OOP
|
|
|
|
# classes
|
|
|
|
In another file
|
|
```C#
|
|
class Messages()
|
|
{
|
|
public static void Hello()
|
|
{
|
|
Console.WriteLine("Hello! Welcome to the program");
|
|
}
|
|
public static void Waiting()
|
|
{
|
|
Console.WriteLine("I am waiting for somtething");
|
|
}
|
|
public static void Bye()
|
|
{
|
|
Console.WriteLine("Bye! Thanks for visiting");
|
|
}
|
|
}
|
|
```
|
|
|
|
```C#
|
|
Messages.Hello();
|
|
```
|
|
|
|
## Object
|
|
|
|
Object is an instance of a class
|
|
can have fields & nethods
|
|
|
|
```C#
|
|
class Human
|
|
{
|
|
String name;
|
|
int age;
|
|
|
|
public void Eat()
|
|
{
|
|
Console.WriteLine(name + " is eating");
|
|
}
|
|
public void Sleep()
|
|
{
|
|
Console.WriteLine(name + " is sleeping");
|
|
}
|
|
}
|
|
```
|
|
|
|
```C#
|
|
// in the main class
|
|
Human human1 = new Human()
|
|
|
|
human1.name = "Rick";
|
|
human1.age = 65;
|
|
|
|
human1.Eat();
|
|
human1.Sleep();
|
|
|
|
Human human2 = new Human()
|
|
|
|
human1.name = "Morty";
|
|
human1.age = 16;
|
|
|
|
human1.Eat();
|
|
human1.Sleep();
|
|
```
|
|
|
|
## Construtor
|
|
|
|
Constructor is a special method in a class
|
|
|
|
```C#
|
|
class Human
|
|
{
|
|
String name;
|
|
int age;
|
|
|
|
public Human(String name, int age){
|
|
this.name = name;
|
|
this.age = age;
|
|
}
|
|
|
|
public void Eat()
|
|
{
|
|
Console.WriteLine(name + " is eating");
|
|
}
|
|
public void Sleep()
|
|
{
|
|
Console.WriteLine(name + " is sleeping");
|
|
}
|
|
}
|
|
|
|
```
|
|
|
|
```C#
|
|
// in the main class
|
|
|
|
Human human1 = new Human("Rick",65)
|
|
Human human2 = new Human("Morty", 16)
|
|
|
|
human1.Eat();
|
|
human1.Sleep();
|
|
human2.Eat();
|
|
human2.Sleep();
|
|
```
|
|
|
|
## static
|
|
|
|
static is a modifier to declare a static member, which belongs to the class itself
|
|
rather than to any specific object
|
|
|
|
```C#
|
|
class Car
|
|
{
|
|
String model;
|
|
public static int numberOfCars;
|
|
|
|
public Car(String model)
|
|
{
|
|
this.model = model;
|
|
numberOfCars++;
|
|
}
|
|
public static void StartRace()
|
|
{
|
|
Console.WriteLine("The race as begun !")
|
|
}
|
|
}
|
|
```
|
|
|
|
```C#
|
|
// in the main class
|
|
|
|
Car car1 = new Car("Mustang");
|
|
Car car2 = new Car("Corvette");
|
|
Console.WriteLine(Car.numberOfCars);
|
|
|
|
Car.StartRace();
|
|
```
|
|
|
|
## overladed constructo
|
|
|
|
Technique to create multiple constructors, with a different set of parameters but the same name.
|
|
name + parameters = signature
|
|
|
|
```C#
|
|
class Pizza
|
|
{
|
|
String bread;
|
|
String sauce;
|
|
String cheese;
|
|
String topping;
|
|
|
|
public Pizza(String bread,String sauce, String cheese, String topping)
|
|
{
|
|
this.bread = bread;
|
|
this.sauce = sauce;
|
|
this.cheese = cheese;
|
|
this.topping = topping;
|
|
}
|
|
|
|
public Pizza(String bread,String sauce, String cheese)
|
|
{
|
|
this.bread = bread;
|
|
this.sauce = sauce;
|
|
this.cheese = cheese;
|
|
}
|
|
|
|
public Pizza(String bread,String sauce)
|
|
{
|
|
this.bread = bread;
|
|
this.sauce = sauce;
|
|
}
|
|
|
|
public Pizza(String bread)
|
|
{
|
|
this.bread = bread;
|
|
}
|
|
|
|
}
|
|
```
|
|
|
|
```C#
|
|
// Main
|
|
|
|
Pizza pizza = new Pizza("stuffed crust", "red sauce", "mozzarella");
|
|
Pizza pizza1 = new Pizza("stuffed crust", "red sauce");
|
|
```
|
|
|
|
## inheritance
|
|
|
|
1 or more child classes recieving fields, methods, etc. from a common parent
|
|
|
|
```C#
|
|
class Vehicle
|
|
{
|
|
public int speed = 0
|
|
|
|
public void go()
|
|
{
|
|
Console.WriteLine("this vehicle is moving !");
|
|
}
|
|
}
|
|
|
|
Class Car : Vehicle
|
|
{
|
|
public int wheels = 4;
|
|
}
|
|
Class Bicycle : Vehicle
|
|
{
|
|
public int wheels = 2;
|
|
}
|
|
Class Boat : Vehicle
|
|
{
|
|
public int wheels = 0;
|
|
}
|
|
```
|
|
|
|
```C#
|
|
// Main
|
|
|
|
Car car = new Car();
|
|
Bicycle bicycle = new Bicycle();
|
|
Boat boat = new Boat();
|
|
|
|
Console.WriteLine(car.speed());
|
|
Console.WriteLine(car.wheels());
|
|
Console.WriteLine(car.go());
|
|
|
|
Console.WriteLine(bicycle.speed());
|
|
Console.WriteLine(bicycle.wheels());
|
|
Console.WriteLine(bicycle.go());
|
|
|
|
Console.WriteLine(boat.speed());
|
|
Console.WriteLine(boat.wheels());
|
|
Console.WriteLine(boat.go());
|
|
```
|
|
|
|
## abstract classes
|
|
|
|
modifier that indicates missing components or incomplete implemetation
|
|
|
|
```C#
|
|
abstract class Vehicle
|
|
{
|
|
public int speed = 0
|
|
|
|
public void go()
|
|
{
|
|
Console.WriteLine("this vehicle is moving !");
|
|
}
|
|
}
|
|
|
|
Class Car : Vehicle
|
|
{
|
|
public int wheels = 4;
|
|
int maxSpeed = 500;
|
|
}
|
|
Class Bicycle : Vehicle
|
|
{
|
|
public int wheels = 2;
|
|
int maxSpeed = 50;
|
|
}
|
|
Class Boat : Vehicle
|
|
{
|
|
public int wheels = 0;
|
|
int maxSpeed = 100;
|
|
}
|
|
```
|
|
|
|
```C#
|
|
// Main
|
|
Car car = new Car();
|
|
Bicycle bicycle = new Bicycle();
|
|
Boat boat = new Boat();
|
|
Vehicle vehicle new Vehicle();
|
|
// error can't instanciate
|
|
```
|
|
|
|
## array of object
|
|
|
|
```C#
|
|
Class Car
|
|
{
|
|
public String model;
|
|
public Car(String model)
|
|
{
|
|
this.model = model;
|
|
}
|
|
}
|
|
```
|
|
|
|
```C#
|
|
// main
|
|
Car[] garage = new Car[3]
|
|
|
|
Car car1 = new Car("Mustang");
|
|
Car car2 = new Car("Corvette");
|
|
Car car3 = new Car("Lambo");
|
|
|
|
garage[0] = car1
|
|
garage[1] = car2
|
|
garage[2] = car3
|
|
|
|
// or with anonymous object
|
|
|
|
Car[] garage = {new Car("Mustang"), new Car("Corvette"), new Car("Lambo")}
|
|
|
|
forach(Car car in garage)
|
|
{
|
|
Console.WriteLine(car.model());
|
|
}
|
|
```
|
|
|
|
## Object as arguments
|
|
|
|
|
|
```C#
|
|
Class Car
|
|
{
|
|
public String model;
|
|
public String color;
|
|
|
|
public Car(String model, String color)
|
|
{
|
|
this.model = model;
|
|
this.color = color;
|
|
}
|
|
}
|
|
```
|
|
|
|
```C#
|
|
// main
|
|
|
|
Car car1 = new car("Mustang", "red");
|
|
Car car 2 = Copy(car1)
|
|
|
|
ChangeColor(car1, "siver");
|
|
|
|
Console.WriteLine(car1.color + " " + car1.model)
|
|
|
|
Console.WriteLine(car2.color + " " + car2.model)
|
|
|
|
public static void ChangeColor(Car car, String color)
|
|
{
|
|
car.color = color;
|
|
}
|
|
|
|
public static Car Copy(Car car)
|
|
{
|
|
return new Car(car.model, car.color);
|
|
}
|
|
```
|
|
|
|
## method overiding
|
|
|
|
provides a new version of a method inherited from a parent class
|
|
inherited method must be: abstract, virtual, or already overriden
|
|
Used with Tostring(), polymporphism
|
|
|
|
```C#
|
|
Class Animal
|
|
{
|
|
public virtual void Speak()
|
|
{
|
|
Console.WriteLine("The animal goes *brrr*")
|
|
}
|
|
}
|
|
Class Dog : Animal
|
|
{
|
|
public override void Speak()
|
|
{
|
|
Console.WriteLine("The dog goes *Woof*")
|
|
}
|
|
|
|
}
|
|
Class Cat : Animal
|
|
{
|
|
public override void Speak()
|
|
{
|
|
Console.WriteLine("The cat goes *Meow*")
|
|
}
|
|
}
|
|
```
|
|
|
|
```C#
|
|
Dog dog = new Dog();
|
|
Cat cat = new Cat();
|
|
|
|
dog.Speak();
|
|
cat.Speak();
|
|
```
|
|
|
|
## ToString
|
|
|
|
Converts an object ot its string representation so that it is suitable for display
|
|
|
|
```C#
|
|
class Car
|
|
{
|
|
String make;
|
|
String model;
|
|
int year;
|
|
String color;
|
|
|
|
public Car(String make,String model,int year,String color)
|
|
{
|
|
this make = make;
|
|
this model = model;
|
|
this year = year;
|
|
this color = color;
|
|
}
|
|
|
|
public overide ToString()
|
|
{
|
|
String message = "this is a " + make + " " + model;
|
|
return message;
|
|
}
|
|
}
|
|
```
|
|
|
|
```C#
|
|
// Main
|
|
|
|
Car car = new Car("Chevy", "Corvette", 2022, "blue");
|
|
|
|
Console.WriteLine(car);
|
|
```
|
|
|
|
## Polymporphism
|
|
|
|
Objects can e identified by more than one type
|
|
Ex. A Dog is also: Canine, Animal, Organisms
|
|
|
|
```C#
|
|
class Vehicle
|
|
{
|
|
public int speed = 0
|
|
|
|
// have to add virtual for polymporphism
|
|
public virtual void Go()
|
|
{
|
|
Console.WriteLine("this vehicle is moving !");
|
|
}
|
|
}
|
|
|
|
Class Car : Vehicle
|
|
{
|
|
public int wheels = 4;
|
|
public overide void Go()
|
|
{
|
|
Console.WriteLine("this car is moving !");
|
|
}
|
|
}
|
|
Class Bicycle : Vehicle
|
|
{
|
|
public int wheels = 2;
|
|
public overide void Go()
|
|
{
|
|
Console.WriteLine("this go is moving !");
|
|
}
|
|
}
|
|
Class Boat : Vehicle
|
|
{
|
|
public int wheels = 0;
|
|
public overide void Go()
|
|
{
|
|
Console.WriteLine("this boat is moving !");
|
|
}
|
|
}
|
|
```
|
|
|
|
```C#
|
|
// Main
|
|
Car car = new Car();
|
|
Bicycle bicycle = new Bicycle();
|
|
Boat boat = new Boat();
|
|
|
|
Vehicle[] vehicles = {car, bicycle, boat};
|
|
|
|
forach (vehicle vehicle in vehicle)
|
|
{
|
|
vehicle.Go();
|
|
}
|
|
```
|
|
|
|
## interfaces
|
|
|
|
defines a "contract" that all the classes inheriting from should follow
|
|
An inerface declares "what a class should have"
|
|
An inheriting class defines "how it should do it"
|
|
Benefit are security + multiple iheritance + "plug-and-play"
|
|
|
|
|
|
```C#
|
|
interface IPrey
|
|
{
|
|
void Flee();
|
|
}
|
|
|
|
interface IPredator
|
|
{
|
|
void Hunt();
|
|
|
|
}
|
|
|
|
class Rabbit : IPrey
|
|
{
|
|
pulic void Flee()
|
|
{
|
|
Console.WriteLine("The rabbit run away!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
class hawk : IPredator
|
|
{
|
|
pulic void Hunt()
|
|
{
|
|
Console.WriteLine("The hawk is searching for food!");
|
|
}
|
|
|
|
}
|
|
|
|
class fish : IPrey, IPredator
|
|
{
|
|
|
|
pulic void Flee()
|
|
{
|
|
Console.WriteLine("The fish swims away!");
|
|
|
|
}
|
|
pulic void Hunt()
|
|
{
|
|
Console.WriteLine("The fish is searching for food!");
|
|
}
|
|
|
|
|
|
}
|
|
|
|
```
|
|
```C#
|
|
// Main
|
|
|
|
Rabbit rabbit = new Rabbit();
|
|
Hawk hawk = new Hawk();
|
|
Fish fish = new Fish();
|
|
|
|
rabit.flee();
|
|
hawk.hunt();
|
|
fish.flee();
|
|
fish.hunt();
|
|
```
|
|
|
|
## getter and setter
|
|
|
|
getter & setters add security to fields by encapsulation
|
|
They're accessors found within properties
|
|
|
|
properties = combine aspects of both fields and methods (share name with a field)
|
|
get accessor = used to return the property value
|
|
set accessor = used to assign a new value
|
|
value keyword = defines the value being assigned by the set (parameter)
|
|
|
|
|
|
```C#
|
|
Class Car
|
|
{
|
|
private int speed;
|
|
|
|
public Car(int speed)
|
|
{
|
|
Speed = speed;
|
|
}
|
|
|
|
// properties
|
|
public int Speed
|
|
{
|
|
|
|
get { return speed}; //read
|
|
set
|
|
{
|
|
if value(value > 500)
|
|
{
|
|
speed = 500;
|
|
}else{
|
|
speed = value;
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
```
|
|
|
|
```C#
|
|
// Main
|
|
|
|
Car car = new Car();
|
|
|
|
Car Speed = 10000000000;
|
|
|
|
Console.WriteLine(car.Speed);
|
|
```
|
|
|
|
## Auto-Implemted properties
|
|
|
|
shortcut when no additional logic is required in the property
|
|
you do not have to define a field for a property,
|
|
you only have to write get; and/or set; insid the property;
|
|
|
|
```C#
|
|
Class Car
|
|
{
|
|
// shortcut
|
|
public String Model {get; set;}
|
|
public Car(String model)
|
|
{
|
|
this.Model = model;
|
|
}
|
|
}
|
|
```
|
|
|
|
## Enums
|
|
|
|
Special "class" that contains a set of named integer constans.
|
|
use enums when you have values that you know will not change,
|
|
To get the integer value from an item, you must explicitly convert to an int
|
|
|
|
name = integer
|
|
|
|
```C#
|
|
enum Planets
|
|
{
|
|
Mecury,
|
|
Venus,
|
|
Earth,
|
|
Mars,
|
|
Jupiter,
|
|
Saturn,
|
|
Uranus,
|
|
Neptune,
|
|
Pluto
|
|
}
|
|
|
|
enum PlanetsRadius
|
|
{
|
|
Mecury = 2439,
|
|
Venus = 6051,
|
|
Earth = 6371,
|
|
Mars = 3389,
|
|
Jupiter = 69911,
|
|
Saturn = 58232,
|
|
Uranus = 25362,
|
|
Neptune = 24622,
|
|
Pluto = 1188
|
|
}
|
|
|
|
|
|
Console.WriteLine(Planets.Pluto + " is a planet")
|
|
|
|
|
|
String name = PlanetsRadius.Earth.ToString();
|
|
// return : Earth
|
|
|
|
int radius = (int)PlanetsRadius.Earth;
|
|
// return : 6371
|
|
```
|
|
|
|
## generics
|
|
|
|
not specific to a particular data type
|
|
add <T> to : classes, methods, fields, etc.
|
|
or other name ex : <Things>
|
|
allow for code reusability for different data types
|
|
|
|
```C#
|
|
int[] intArray = { 1, 2, 3};
|
|
double[] doubleArray = { 1.0, 2.0, 3.0};
|
|
String[] stringArray = {"1","2","3"};
|
|
|
|
// don't work for other array
|
|
public static void() displayElements<T>(T[] array)
|
|
{
|
|
foreach(T item in array)
|
|
{
|
|
Console.Write(item + " ")
|
|
}
|
|
|
|
}
|
|
```
|