OOAD: Experimental lecture 6
Advanced GUI
Name :Li Hongfu
Student no: 201219010145
Date: December 10 2014
Table of Contents
Objectives:------------------------------------------------------------3
Methods:--------------------------------------------------------------3
Results:----------------------------------------------------------------3
Exercise 1:----------------------------------------------------------------------3
Exercise 2:----------------------------------------------------------------------3
Exercise 3:----------------------------------------------------------------------4
Exercise 4:----------------------------------------------------------------------4
Code--------------------------------------------------------------------4
Exercise 1:----------------------------------------------------------------------4
Exercise 2:----------------------------------------------------------------------5
Exercise 3:----------------------------------------------------------------------5
Exercise 4:----------------------------------------------------------------------6
Picture------------------------------------------------------------------6
picture 1:------------------------------------------------------------------------6
picture 2:------------------------------------------------------------------------7
picture 3:------------------------------------------------------------------------7
picture 4:------------------------------------------------------------------------7
Conclusions:---------------------------------------------------------8
A.Objectives:
1. Learning the abstract function.
2. Getting familiar with the super-class and subclass.
3. Learn about the usage of abstract.
4. Getting learned about the override.
B. Methods:
There is the following part that is needed to understand exercise:
1. Abstract class
2. Virtual function
3. Override function
4. Create new object
C.Result:
Exercise 1
Firstly, I created a abstract animal as the super class, it’s basically a abstract class. And after that, I created two methods Eatfood and speak in the animal class. To inherit the abstract animal, I created two subclass named cow and chicken.
Public class cow:animal
The colon represents inevitability.
Public void virtual Eatfood()
Void means no value will be returned.
Exercise 2
Created the main function, and then crated the new objects. And when you want to invoke the method, just write like this way:myanimal.Eatfood();
New objects:
Animal myanimal=new Cow();
Cow mycow=new Cow();
The result is in the appendix.
Exercise 3
In this exercise, you can see that an abstract class can’t be instantiated directly; to use it we need to inherit from it. We can just see the result in appendix.
Exercise4:
In this exercise, I add some methods for chicken and animal.
Public void override Eatfood()
{ Console.Writeline(‘Chicken eats stone”);
Console.Writeline(‘Chicken speak Hello”);}
The whole code:
Exercise 1:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MyFirstABstract
{
public abstract class Animal
{
public virtual void EatFood()
{
Console.WriteLine("Animal eat");
}
public virtual void Speak()
{
Console.WriteLine("Animal speak");
}
}
public class Cow : Animal
{
public override void EatFood()
{
Console.WriteLine("Cow eats");
}
}
public class Chinken : Animal
{ }
}
Exercise 2 :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MyFirstABstract
{
public abstract class Animal
{
public virtual void EatFood()
{
Console.WriteLine("Animal eat");
}
public virtual void Speak()
{
Console.WriteLine("Animal speak");
}
}
public class Cow:Animal
{
public override void EatFood()
{
Console.WriteLine("Cow eats");
}
}
public class Chinken: Animal
{ }
class Program
{
static void Main(string[] args)
{
Animal myAnimal = new Cow();
Cow myCow = new Cow();
myAnimal.EatFood();
myCow.EatFood();
myCow.Speak();
Console.ReadKey();
}
}
}
Exercise 3:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MyFirstABstract
{
public abstract class Animal
{
public virtual void EatFood()
{
Console.WriteLine("Animal eat");
}
public virtual void Speak()
{
Console.WriteLine("Animal speak");
}
}
public class Cow:Animal
{
public override void EatFood()
{
Console.WriteLine("Cow eats");
}
}
public class Chinken: Animal
{ }
class Program
{
static void Main(string[] args)
{
Animal myAnimal = new Animal();
Cow myCow = new Cow();
myAnimal.EatFood();
myCow.EatFood();
myCow.Speak();
Console.ReadKey();
}
}
}
Exercise 4
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MyFirstABstract
{
public abstract class Animal
{
public virtual void EatFood()
{
Console.WriteLine("Animal speak");
Console.WriteLine("\n");
}
public virtual void Speak()
{
Console.WriteLine("Animal speak");
Console.WriteLine("\n");
}
}
public class Cow:Animal
{
public override void EatFood()
{
string Food1;
Console.WriteLine("Please input Cow eat :");
Food1 = Console.ReadLine();
Console.WriteLine("Cowsd eat :\t" + Food1);
Console.WriteLine("\n");
}
}
public class Chinken: Animal
{
public void Eat()
{
string Food2;
Console.WriteLine("Please input Animal eat :");
Food2 = Console.ReadLine();
Console.WriteLine("Animal eat :\t" + Food2);
Console.WriteLine("\n");
}
}
class Program
{
static void Main(string[] args)
{
Animal myAnimal = new Cow();
Cow myCow = new Cow();
Chinken myChinken = new Chinken();
myAnimal.EatFood();
myCow.EatFood();
myCow.Speak();
myChinken.Eat();
Console.ReadKey();
}
}
}
Picture 1
Picture 2
Picture 3
Picture 4
D.Conclude
1. An abstract class can’t be instantiated directly; to use it we need to inherit from it.
2. Abstract classes can have abstract members, which have no implementation in the base class, so an implementation must be supplied in the derived class.
3. Child classes can override members that are defined as virtual in a parent class.
4. Child classes cannot access private members in its parent, but it is possible to define protected members that are available only within a class or classes that derive from that class.