Re: Beginner interface question
- From: "^MisterJingo^" <misterjingo@xxxxxxxxx>
- Date: 12 Dec 2005 08:51:14 -0800
Thanks for the replies all, it makes more sense now. I thought it might
be a bit 'fiddly' casting to the interface type to call certain
methods, but after playing around, I found out this:
public interface IShape
{
double area();
double circumference();
int sides();
void blah();
}
public class Oblong :IShape
{
public int side;
public double area() { return (double)((side * side) * 2); }
public double circumference() { return ((double)(6 * side)); }
public int sides() { return 4; }
public Oblong() { side = 0;}
void IShape.blah(){ Console.WriteLine("Test"); }
}
public class Shape
{
public static void Main(string[] args)
{
Oblong myOb = new Oblong();
myOb.side = 5;
Console.WriteLine("Displaying Info:");
displayInfo(myOb);
}
static void displayInfo(IShape myShape)
{
Console.WriteLine("Area: {0}", myShape.area());
Console.WriteLine("Sides: {0}", myShape.sides());
Console.WriteLine("Circumference: {0}",
myShape.circumference());
myShape.blah();
}
}
using a method taking the interface type, I can perhaps see uses for
that.
A quick question:
If I had a number of objects which were not derived from the same base
class. Each of these objects had a load and save method, which were
called on every instanced object when the user selected a save event.
Would it be best to create a load/save interface, and then use a method
such as above. Or would it be best to use a function which calls every
object in turn, decides what form the object is and then load/save. Or
even factoring all objects which need load and save functionality into
a common base class? (the load or save could be something like
drawToScreen etc).
I'm trying to get my head around when best to use interfaces etc.
Thanks,
Chris
.
- Follow-Ups:
- Re: Beginner interface question
- From: Jeff Louie
- Re: Beginner interface question
- From: Jeff Louie
- Re: Beginner interface question
- References:
- Beginner interface question
- From: ^MisterJingo^
- Beginner interface question
- Prev by Date: Re: How to set DataGridTextBox.TabStop ?
- Next by Date: Re: DataGrid loading password protected MSAccess DB
- Previous by thread: Re: Beginner interface question
- Next by thread: Re: Beginner interface question
- Index(es):
Relevant Pages
|