pause at 3:00
This commit is contained in:
309
C# Full Course for free/14.OOP.md
Normal file
309
C# Full Course for free/14.OOP.md
Normal file
@@ -0,0 +1,309 @@
|
||||
# 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");
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
// 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#
|
||||
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());
|
||||
}
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user