daily exercise - object oriented
Daily exercise (Object Oriented)
//encapsulation :Only leave behind the interface for client and hide the member of the class by get/write method or property.
//encapsulation example
public class Person
{
private string _name;
public string Name
{
get
{
Return _name;
}
set
{
_name=value;
}
}
}
Question1: Which method is better between using attribute and encapsulation method?
Question2:When we will must use index ?
Question3:Can we get the value like Name if we will get it in AjaxMethod ?
//Inheritance :The class can use other class’s member.
//Inheritance example
public class ParentClass
{
public ParentClass()
{
Console.WriteLine(“ParentClass”);
}
public void Print()
{
Console.WriteLine(“This is a parent class”);
}
}
public class ChildClass:ParentClass
{
Public ChildClass()
{
Console.WriteLine(“ChildClass”);
}
}
public class App
{
public static void
{
ChildClass child=new ChildClass();
Child.Print();
}
}
Question4:What’s the number of the class we can inherit most?
Question5:What’s the number of the level we can inherit most?
Question6:Which is better between abstract class and interface?
Question7:Usually,which is used more times between abstract class and interface?
//Polymorphism : Use new or override to rewrite base class’s member.
//Polymorphism example
public class Log
{
public virtual void Write();
}
public class EventLog:Log
{
public override void Write()
{
Console.WriteLine("EventLog write sucess !");
}
}
public class FileLog:Log
{
public override void Write()
{
Console.WriteLine("FileLog write sucess !");
}
}
Question8:What’s the different between the next code if I change it?
public abstract class Log
{
public abstract void Write();
}
public class Log
{
public virtual void Write();
}
Question9: Is it true if I write the code like this?
Public class Log
{
Public abstract void Write();
}
Question10:Can I write the method like this : public abstract override void Write();
Question11:When we use the keywords ‘new’?
Question12:What’s the different between ‘public sealed override void Write()’ and ‘public new void Write()’?
Question13:What is the advantage if I use ‘base.Write();’?
Question14:Can I write the code like this ?
Public abstract class Log
{
Public virtual void Write();
}
Public class FileLog:Log
{
Public new void Write();
}
Question15: What’s the result we can get?
ChildClass child=new ChildClass();
Child.Write();
ParentClass parent=(ParentClass)child;
Parent.Write();
Question16:What we can get if write the code like this ‘ParentClass parent=new ChildClass();’?
Question17:When we could use sealed?
Question18:Can I write the code like this : public sealed void Write();?
Question19:If I define the method with virtual in base class, Is it necessary to inherite it in child class ?
Question20: Which method is helpful for me to abstract the object in project?