Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.97 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace ConsoleApplication2
  9. {
  10. class Animal
  11. {
  12.  
  13. // public : Access is not limited
  14. // protected : Access is limited to the class methods and subclasses
  15. // private : Access is limited to only this classes methods
  16. public double height { get; set; }
  17. public double weight { get; set; }
  18. public string sound { get; set; }
  19.  
  20. // We can either have C# create the getters and setters or create them ourselves to verify data
  21. public string name;
  22. public string Name
  23. {
  24. get { return name; }
  25. set { name = value; }
  26. }
  27.  
  28. // Every object has a default constructor that receives no attributes
  29. // The constructor initializes every object created
  30. // this is used to refer to this objects specific fields since we don't know the objects given name
  31.  
  32. // The default constructor isn't created if you create any other constructor
  33. public Animal()
  34. {
  35. this.height = 0;
  36. this.weight = 0;
  37. this.name = "No Name";
  38. this.sound = "No Sound";
  39.  
  40. numOfAnimals++;
  41. }
  42.  
  43. // You can create custom constructors as well
  44. public Animal(double height, double weight, string name, string sound)
  45. {
  46. this.height = height;
  47. this.weight = weight;
  48. this.name = name;
  49. this.sound = sound;
  50.  
  51. numOfAnimals++;
  52. }
  53.  
  54. // A static fields value is shared by every object of the Animal class
  55. // We declare thinsg static when it doesn't make sense for our object to either have the property or
  56. // the capability to do something (Animals can't count)
  57. static int numOfAnimals = 0;
  58.  
  59. // A static method cannot access non-static class members
  60. public static int getNumOfAnimals()
  61. {
  62. return numOfAnimals;
  63. }
  64.  
  65. // Declare a method
  66. public string toString()
  67. {
  68. return String.Format("{0} is {1} inches tall, weighs {2} lbs and likes to say {3}", name, height, weight, sound);
  69. }
  70.  
  71. // Overloading methods works if you have methods with different attribute data types
  72. // You can give attributes default values
  73. public int getSum(int num1 = 1, int num2 = 1)
  74. {
  75. return num1 + num2;
  76. }
  77.  
  78. public double getSum(double num1, double num2)
  79. {
  80. return num1 + num2;
  81. }
  82.  
  83. static void Main(string[] args)
  84. {
  85. // Create an Animal object and call the constructor
  86. Animal spot = new Animal(15, 10, "Spot", "Woof");
  87.  
  88. // Get object values with the dot operator
  89. Console.WriteLine("{0} says {1}", spot.name, spot.sound);
  90.  
  91. // Calling a static method
  92. Console.WriteLine("Number of Animals " + Animal.getNumOfAnimals());
  93.  
  94. // Calling an object method
  95. Console.WriteLine(spot.toString());
  96.  
  97. Console.WriteLine("3 + 4 = " + spot.getSum(3, 4));
  98.  
  99. // You can assign attributes by name
  100. Console.WriteLine("3.4 + 4.5 = " + spot.getSum(num2: 3.4, num1: 4.5));
  101.  
  102. // You can create objects with an object initializer
  103. Animal grover = new Animal
  104. {
  105. name = "Grover",
  106. height = 16,
  107. weight = 18,
  108. sound = "Grrr"
  109. };
  110.  
  111. Console.WriteLine(grover.toString());
  112.  
  113. // Create a subclass Dog object
  114. Dog spike = new Dog();
  115.  
  116. Console.WriteLine(spike.toString());
  117.  
  118. spike = new Dog(20, 15, "Spike", "Grrr Woof", "Chicken");
  119.  
  120. Console.WriteLine(spike.toString());
  121.  
  122. // One way to implement polymorphism is through an abstract class
  123. Shape rect = new Rectangle(5, 5);
  124. Shape tri = new Triangle(5, 5);
  125. Console.WriteLine("Rect Area " + rect.area());
  126. Console.WriteLine("Trit Area " + tri.area());
  127.  
  128. // Using the overloaded + on 2 Rectangles
  129. Rectangle combRect = new Rectangle(5, 5) + new Rectangle(5, 5);
  130.  
  131. Console.WriteLine("combRect Area = " + combRect.area());
  132.  
  133. // ---------- GENERICS ----------
  134. // With Generics you don't have to specify the data type of an element in a class or method
  135. KeyValue<string, string> superman = new KeyValue<string, string>("","");
  136. superman.key = "Superman";
  137. superman.value = "Clark Kent";
  138. superman.showData();
  139.  
  140. // Now use completely different types
  141. KeyValue<int, string> samsungTV = new KeyValue<int, string>(0, "");
  142. samsungTV.key = 123456;
  143. samsungTV.value = "a 50in Samsung TV";
  144. samsungTV.showData();
  145.  
  146. Console.Write("Hit Enter to Exit");
  147. string exitApp = Console.ReadLine();
  148.  
  149. }
  150. }
  151.  
  152. class Dog : Animal
  153. {
  154. public string favFood { get; set; }
  155.  
  156. // Set the favFood default and then call the Animal super class constructor
  157. public Dog() : base()
  158. {
  159. this.favFood = "No Favorite Food";
  160. }
  161.  
  162. public Dog(double height, double weight, string name, string sound, string favFood) :
  163. base(height, weight, name, sound)
  164. {
  165. this.favFood = favFood;
  166. }
  167.  
  168. // Override methods with the keyword new
  169. new public string toString()
  170. {
  171. return String.Format("{0} is {1} inches tall, weighs {2} lbs, likes to say {3} and eats {4}", name, height, weight, sound, favFood);
  172. }
  173.  
  174. }
  175.  
  176. // Abstract classes define methods that must be defined by derived classes
  177. // You can only inherit one abstract class per class
  178. // You can't instantiate an abstract class
  179. abstract class Shape
  180. {
  181. public abstract double area();
  182.  
  183. // An abstract class can contain complete or default code for methods
  184. public void sayHi()
  185. {
  186. Console.WriteLine("Hello");
  187. }
  188. }
  189.  
  190. // A class can have many interfaces
  191. // An interface can't have concrete code
  192. public interface ShapeItem
  193. {
  194. double area();
  195. }
  196.  
  197. class Rectangle : Shape
  198. {
  199. private double length;
  200. private double width;
  201.  
  202. public Rectangle( double num1, double num2)
  203. {
  204. length = num1;
  205. width = num2;
  206. }
  207.  
  208. public override double area()
  209. {
  210. return length * width;
  211. }
  212.  
  213. // You can redefine many built in operators so that you can define what happens when you
  214. // add to Rectangles
  215. public static Rectangle operator+ (Rectangle rect1, Rectangle rect2)
  216. {
  217. double rectLength = rect1.length + rect2.length;
  218. double rectWidth = rect1.width + rect2.width;
  219.  
  220. return new Rectangle(rectLength, rectWidth);
  221.  
  222. }
  223.  
  224. }
  225.  
  226. class Triangle : Shape
  227. {
  228. private double theBase;
  229. private double height;
  230.  
  231. public Triangle(double num1, double num2)
  232. {
  233. theBase = num1;
  234. height = num2;
  235. }
  236.  
  237. public override double area()
  238. {
  239. return .5 * (theBase * height);
  240. }
  241. }
  242.  
  243. // ---------- GENERIC CLASS ----------
  244.  
  245. class KeyValue<TKey, TValue>
  246. {
  247. public TKey key { get; set; }
  248. public TValue value { get; set; }
  249.  
  250. public KeyValue(TKey k, TValue v)
  251. {
  252. key = k;
  253. value = v;
  254. }
  255.  
  256. public void showData()
  257. {
  258. Console.WriteLine("{0} is {1}", this.key, this.value);
  259. }
  260.  
  261. }
  262.  
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement