end of the course
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
# Classes
|
||||
# OOP
|
||||
|
||||
# classes
|
||||
|
||||
In another file
|
||||
```C#
|
||||
@@ -90,6 +92,8 @@ class Human
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
```C#
|
||||
// in the main class
|
||||
|
||||
Human human1 = new Human("Rick",65)
|
||||
@@ -287,6 +291,7 @@ Class Car
|
||||
```
|
||||
|
||||
```C#
|
||||
// main
|
||||
Car[] garage = new Car[3]
|
||||
|
||||
Car car1 = new Car("Mustang");
|
||||
@@ -307,3 +312,380 @@ forach(Car car in garage)
|
||||
}
|
||||
```
|
||||
|
||||
## 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 + " ")
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user