public void ConstructorTests() { Animal casey = new Animal(); casey.Move(); Console.WriteLine(); Cat mittens = new Cat(); mittens.Move(); mittens.MakeSound(); Console.WriteLine(); Cat content = new Liger(); content.Move(); content.MakeSound(); // content and garfield are both referencing Liger object, but content is Cat type and garfield is Liger type Liger garfield = (Liger)content; // casting content as a Liger garfield.TeethSize = "Kinda large"; Console.WriteLine(); Liger billieJoel = new Liger(); billieJoel.Move(); billieJoel.MakeSound(); billieJoel.TeethSize = "Reeeally big."; }
public void TestMethod1() { Animal firstAnimal = new Animal(); firstAnimal.LegCount = 4; Sloth sloth = new Sloth(); sloth.LegCount = 4; Console.WriteLine(sloth.IsSlow); Cat baseCat = new Cat(); baseCat.MakeSound(); TabbyCat tabby = new TabbyCat(); Liger liger = new Liger(); List <Animal> listOfAnimals = new List <Animal>(); listOfAnimals.Add(baseCat); listOfAnimals.Add(tabby); listOfAnimals.Add(liger); listOfAnimals.Add(sloth); foreach (Animal animal in listOfAnimals) { if (animal is Cat) { Cat cat = (Cat)animal; cat.MakeSound(); } } }
public void ConstructorTests() { Animal casey = new Animal(); casey.Move(); Console.WriteLine(); Cat mittens = new Cat(); mittens.Move(); mittens.MakeSound(); Console.WriteLine(); Cat content = new Liger(); content.Move(); content.MakeSound(); Liger garfield = (Liger)content; garfield.TeethSize = "Kinda large"; Console.WriteLine(); Liger billyJoel = new Liger(); billyJoel.Move(); billyJoel.MakeSound(); billyJoel.TeethSize = "Reeallly big."; }
public void TestMethod1() { Cat firstCat = new Cat(); firstCat.Move(); firstCat.MakeSounds(); Liger oneLiger = new Liger(); oneLiger.MakeSounds(); oneLiger.Move(); }
public void CatTesting() { Animal firstAnimal = new Animal(); Cat firstCat = new Cat(); firstAnimal.Move(); firstCat.Move(); firstCat.MakeSound(); Liger oneLiger = new Liger(); }
public void TestMethod1() { Cat firstCat = new Cat(); firstCat.Move(); firstCat.MakeSounds(); Liger oneLiger = new Liger(); oneLiger.MakeSounds(); oneLiger.Move(); //Animal oneAnimal = new Animal(); //oneAnimal.HasFur = false; }
public void CheckingTypes() { Liger mittens = new Liger(); List <Animal> animals = new List <Animal>(); animals.Add(mittens); animals.Add(new Cat()); animals.Add(new Animal()); foreach (var animal in animals) { animal.Move(); } }
public void TestMethod1() { Cat firstCat = new Cat(); firstCat.Move(); firstCat.MakeSounds(); Liger oneLiger = new Liger(); oneLiger.MakeSounds(); oneLiger.Move(); Animals oneAnimal = new Animals(); //reason you may make a class abstract is to make it so you cannot create instances from that object it. oneAnimal.HasFur = false; }
public void TestMethod1() { Cat captain = new Cat(); captain.Move(); captain.MakeSounds(); Liger oneLiger = new Liger(); oneLiger.MakeSounds(); oneLiger.Move(); //Animal oneAnimal = new Animal(); //oneAnimal.canFly = true; /// Can't make Animal objects now since the class is abstract }
public void CheckingTypes() { Liger mittens = new Liger(); // Make a collection that holds animals List <Animal> animals = new List <Animal>(); animals.Add(mittens); animals.Add(new Cat()); animals.Add(new Animal()); foreach (var animal in animals) { animal.Move(); } }
public void CatTesting() { Animal firstAnimal = new Animal(); Console.WriteLine("Break after animal"); Cat firstCat = new Cat(); firstAnimal.Move(); firstCat.Move(); firstCat.MakeSound(); Liger oneLiger = new Liger(); oneLiger.Move(); oneLiger.MakeSound(); }
public void TestMethod1() { Cat firstCat = new Cat(); firstCat.MakeSound(); firstCat.Move(); Liger firstLiger = new Liger(); firstLiger.MakeSound(); firstLiger.Move(); List <Animal> test = new List <Animal>(); test.Add(firstCat); test.Add(firstLiger); }
public void CatTesting() { Cat firstCat = new Cat(); firstCat.Move(); firstCat.MakeSound(); Liger oneLiger = new Liger(); List <Animal> allAnimals = new List <Animal>(); allAnimals.Add(firstCat); allAnimals.Add(oneLiger); oneLiger.Move(); oneLiger.MakeSound(); //Try making other classes that inherit from animal class }
public void TestMethod1() { Cat firstCat = new Cat(); firstCat.Move(); firstCat.MakeSounds(); Liger oneLiger = new Liger(); oneLiger.MakeSounds(); oneLiger.Move(); //Can't do this if the Animal class is declared as "abstract" // CAN do this if the Animal class isn't declared as "abstract" Animal oneAnimal = new Animal(); oneAnimal.HasFur = false; }
public void CatTesting() { //Animal firstAnimal = new Animal(); Console.WriteLine("Break"); Cat firstCat = new Cat(); Console.WriteLine("Break"); //firstAnimal.Move(); Console.WriteLine("Break"); firstCat.Move(); Console.WriteLine("Break"); firstCat.MakeSound(); Console.WriteLine("Break"); Liger oneLiger = new Liger(); Console.WriteLine("Break"); oneLiger.Move(); Console.WriteLine("Break"); oneLiger.MakeSound(); //Try making other classes that inherit from the animal class }
public void TestMethod1() { Cat firstCat = new Cat(); firstCat.MakeSound(); firstCat.Move(); Liger firstLiger = new Liger(); firstLiger.MakeSound(); firstLiger.Move(); List <Animal> test = new List <Animal>(); test.Add(firstCat); test.Add(firstLiger); Animal oneAnimal = new Animal(); oneAnimal.BreathsAir = false; }
public void TestMethod1() { // ctrl . to connect/reference the Cat to the Cat file // newing up the belwo infomation Cat firstCat = new Cat(); firstCat.MakeSound(); firstCat.Move(); //doing it for liger now referencing and newing up stff Liger firstLiger = new Liger(); firstLiger.MakeSound(); firstLiger.Move(); // connected the list with animial to cat and liger List <Animal> test = new List <Animal>(); test.Add(firstCat); test.Add(firstLiger); }
public void TestMethod1() { Animal firstAnimal = new Animal(); firstAnimal.LegCount = 4; Sloth sloth = new Sloth(); sloth.LegCount = 4; Console.WriteLine(sloth.IsSlow); Cat baseCat = new Cat(); baseCat.MakeSound(); Tabbycat tabby = new Tabbycat(); Liger liger = new Liger(); List <Animal> listOfAnimals = new List <Animal>(); listOfAnimals.Add(baseCat); listOfAnimals.Add(tabby); listOfAnimals.Add(liger); listOfAnimals.Add(sloth); foreach (Animal animal in listOfAnimals) { //if (animal.GetType() == typeof(Cat)) if (animal is Cat) { Cat cat = (Cat)animal; //casting- making a cat into animal,keeping animal features but still be a cat cat.MakeSound(); // break point-to check is the new code works } } }
public void TestMethod1() { Animal firstAnimal = new Animal(); firstAnimal.LegCount = 4; //cannot access sloth props, only its own Sloth sloth = new Sloth(); sloth.LegCount = 4; //CAN access animal props and its own bool value = sloth.IsSlow; //cant declare, must set to variable as it already has a unchanging value Megatherium giantSloth = new Megatherium(); giantSloth.HasTail = false; giantSloth.IsExtinct = true; Cat firstCat = new Cat(); firstCat.MakeSound(); //Meow Liger liger = new Liger(); liger.MakeSound(); //hover shows Liger.MakeSound only, not Cat.MakeSound Cheetah chee = new Cheetah(); chee.MakeSound(); //Purr. //chee is purr here, but not below as a Cat type List <Cat> catList = new List <Cat>(); //works with Animal too, but not Sloths catList.Add(firstCat); catList.Add(liger); catList.Add(chee); //works, happy List <Animal> animalList = new List <Animal>(); //works with cats, and sloths animalList.Add(firstCat); animalList.Add(liger); animalList.Add(chee); animalList.Add(giantSloth); foreach (Animal animal in animalList) { //cant call MakeSound() because not all animals have MakeSound //can only call Animal properties, not children properties } //foreach(Cat animal in animalList) //doesnt work, gets stuck at non-Cats //{ // animal.MakeSound(); //} foreach (Animal animal in animalList) { //if(animal.GetType() == typeof(Cat)) //Must be Cat type! Liger and Cheetah types wont count as Cat! and skips if (animal is Cat) //works with Cat and children of Cat (ie Liger and Cheetah) but not Sloth etc { //casting yay! Cat cat = (Cat)animal; //cant do above if type isnt already Cat (ex. sloths) //if using (animal is Cat), casting still retains Liger as Liger, etc cat.MakeSound(); //so cat meows and liger still roars //Cheetah didnt purr, because it called original MakeSound, not new MakeSound //new made method unrelated to original MakeSound; } } }