private static void SimpleClassPolymorphism() { Daughter daughter = new Daughter(); daughter.MaString = "test"; String test = daughter.MaString; Mother mother = new Mother(); mother.Var1 = 1; // Simple cast ((Mother)daughter).Var1 = 10; // Unallowed because Daughter is under Mother //((Daughter)mother).Var1 = 10; //((Daughter)mother).MaString = "string from daughter"; // No explicite conversion allowed //((String)mother).MaString = "string from daughter"; (daughter as Mother).Var1 = 12; if (daughter is Mother) { Console.WriteLine("Daughter have Mother in tree"); } if (daughter is Daughter) { Console.WriteLine("Daughter from Daughter class"); } Console.ReadKey(); }
public override void Initiate() { var ai = (AILevels)Npc.Hangar.Ship.AI; switch (ai) { case AILevels.PASSIVE: case AILevels.AGGRESSIVE: CurrentNpc = new Basic(this); break; case AILevels.GALAXY_GATES: case AILevels.INVASION: CurrentNpc = new Aggressive(this); break; case AILevels.MOTHERSHIP: //CurrentNpc = new Mothership(this, World.StorageManager.Ships[81]); break; case AILevels.DAUGHTER: CurrentNpc = new Daughter(this); break; } Active = true; //Npc.Log.Write($"(ID: {Npc.Id}, {DateTime.Now}) Setted AI to {ai}"); Checkers.Start(); Task.Factory.StartNew(ActiveTick); }
static void Main(string[] args) { Console.Title = "Base"; Daughter anna = new Daughter(); Son brad = new Son(); Son carl = new Son(100); Console.ReadKey(); }
static void Main(string[] args) { //Instantiate Father List which is going to hold the Daughter and Son objects List<Father> myList = new List<Father>(); //Instatiate Son (derived from Father) object and add values Son theSon = new Son(); theSon.HasADick = true; theSon.Age = 20; theSon.Name = "Luke"; //Instatiate Daughter (derived from Father) object and add values Daughter theDaughter = new Daughter(); theDaughter.HasAPussy = true; theDaughter.Name = "Leia"; theDaughter.Age = 19; //Add Son and Daughter objects to the baseclass List<Father> myList.Add(theSon); myList.Add(theDaughter); //Print objects in the Father list Console.WriteLine("*************iteration start***********"); foreach (var item in myList) { if (item is Son) { Son tempSon = item as Son; Console.WriteLine("Name: {0}", item.Name); Console.WriteLine("Age: {0}", item.Age); Console.WriteLine("Has a dick: {0}", tempSon.HasADick); } else if (item is Daughter) { Daughter tempDaughter = item as Daughter; Console.WriteLine("Name: {0}", item.Name); Console.WriteLine("Age: {0}", item.Age); Console.WriteLine("Has a pussy: {0}", tempDaughter.HasAPussy); } if (!myList.LastIndexOf(item).Equals(myList.Count - 1)) { Console.WriteLine(" /^ ^ ( o Y o) "); Console.WriteLine(" / . .) ) ( "); Console.WriteLine("*******************( * )*******************"); Console.WriteLine(" OMG..! \\ Y / "); Console.WriteLine(" \\/ / "); } } Console.WriteLine("*************iteration end*************"); }
static void Main(string[] args) { List <string> family_list = new List <string>(); Father father = new Father(); Mother mother = new Mother(); Daughter daughter = new Daughter(); father.gender = "male"; father.name = "Alex"; father.Age = 40; family_list.Add(father.name); Console.WriteLine(family_list[0]); Console.WriteLine(); father.Work(); MyEvent evt1 = new MyEvent(); evt1.UserEvent += father.FamilyInfoHandler; evt1.OnUserEvent(); mother.gender = "female"; mother.name = "Anna"; mother.Age = 36; family_list.Add(mother.name); Console.WriteLine(family_list[1]); Console.WriteLine(); mother.Work(); MyEvent evt2 = new MyEvent(); evt2.UserEvent += mother.FamilyInfoHandler; evt2.OnUserEvent(); daughter.gender = "female"; daughter.name = "Alice"; daughter.Age = 4; family_list.Add(daughter.name); Console.WriteLine(family_list[2]); Console.WriteLine(); MyEvent evt3 = new MyEvent(); evt3.UserEvent += daughter.FamilyInfoHandler; evt3.OnUserEvent(); }
static void Main(string[] args) { /*The instance of the book's class with the constructor*/ Book book = new Book("Stephen King", "Shining", 200); Console.WriteLine(book.getAuthor() + ", " + book.getTitle() + ", " + book.getPages()); Person p1 = new Person("Valerio", "Valentino"); Console.Write(p1.getName + ", " + p1.getSurname); Console.Write("Age : " + Person.age); Console.WriteLine(); /*New instance of Mother Class*/ Mother m = new Mother("Bubba"); /*New instance of Daughter Class, derived by the Mother Class*/ Daughter d = new Daughter("Forrest", "Gump"); Console.Write("mother's name -> " + m.getName); Console.WriteLine(); Console.Write("daughter's name -> " + d.getName + "... and second name -> " + d.getSecondName); Console.ReadLine(); }
public static void Main(string[] args) { Console.WriteLine("----------------------------------Encapsulation-----------------------"); Bank suriBank = new Bank(); suriBank.Balance = 1500; Console.WriteLine(suriBank.Balance); Console.WriteLine("----------------------------------Abstraction-----------------------"); UPIAccount suriUpi = new UPIAccount(); suriUpi.BankName = "Suri Bank"; suriUpi.accountType = "Salary"; suriUpi.UPIAccountNumber = "7382659190@Oksuri"; suriUpi.accountHolderName = "Surendra Kumar L"; suriUpi.Balance = 62000; suriUpi.AccountDetails(); Console.WriteLine("----------------------------Single Inheritance-----------------------"); Child child = new Child(); child.ChildAsserts(); child.ParentAsserts(); Console.WriteLine("----------------------------Hierarchical Inheritance-----------------"); Son son = new Son(); son.ParentAsserts(); son.SonAsserts(); Console.WriteLine("------------------------"); Daughter daughter = new Daughter(); daughter.ParentAsserts(); daughter.DaughterAsserts(); Console.WriteLine("----------------------------Multilevel Inheritance-----------------"); MISon mIson = new MISon(); mIson.GrandParentAsserts(); mIson.ParentAsserts(); mIson.SonAsserts(); Console.WriteLine("------------------------"); MIDaughter mIdaughter = new MIDaughter(); mIdaughter.GrandParentAsserts(); mIdaughter.ParentAsserts(); mIdaughter.DaughterAsserts(); Console.WriteLine("----------------------------Multiple Inheritance-----------------"); LoanCalculator loanCalculator = new LoanCalculator(); Console.WriteLine("5 Lac Personal loan interest for 5Years :" + loanCalculator.CalculatePersonalLoan(500000, 5)); Console.WriteLine("5 Lac Car loan interest for 5Years:" + loanCalculator.CalculateCarLoan(500000, 5)); Console.WriteLine("5 Lac Home loan interest for 5Years :" + loanCalculator.CalculateHomeLoan(500000, 5)); Console.WriteLine("--------------------------Compile Time Polymorphism---------------"); Calculator calculatior = new Calculator(); Console.WriteLine("With two int param value :" + calculatior.Add(1, 1)); Console.WriteLine("With one int,one double param's value :" + calculatior.Add(1, 1.1)); Console.WriteLine("With two double param value :" + calculatior.Add(1.5, 1.8)); Console.WriteLine("--------------------------Run Time Polymorphism---------------"); CentralPetrol centralPetrol = new CentralPetrol(); CentralPetrol apPetrol = new ApPetrol(); CentralPetrol tsPetrol = new TSPetrol(); CentralPetrol ksPetrol = new KSPetrol(); Console.WriteLine(" CentralPetrol price per litter :" + centralPetrol.GetLatestPetrolPrice(1)); Console.WriteLine("Andhra pradesh state Petrol price per litter :" + apPetrol.GetLatestPetrolPrice(1)); Console.WriteLine("Tamilanadu state Petrol price per litter :" + tsPetrol.GetLatestPetrolPrice(1)); Console.WriteLine("Karnataka state Petrol price per litter :" + ksPetrol.GetLatestPetrolPrice(1)); /* * The compiler requires an GetLatestPetrolPrice() method in CentralPetrol class * and it will find the method then compiles successfully. * but the right version of the GetLatestPetrolPrice() method is not being determined at compile time * but determined at runtime. Finally the overriding methods must have the same name and parameters/arguments, * as the virtual or abstract method defined in the base class method and that it is overriding in the derived class. */ Console.ReadLine(); }
/////////////////////////////////////////////////////////////////////////////// // NAME // CalculateXCoordinate // ARGUMENTS // pTree - pointer to tree this node is in // pDC - pointer to the device context // iMaxColWidth - maximum width of the current column // DESCRIPTION // Determine the X-axis coordinate for this node // It assumes that the width of this and all other nodes have // already been established. // It also assumes that higher branching nodes are not wider than the // total width of their daughters (which may not always be true...) // RETURN VALUE // The X-axis coordinate at the mid-point of this node // public int CalculateXCoordinate(LingTreeTree Tree, Graphics grfx, int iMaxColWidth) { XMid = 0; // Determine the width for this node CalculateWidth(Tree, grfx); if (Daughter != null) { // is a non-terminal node LingTreeNode NextDaughter = Daughter.Sister; if (NextDaughter != null) { // node branches if (iMaxColWidth < m_iWidth) { iMaxColWidth = m_iWidth; // remember widest node in the column } } else { // only one daughter, so could be in a column if (iMaxColWidth < Width) { iMaxColWidth = Width; // remember widest node in the column } } int iLeftMost = Daughter.CalculateXCoordinate(Tree, grfx, iMaxColWidth); int iRightMost = iLeftMost; while (NextDaughter != null) { // calculate coordinates for other daughters iRightMost = NextDaughter.CalculateXCoordinate(Tree, grfx, iMaxColWidth); NextDaughter = NextDaughter.Sister; } // take mid point between first & last daughter XMid = (iLeftMost + iRightMost) / 2; if (iRightMost > iLeftMost) { if (m_iWidth > (iRightMost - iLeftMost)) { int iAdjust = (m_iWidth - (iRightMost - iLeftMost)) / 2; m_iXMid += iAdjust; NextDaughter = Daughter; while (NextDaughter != null) { NextDaughter.AdjustXValues(Tree, iAdjust); NextDaughter = NextDaughter.Sister; } } } } else { // is a terminal node if (iMaxColWidth < Width) { iMaxColWidth = Width; // this might be the widest node in the column } XMid = (int)Tree.HorizontalOffset + // Offset from last terminal node plus iMaxColWidth / 2; // half the width of this column Tree.HorizontalOffset = XMid + // have a new offset for next terminal node Tree.HorizontalGap + // gap between terminal nodes plus iMaxColWidth / 2; // half the width of the widest node in this column } XCoord = XMid - Width / 2; // adjust for width of this node int iEnd = XCoord + Width; if (Triangle) { iEnd += Tree.HorizontalGap / 2; } if (iEnd > Tree.XSize) { Tree.XSize = iEnd; // Keep track of total width for scrolling } #if DoTrace Console.WriteLine("{0}\tXSize = {1},\tWidth = {2},\tXCoord = {3},\tYCoord = {4}, \tXMid = {4}", Content, Tree.XSize, Width, XCoord, YCoord, XMid); #endif return(XMid); }