I imagine a zoo with lots of animals. To deal with their info I organized them somehow.
They all:
Sleep xx hrs / day.
Eat food xx Kg / week.
Some of them:
bellow, cheep, howl or whatever.
are he and some are she.
are covered by feathers, leather, bristles or ...
have names like "Jack", "Tweety", "Pirate" or no name at all.
walk, run, fly or ...
I would like to be able to do, as an example:
Compare some felines with some birds to see how much more food each one eats per month.
For a specific type, let's say siamese cats, to know they gender and names if any.
What type of cow sleeps more hours / month.
That is, I need, in some cases to pass / retrieve numeric data, so some of the classes are not all static.
My comments:
I am not asking for code, of course. It is that I find difficult to organize in my mind kind of a template more or less complete and somehow more complex than most of the examples allow to expect (given online by Microsoft, I mean).
I realized that once I grasp how to organize namespaces and classes, I could walk faster by myself.
I am learning just by reading online. Tutorials put their accent in this or that but it seems that what I am asking here is almost taken for granted.
I have no previous experience with C, C++. Just that old BASIC from 25 years ago and assembler for PIC micros which I write fluently.
My questions:
a) Is this organization reasonable (classes inside classes inside classes), or there is a better way to work with this?
b) Could be that I would need another namespace besides this one only? Maybe two, three or more? (Totally clueless about that!)
c) Where should I locate Main? Inside the class XXXX_catalogue or...?
d) Am I too off subject, maybe?
| Code: |
namespace The_zoo_next_door { class MaybeIncongruent_animals_catalogue { class bovine { class Angus { } class Hereford { } class Holando { } } class feline { class domestic { class siamese { } class maltese { } } class wild { class tiger { } class bobcat { } class cheetah { } } } class birds { class domestic { class chicken { } class goose { } class canary { } } class wild { class robin { } class condor { } class falcon { } } } // Is this the right place? static void Main(string[] args) { } } } |
Jan MontanoPosted Jan 12, 2009, 9:56 PM
b) let's try to leave namespaces for a while. One namespace would do for now. Let's focus more on your classes.
c) if you're doing a console app, c# will take care of the Main part for you.
d) not really. you're doing fine.
The first thing I realized is that we're talking about Animals, and it's just right to create a class called Animals. Then we have 3 types of animals: Bovine, Feline, and Bird. Let's also create classes for those types. Bovine, Feline and Bird are all animals so it makes sense to Inherit from the Animal Class.
*remember. one class per file: Animal.cs, Bovine.cs, Feline.cs, Bird.cs
Here's to get you started:
class Animal
{
private AnimalCategory category;
private string name;
public enum AnimalCategory
{
Domestic = 0,
Wild = 1
} ;
public AnimalCategory Category
{
get { return category; }
set { category = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
}
class Bird : Animal
{
public Bird(AnimalCategory category)
{
this.Category = category;
}
}
class Bovine : Animal
{
}
class Feline : Animal
{
public Feline(AnimalCategory category)
{
this.Category = category;
}
}
Example:
static void Main(string[] args)
{
Bird chicken = new Bird(Animal.AnimalCategory.Domestic);
Bird robin = new Bird(Animal.AnimalCategory.Wild);
Feline Tiger = new Feline(Animal.AnimalCategory.Wild);
robin.Name = "Robinhood";
}
Agustin FerrariPosted Jan 15, 2009, 1:28 AM
It is clear enough now, Jay.
Gracias!
Jan MontanoPosted Jan 13, 2009, 7:57 PM
Sleep = check.
Eat food = check.
bellow, cheep, howl or whatever. ( you could generalize this as an Action and make an enum like Action.Bellow etc.
are he and some are she. (Gender)
are covered by feathers, leather, bristles or ... (Armor? hehe)
walk, run, fly or ... (PrimaryAction ?)
According to your example there should be a class for feline, reptile, bovine or whatever with Animal as a base and that is all the complexitiy of this? Yes.
For Animal type specific members/attributes/methods, specify it in the specific type instead.
Agustin FerrariPosted Jan 13, 2009, 6:15 AM
Hola Jan. Thanks for replying.
Just to be sure I understand you completely: the class Animal should have a member to deal with all this, right: ?
-------------------------------------------------
All of them:
Sleep xx hrs / day.
Eat food xx Kg / week.
Some of them:
bellow, cheep, howl or whatever.
are he and some are she.
are covered by feathers, leather, bristles or ...
have names like "Jack", "Tweety", "Pirate" or no name at all.
walk, run, fly or ...
------------------------------------------------
According to your example there should be a class for feline, reptile, bovine or whatever with Animal as a base and that is all the complexitiy of this?
Thanks for replying.