In this part of my C# Tutorial we learn about Inheritance, Aggregations, Virtual, Override, Inner Classes, New, Base, Polymorphism and much more.
For the best results print out the code below and take notes as you pause your way through the video. All of the code and a transcript of the video follows below the video.
If you like videos like this consider donating a $1 on Patreon.
[googleplusone]
Code & Transcript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
---------- Animal.cs ---------- using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CSharpTutA.cs { // If this class was sealed you couldn't inherit // from it sealed class Animal class Animal { private string name; // A protected field can be changed by // a subclass directly protected string sound; // Inheritance has a "is-a" relationship, // while an aggregation or delegate // represents a "Has-a" relationship // like we have here with the AnimalIDInfo // object protected AnimalIDInfo animalIDInfo = new AnimalIDInfo(); public void SetAnimalIDInfo(int idNum, string owner) { animalIDInfo.IDNum = idNum; animalIDInfo.Owner = owner; } public void GetAnimalIDInfo() { Console.WriteLine($"{Name} has the ID of {animalIDInfo.IDNum} and is owned by {animalIDInfo.Owner}"); } // Added virtual so that this method can // be overridden by subclasses // You must add override to the method in // the subclass public virtual void MakeSound() { Console.WriteLine($"{Name} says {Sound}"); } public Animal() :this("No Name", "No Sound") { } public Animal(string name) :this(name, "No Sound") { } public Animal(string name, string sound) { Name = name; Sound = sound; } public string Name { get { return name; } set { if (!value.Any(char.IsDigit)) { name = "No Name"; } else { name = value; } } } public string Sound { get { return sound; } set { if(value.Length > 10) { sound = "No Sound"; } sound = value; } } // You can create inner classes that are // normally helper classes for the outer // class because it can access private // members of the outer class public class AnimalHealth { public bool HealthyWeight(double height, double weight) { double calc = height / weight; if ((calc >= .18) && (calc <= .27)) { return true; } else return false; } } } } ---------- Dog.cs ---------- using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CSharpTutA.cs { // When you inherit from another class you // receive all of its fields and methods // You cannot inherit from multiple classes class Dog : Animal { // You can add additional properties // or fields public string Sound2 { get; set; } = "Grrrrr"; // You can overwrite methods by adding new /* public new void MakeSound() { Console.WriteLine($"{Name} says {Sound} and {Sound2}"); } */ // Add override so that the correct method // is called when a Dog is created as an // Animal type public override void MakeSound() { Console.WriteLine($"{Name} says {Sound} and {Sound2}"); } // Create a constructor that has the base // constructor initialize everything except // Sound2 public Dog(string name = "No Name", string sound = "No Sound", string sound2 = "No Sound 2") :base(name, sound){ Sound2 = sound2; } } } ---------- Program.cs ---------- using System; namespace CSharpTutA.cs { class Program { static void Main(string[] args) { Animal whiskers = new Animal() { Name = "Whiskers", Sound = "Meow" }; Dog grover = new Dog() { Name = "Grover", Sound = "Woof", Sound2 = "Grrrrr" }; // Demonstrate changing the protected // field sound grover.Sound = "Wooooof"; whiskers.MakeSound(); grover.MakeSound(); // Define the AnimalIDInfo whiskers.SetAnimalIDInfo(12345, "Sally Smith"); grover.SetAnimalIDInfo(12346, "Paul Brown"); whiskers.GetAnimalIDInfo(); // Test the inner class Animal.AnimalHealth getHealth = new Animal.AnimalHealth(); Console.WriteLine("Is my animal healthy : {0}", getHealth.HealthyWeight(11, 46)); // You can define 2 Animal objects but have // one actually be a Dog type. Animal monkey = new Animal() { Name = "Happy", Sound = "Eeeeee" }; Animal spot = new Dog() { Name = "Spot", Sound = "Wooooff", Sound2 = "Geerrrr" }; // The problem is that if you call a // function in Animal it won't call // the overridden method in Dog unless // the method that might be overridden // is marked virtual spot.MakeSound(); // This is an example of how Polymorphism // allows a subclass to override a method // in the super class and even if the // subclass is defined as the super class // type the correct method will be called Console.ReadLine(); } } } ---------- AnimalIDInfo.cs ---------- using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CSharpTutA.cs { class AnimalIDInfo { public int IDNum { get; set; } = 0; public string Owner { get; set; } = "No Owner"; } } |
Leave a Reply