Did you know that C# is an object oriented programming language (OOP)?
Computer programs are made up of objects that usually interact with each other. For example in a game you have an object called "hero" and this object "hero" may have to interact with an object called "villain".
- Objects have different attributes and behaviors.
- When one is about to build software he should consider all the objects that he/she may need.
- It is even possible that some of these objects could be reused in other programs.
Types of Objects:
- int e.g 1, 2, 3, 100, 1000, -10000 (positive and negative whole numbers.
- double e.g 2.234, 6.878, 75.55 (decimals)
- string e.g "Hello World" (text enclosed by quotation marks)
Classes:
A class is a blueprint for making objects of a particular type. Doubles, ints and strings are examples of classes.
Classes contain attributes or things we call fields. Every field has an accessibility level which determines if it can be accessed outside of the class. A public keyword means that a field can be accessed from outside the class whereas a private keyword means that it cannot.
These fields can be accessed using dot notation.
e.g
class Car {
public int NumberOfWheels;
private int NumberOfDoors
}
//Creating a car object
Car car = new Car();
Classes contain attributes or things we call fields. Every field has an accessibility level which determines if it can be accessed outside of the class. A public keyword means that a field can be accessed from outside the class whereas a private keyword means that it cannot.
These fields can be accessed using dot notation.
e.g
class Car {
public int NumberOfWheels;
private int NumberOfDoors
}
//Creating a car object
Car car = new Car();
//Using dot notation to give that car 4 wheels
car.NumberOfWheels = 4
car.NumberOfWheels = 4
Instances:
An instance of that class is an individual object of that particular class.
Lets say we had a class Car and we wanted to create a car object.
That is all there is to it!.
- "Hello World" is an object of type string;
- 4 is an object of type int;
- 3.142 is an object of type double;
Constructors:
Constructor methods ensure that the fields in a class are filled out when we create an object by using a constructor method. Constructor methods are used to create instances of a class and they have the same name as the class that they are in(they are used to initialize an object).
Constructors do not return any values.
No comments:
Post a Comment