Ejemplo n.º 1
0
        static void Main()
        {
            /* Objects and Classes*/
            
            Animal dogAnimal = new Animal(); // You create an instance of the Class 'Animal' with the empty constructor
            dogAnimal.Name = "Dog"; //You set the property Name to Dog

            Animal catAnimal = new Animal("Cat"); // You create an instance of the Class 'Animal' with a parameter for Name

            Console.WriteLine("Dog's animal name is " + dogAnimal.Name);
            Console.WriteLine("Cats's animal name is " + catAnimal.Name);

            //Two vars can reference the same Object
            Animal bearOne = new Animal("Bear");
            Animal bearTwo = bearOne;
            Console.WriteLine("bearTwo name is " + bearTwo.Name);

            //One var can reference different Objects (at different time)
            Animal lion = new Animal("Tiger");
            lion = new Animal("Lion");
            Console.WriteLine("lion name is " + lion.Name);

            /* Inheritance*/

            Animal dog = new Dog("Dog");
            Console.WriteLine("Dog's name is " + dog.Name);

            Animal cat = new Cat("Cat");
            Console.WriteLine("Cat's name is " + cat.Name);
            Console.WriteLine("Cat's, which are Animals, breathe like this ");
            cat.Breathe(); //method defined for all Animals 

            Console.ReadLine();
        }
Ejemplo n.º 2
0
        public void PassObjectReferenceByReference()
        {
            Animal animal = new Animal();
            animal.Age = 5;

            SetAgeToTwoByRef(ref animal);

            Assert.IsTrue(animal.Age == 2); 
            Assert.IsFalse(animal.Age == 5);
        }
Ejemplo n.º 3
0
        public void PassObjectReferenceByValue()
        {
            Animal animal = new Animal();
            animal.Age = 5;

            SetAgeToTwo(animal);

            Assert.IsTrue(animal.Age == 5); //Object vars should remain the same
            Assert.IsFalse(animal.Age == 2);
        }
Ejemplo n.º 4
0
        public void PassByValue()
        {
            Animal animal = new Animal();
            animal.Age = 5;
            int animalAge = 5;

            addBirthday(animal, animalAge);

            Assert.IsTrue(animal.Age == 6); //animal variable is a reference, therfore, changes inside the method will alter the Object
            Assert.IsTrue(animalAge == 5);  //animalAge is passed by value, since it is a primitive type, which means we send a copy of the variable, which once it is out of scope,
                                            //it is lost
        }
Ejemplo n.º 5
0
 private void SetAgeToTwoByRef(ref Animal animal)
 {
     animal = new Animal(); //We are changing the reference of the variable animal which is a reference of the object animal
     animal.Age = 2; //we change the value on the object 'animal'
 }
Ejemplo n.º 6
0
 private void SetAgeToTwo(Animal animal)
 {
     animal = new Animal(); //We are changing the reference of the variable 'animal', therfore the original object won't be changed
     animal.Age = 2;
 }
Ejemplo n.º 7
0
 private void addBirthday(Animal animal, int animalAge)
 {
     animal.Age++;
     animalAge++;
 }