image src: https://javatutorial.net/java-oop
What is Class
Here's the definition of class from Yann MulondaA class is a collection of method and variables. It is a blueprint that defines the data and behavior of a type.This is actually the implementation of Encapsulation Concept
The Anatomy of Class
namespace personEx { abstract class Person { string Name; int Age; int Gender; abstract public void Walk(); abstract public void Eat(); abstract public void Sleep(); } }
On the left is an UML representation of the class and on the right is the c# implementation of the class.
Class Component are as following;
Class Component are as following;
- Class Name
- Data (Represented by fields)
- Behavior (Represented by methods/function)
Object
Object is an instance of a class. From the figure below we create people (John, Peter, Marry, and Sarah) based on Person Class. Everyone can walk, eat and sleep. However everyone have their on Name, age, and gender.
namespace personEx { class Program { static void Main(string[] args) { // Instantiate object var personJohn = new Person("John", 24, "Male"); var personPeter = new Person("Peter", 15, "Male"); var personMarry = new Person("Marry", 22, "Male"); var personSarah = new Person("Sarah", 18, "Male"); } } }
Class Component - In more detail
a bit in more detail about class so you can actually be able to implement it.
- Constructor
- Field
- Method
- Access Modifiers
- Properties
A Constructor
Constructor is a special kind of method that is called when an instance of a class is created. Below are fact about constructor;- Constructor has the same name as the class
- Constructors do not have a return type
- Constructors can be overloaded.
public Person(string name, int age, string gender) { Name = name; Age = age; Gender = gender; }
Field
Field, or instance variables, is a variable defined in a class, for which each object has their own copy if these variable (separate memory)
- A field can be initialized in two ways:
- In a constructor
- directly upon declaration
- use the readonly modifier to improve the robustness of our code
public class Person { string Name; int Age; string Gender; readonly ListfamilyMember;
Method
Method contains the functionality of the object. Here are some thing you should know about method;
- Overloading a method means having a method with the same name but with different signatures.
- Signature of a method consists of the;
- number of the input
- the type of input(s)
- order of input(s)
public void Eat() { Console.WriteLine("{0} is Eating", Name); } public void Eat(string foodName) { Console.WriteLine("{0} is Eating {1}", Name, foodName); }
Main Program:
var personJohn = new Person("John", 24, "Male"); personJohn.Eat();
Output:
John is Eating
Main Program:
var personJohn = new Person("John", 24, "Male"); personJohn.Eat("apple");
Output:
John is Eating apple
Access Modifiers
A way to control access to a class and/or its members for safety for safety and information hiding(Encapsulation and Abstraction)
- Public – access everywhere
- Private – only from the class
- Protected – inside the class and Its derived class
- Internal – Inside the same assembly
- Protected Internal – Inside the same assembly and its derived class
Properties
A property is a kind of class member that is used for providing access to fields of a class.
format1:
format2:
format1:
public class Person { public string name { get; set; }
format2:
public class Person { private string _name; public string Name { get { //we can do some data modification before return the value //eg; string retString = "Mr." + _name return _name; } set { _name = value; } }
Code:
here's the code use in this post
person class(person.cs) :
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace personEx { public class Person { string Name; int Age; string Gender; public Person(string name, int age, string gender) { Name = name; Age = age; Gender = gender; } public void Walk() { Console.WriteLine("{0} is Walking",Name); } public void Eat() { Console.WriteLine("{0} is Eating", Name); } public void Eat(string foodName) { Console.WriteLine("{0} is Eating {1}", Name, foodName); } public void Sleep() { Console.WriteLine("{0} is Sleeping", Name); } } }
Main Console app:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace personEx { class Program { static void Main(string[] args) { // Instantiate object var personJohn = new Person("John", 24, "Male"); var personPeter = new Person("Peter", 15, "Male"); var personMarry = new Person("Marry", 22, "Male"); var personSarah = new Person("Sarah", 18, "Male"); personJohn.Eat(); personJohn.Eat("apple"); Console.ReadKey(); } } }
No comments:
Post a Comment