Using Abstract class, Interface, Collections in C#
Abstract
class – bu class ob’yektini yaratib bo’lmaydigan class turi, uni base class
sifatida ishlatish mumkin. Abstract classda abstract yoki abstract bo’lmagan
metodlar va memberlardan foydalanish mumkin. Abstract classda maydon(field)ni
abstract qilib yaratish MUMKIN EMAS, property(get;set;) va metodlarni abstract
qilib yaratish MUMKIN.
Masalan:
public abstract class MyClass
{
public
abstract string Name{get;set;} //Mumkin
public abstract string
ColumnName; //Mumkin emas, chunki
bu FIELD
public abstract void GetValue() //Mumkin
{
//…
}
}
Question 1:
Abstract classda aniqlangan abstract metod(yoky property) va abstract bo’lmagan
metodning(yoki property) qanday farqi bor?
Answer: abstract classdan inheritance olgan classda abstract classdagi abstract metodlar(propertylar) override qilinib qayta aniqlanishi shart. Abstract metod(yoki property) lar Abstract classda faqatgina e’lon qilinadi uning nima ish bajarishi esa Abstract classdan inheritance olgan classda aniqlanadi.
Masalan:
1)
Methods
public abstract
class A
{
public string Name;
public abstract string GetName1();//Bu metodni GetName1(){…} kabi
aniqlash mumkin emas
public string GetName2()
{
return “This is
GetName2 method’s result”
}
}
public class
B:A
{
//Bu yerda A classdagi GetName1 metodi override qlinib qayta aniqlanishi
shart chunki u abstract metod, aks holda //xatolik yuz beradi.
public override string GetName1()
{
return
“This is GetName1() method’s result”;
}
}
2) abstract properieslarni ishlatish:
abstract class A
{
protected int myNumber;
public abstract int numbers
{
get;
set;
}
}
class B: A
{
// abstract propertylarni Implement qilamiz
public override int numbers
{
get
{
return myNumber;
}
set
{
myNumber = value;
}
}
}
Question 2: Abstract
classning interfacedan farqi ?
Answer:
Abstract sinf abstract memberlardan va abstract bo’lmagan memberlardan iborat
bo’lishi mumkin, interfaceda esa barcha memberlar abstract bo’lib, har bir
child sinfda override qilinishi shart! Ya’ni interfaceda abstract bo'lmagan member
yaratib bo’lmaydi.
interface iSampleInterface
{
//Betta barcha memberlar avtomatik abstract yaratilvotti
int AddNumbers(int Num1, int Num2);
int MultiplyNumbers(int Num1, int Num2);
}
Abstract
sinfda abstract memberlarni yaratish interfacedagi kabi bir xil effect beradi
Endi
quyidagi CODE yordamida abstract class va interfaceni ishlatamiza:
Task: Ishchi
(Worker), Programmist(Programmer), O’qituvchi(Teacher) sinflarini ulardagi
logika bo’yicha yaratish
Soluation:
1)
Dastlab IBaseWorker interfaceini yaratamiz:
public interface IBaseWorker : IDisposable
{
List<string> GetResume(); //Har bir ishchiga mos RESUME
}
2)
BaseWorker sinfini abstract ko’rinishda yaratamiz(chunki
uning ob’yektini yasash mumkin bo’lmasligi lozim):
public abstract class BaseWorker : IBaseWorker
{
public BaseWorker()
{
BeforeInit();
//TODO
AfterInit();
}
public Guid Id { get; set; }
public string FullName { get; set; }
public string AddinationalInfo { get; set; }
public string PhoneNumber { get; set; }
public int Age { get; set; }
public string WorkExperience { get; set; }
public double Salary { get; set; }
public virtual void AfterInit() { }
public virtual void BeforeInit()
{
Id = Guid.NewGuid();//Initdan oldin har
bir workerga ID berish
}
public abstract List<string> GetResume();
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
//Bu
yerda DISPOSE class ob’yekti qilinadi
}
}
}
3)
Programmer sinfini yaratamiz, bu BaseWorkerning “bolasi”
bo’lishi lozim:
public class Programmer:BaseWorker
{
public string ProgrammingLanguages { get; set; }
public string DataBases { get; set; }
public string ProjectsInfo { get; set; }
//Quyida GetResume metodi
override qilinyapdi chunki u Base classda abstract //qilib e’lon qilingan
public override List<string> GetResume()
{
var resume = new List<string>();
resume.Add("Full Name:" +
FullName);
resume.Add("Age:" + Age);
resume.Add("Phone Number:" +
PhoneNumber);
resume.Add("Learning Programming Languages:"+ProgrammingLanguages);
resume.Add("Using Data Bases:" + DataBases);
resume.Add("Succesfully projects:" + ProjectsInfo);
resume.Add("Work Expriense (Ishlagan joylari):" + WorkExperience);
return resume;
}
}
4)
Teacher sinfini yaratamiz(Bu sinf ham BaseWorker
sinfinig “bolasi” sifatida yaratiladi):
public class Teacher:BaseWorker
{
public string Specialty { get; set; }//Mutaxasisligi
public int Category { get; set; } //Toifasi
public string Degree { get; set; }
public override List<string> GetResume()
{
var resume = new List<string>();
resume.Add("Full Name:" +
FullName);
resume.Add("Age:" + Age);
resume.Add("Phone Number:" +
PhoneNumber);
resume.Add("Specialty:" +
Specialty);
resume.Add("Category:" +
Category);
resume.Add("Degree:" +
Degree);
resume.Add("Work Expriense (Ishlagan joylari):" + WorkExperience);
return resume;
}
}
5)
Programmers kolleksiya sinfini yaratamiz, bu
sinf ob’yektida barcha Programmer sinfi ob’yektlari saqlanadi:
public class Programmers:List<Programmer>
{
}
6)
Teachers kolleksiya sinfini yaratamiz:
public class Teachers:List<Teacher>
{
}
Yuqorida yaratilgan sinflardan foydalanish:
Yuqorida yaratilgan sinflardan foydalanish:
namespace AbstractClassInterfaceCollectionTestByMansur
{
class Program
{
static void Main(string[] args)
{
var teachers=GetTeachers();
var programmers = GetProgrammers();
var resumes = new List<string>();
Console.WriteLine(">
Programmers resume:");
programmers.ForEach(f =>
{
f.GetResume().ForEach(r
=>
Console.WriteLine(r));
Console.WriteLine("_".PadRight(50,'_'));
});
Console.WriteLine(">
Teachers resume:");
teachers.ForEach(f =>
{
f.GetResume().ForEach(r =>
Console.WriteLine(r));
Console.WriteLine("_".PadRight(50, '_'));
});
Console.ReadLine();
}
private static Teachers GetTeachers()
{
var teachers = new Teachers();
teachers.Add(new Teacher()
{
FullName = "Qosimov A.",
Degree = "dotsent",
Category = 1,
AddinationalInfo = "Buxoro city",
Salary = 2000,
Specialty = "Mathmatics",
WorkExperience = "year1 - year2. Work exp.1; year2-year3. Work
exp2;"
});
teachers.Add(new Teacher()
{
FullName = "Axmedov H.",
Degree = "dotsent",
Category = 1,
AddinationalInfo = "Jondor city",
Salary = 1500,
Specialty = "Mathmatics",
WorkExperience = "year1 - year2. Work exp.1; year2-year3. Work
exp2;"
});
return teachers;
}
private static Programmers GetProgrammers()
{
var programmers = new Programmers();
programmers.Add(new Programmer()
{
FullName = "Hayotov Hayot Rahmonovich",
Age = 28,
DataBases = "MS Sql 2005,2008,2012, Oracle, Postgres",
ProgrammingLanguages ="C#, Java",
Salary =1500000,
WorkExperience = "2013-2014, Tashkent West Minster University; 2014 -
..., Sunet;",
ProjectsInfo = "Document oborot, Buxgalteriya",
PhoneNumber = "123-45-67",
AddinationalInfo = "Lives in Uzb.,Tashkent"
});
programmers.Add(new Programmer()
{
FullName = "Eshonqulov Hamza",
Age = 28,
DataBases = "MS Sql 2005,2008,2012",
ProgrammingLanguages = "C#, Java, Pascal",
Salary = 1000000,
WorkExperience = "2010-..., Bukhara State University;",
ProjectsInfo = "Avtomatlashtirilgan Ijro intizomi tizimi,
Buxgalteriya",
PhoneNumber = "123-45-67",
AddinationalInfo = "Lives in Uzb.,Bukhara city"
});
return programmers;
}
}
}
No comments:
Post a Comment
Note: only a member of this blog may post a comment.