Chapter 1: OOP Flashcards

1
Q

ចូរនិយាយអំពី field ។ តើ field មានអ្វីខ្លះ? ចូរលើកឱទាហរណ៍អំពីការបង្កើត field។

A

field មានតួនាទីផ្ទុកទិន្នន័យ។ field មានពីរប្រភេទគឺ Instance field និង Static field ។
Example for create field
public class website {
private String url = “https://www.google.com”; // តម្លៃ field
private String description;
public Website (…)
{

}

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

ចូរនិយាយអំពី properties ។ តើ properties មានអ្វីខ្លះ? ចូរលើកឱទាហរណ៍អំពីការបង្កើត properties។

A

Properties ត្រូវបានកំណត់ថាបានតែ read-only, write-only, read-write properties ។ វាត្រូវបាន classified ទៅជា static properties (កំណត់ដោយ keyword static) និង instance properties ។ A static property/an instance property ជាប្រតិបត្តិការដែលអនុញ្ញាតិអោយចូលទៅប្រើ class’s states/an object’s states ។ Property គឺត្រូវាបានប្រើដូចទៅនឹង field តែវាមានដំណើរការដូចជា method ។ Public instance properties គឺចាំបាច់នៅក្នុង data binding នៃ .NET ។
Code:
public class Website {

public object Owner { set{…} }
public DateTime LiveDate{ get{…} }
public string URL {
get{…}
set{…}
}
public static string DefaultSite { get {return “www.google.com (http://www.google.com/)” ; } }
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

ចូរនិយាយអំពី methods ។ តើ methods មានអ្វីខ្លះ? ចូរលើកឱទាហរណ៍អំពីការបង្កើត methods។

A

Method គឺជា function តំណាងឱ្យ class ឬ object ហើយ method ក៏ជាផ្នែកមួយសំខាន់ក្នុងការប្រើប្រាស់ encapsulation និងការគ្រប់គ្រង object behaviour។
Method មាន 2 គឺ instance method និង static method។
ឧទាហរណ៍អំពីការបង្កើត method
public class Website{
public const String protocol = “http://”;

public void Update()
{…}
public bool is Live(Date d)
{…}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

ចូរនិយាយអំពី Inheritance។ ចូរលើកឱទាហរណ៍ទាក់ទងនឹង Inheritance ដោយការសរសេរកូដ។

A

Inheritance គឺជា object-oriented ដែលទាក់ទងនឹងការ shared characteristics និង behaviors ទៅកាន់ class មួយទៀតហៅថា derived class ។
Example: Creating derived class
public class Contact {
protected string_name;
protected string_email;
public string Info() {…}

}
public class ContactX
: Contact
{
protected string_address;

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

ចូរនិយាយអំពី Encapsulation។ ចូរលើកឱទាហរណ៍ទាក់ទងនឹង Encapsulation ដោយការសរសេរកូដ។

A

Encapsulation គឺជាគោលការណ៍តម្រង់ទិសវត្ថុដែលត្រូវបានផ្សាភ្ជាប់ជាមួយនឹងការលាក់ internals នៃ object ពីពិភពលោកខាងក្រៅ។ .Net មានយន្តការជាច្រើនសម្រាប់គាំទ្រ encapsulation, រួមទាំង properties, methods, និង access modifiers ។ វាមានហេតុផលជាច្រើនសម្រាប់ការគ្រប់គ្រង encapsulation គឺ
1. Good encapsulation កាត់បន្ថយការ lose coupling
2. Internal implementation នៃ object អាចផ្លាស់ប្តូរដោយសេរី
3. user ឃើញតែ member ដែលត្រូវបាន exposed តែប៉ុណ្ណោះ
public class Student{
private string name;
public void SetName(string newName){
name = newName;
}
public string GetName(){
return name;
}
}
class Program{
static void Main(){
Student student = new Student();
student.SetName(“Alice”);
Console.WriteLine($”Student Name: {student.GetName()}”);
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

ចូរនិយាយអំពី Polymorphism។ ចូរលើកឱទាហរណ៍ទាក់ទងនឹង Polymorphism ដោយការសរសេរកូដ។

A

Polymorphism គឺជាផ្នែកមួយនៃ OOP ដែលអនុញ្ញាតឱ្យ object មួយមានទម្រង់ច្រើន មានន័យថាវាអាចប្រតិបត្តិឬធ្វើអ្វីមួយដោយមាន object ជាច្រើន។ Polymorphism មាន 2 គឺ Compile Time Polymorphism ( method overloading) និង Run Time Polymorphism ( method overriding )។
ឧទាហរណ៍
public class Game {
public virtual int NumPlayers{ get{return_num;} }
public virtual String InfoType(){ return “game”; }
public string Play(){
return $”Thre are { NumPlayers }” + “players are playing a { InfoType() }”;
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

ចូរនិយាយអំពី Delegates។ ចូរលើកឱទាហរណ៍ទាក់ទងនឹង Delegates ដោយការសរសេរកូដ។

A

Delegates គឺជា core features នៃC# languages ដែលផ្តលនូវ ability to dynamically call different method in runtime។ delegate enable publish/ subscribe model ដែល publisher notify observer certain events or changes។ គោលបំណងនៃ delegates គឺបង្កើត infrastructure សម្រាប់ support events។
public delegate void GreetDelegate(string name);
public class Greeter{
public static void SayHello(string name){
Console.WriteLine($”Hello, {name}!”);
}
public static void SayGoodbye(string name){
Console.WriteLine($”Goodbye, {name}!”);
}
public static void Main(string[] args){
GreetDelegate greet = SayHello;
greet(“RUPP”);
greet = SayGoodbye;
greet(“RUPP”);
}
}

OUTPUT:
Hello, RUPP!
Goodbye, RUPP!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

ចូរនិយាយអំពី Event-based។ ចូរលើកឱទាហរណ៍ទាក់ទងនឹង Event-based ដោយការសរសេរកូដ។

A

Event based គឺជា member ដែល enable objects/class ក្នុងការផ្ញើ information នៅត្រង់ state របស់ខ្លួនទៅកាន់ caller ដោយវាធ្វើការប្រាប់ caller អំពីរឿងដែលត្រូវកើតហើយ caller ធ្វើការសម្រេចចិត្ត។ Event ជាទូទៅត្រូវបានប្រើនៅក្នុង ទម្រង់ជា UI bases application។
public partial class Forml : form{
public Form1(){
btnClickme.Click +=ClickMe_Event;
}
private void ClickMe_Event(object sender, EventArgs e){
MessageBox.Show(“Hello World”);
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

ចូរនិយាយអំពី Abstract class។ ចូរលើកឱទាហរណ៍ទាក់ទងនឹង Abstract class ដោយការសរសេរកូដ។

A

Abstract class គឺជា special type រឺមួយក៏ប្រភេទមួយនៃ base class រឺមួយក៏ classមេមួយដែលមានលក្ខណះspecial ហើយជាទូទៅវាអាចនឹងមាន abstract member នៅក្នុង abstract class ហើយ abstract member ទាំងអស់នោះវាអាចជា method រឺ Property ដោយគ្មានការប្រកាសពី implementation។
គោលបំណងនៃការបង្កើត Abstract class គឺការកំណត់នូវ functionality(មុខងារ)ទៅឲ្យclassទាំងឡាយណាដែលបន្តមរតកពីវាទៅ។
បន្ថែមពីនេះទៀត Abstract classត្រូវតែកំណត់ដោយ keyword Abstract ហើយនៅក្នុងនឹងវាអាចជា Abstract method or Abstract property
NOTE: Abstract Classគ្មាន Body ទេ
public class HelloWorld{
public static void Main(string[] args){
Car myCar = new Car();
myCar.StartEngine();
myCar.StopEngine();
}
}
public abstract class Vehicle {
public abstract void StartEngine();
public void StopEngine(){
Console.WriteLine(“Engine Stopped”);
}
}
public class Car:Vehicle{
public override void StartEngine(){
Console.WriteLine(“Car engine started.”);
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

ចូរនិយាយអំពី Interface។ ចូរលើកឱទាហរណ៍ទាក់ទងនឹង Interface ដោយការសរសេរកូដ។

A

Interface ជា references type ដែលត្រូវបានកំណត់នៅក្នុង interface {…} block។ គ្រប់ concrete class ទាំងអស់ត្រូវតែ implement all members of interface ដែលបាន implemented។
Code:
pubic interface IPlay{
void LoadDate(string filename);
Boolean StartPlay();
}
public class SongPlay : IPlay{
public void LoadData(string filename) {…}
public BooleanStartPlay() {…}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly