<Da**************@gmail.com> a écrit dans le message de news:
11**********************@j73g2000cwa.googlegroups. com...
Darren, it is very important that you learn the correct terminology; this
will really help you in asking for and understanding the help given in these
groups and elsewhere.
| Basically, Canine belongs to Animal.
This is incorrect. Canine doesn't "belong" to Animal, it is said to derive
or inherit from Animal.
Inheritance is described as an "is a" relationship between two classes. In
this case, a Canine *is* an Animal. This implies that a Canine is everything
that an Animal is plus some specialised behaviour; in fact, inheritance is
also known as specialisation.
When designing hierarchies of classes, it is sometimes useful to design from
a "bottom up" viewpoint. IOW, start by saying what ultimate classes you want
to deal with and then look for commonality in those classes which can be
"generalised" into a "superclass".
e.g.
Poodle, Siamese, Dachshund, Wolf, Dingo, Tiger, Lion
Start by inheriting Poodle and Dachshund from DomesticDog, then Wolf and
Dingo from WildDog, then inherit DomesticDog and WildDog from Canine. Now
you can continue by inheritingSiamese from DomesticCat, whilst Tiger and
Lion inherit from WildCat. Both DomesticCat and WildCat can then inherit
from Feline and then Canine and Feline can inherit from Mammal, which can
inherit from Animal.
Of course the above example may be overly fine-grained for the purpose of
the progam and part of good OO design is knowing how much behaviour goes in
what level of the hierarchy as well as how deep the hierarchy should be.
You can also use interfaces to reduce the depth of hierarchies, but that is
best left until you get the hang of class inheritance first :-)
| So, inside Canine, I want to be able to access properties of Animal.
|
| For example:
|
| class Animal
| {
| // Declare the child object (I presume this is correct)
| Canine Canine = new Canine();
|
| public string sName = "Rex";
| }
|
| Class Canine
| {
| Public String myFunc()
| {
| return Animal.Name + " is our returned value"
| }
|
| }
This code also demonstrates that you still haven't quite got the hang of
terminology or how inheritance works.
| // Declare the child object (I presume this is correct)
| Canine Canine = new Canine();
This way of declaring a field of type Canine in the Animal class indicates,
not an "is a" relationship but a "has a" relationship, known as aggregation.
IOW, you are saying that any Animal *has* a Canine inside it; not quite what
you intended :-)
| public string sName = "Rex";
This line will ensure that all instances of the Animal class will have the
Name "Rex".
| return Animal.Name + " is our returned value"
The Name field of the Animal class is only available from instances of that
class, not from the Animal type itself.
If you were not wanting to inherit, then you would have to, at least, place
a field of the Animal type in the Canine class, instantiate it and then use
that instance as a source of the Name property :
class Canine
{
private Animal animal = new Animal();
public Canine()
{
animal.Name = "Canine";
}
public string MyFunc()
{
return animal.Name + " is our returned value"
}
}
Note the difference in case of the Animal type and the animal field. This is
often the only way you will be able to differentiate between classes and
objects which are instances of those classes. You can, of course, adopt a
different naming convention but this is recognised as the "normal" Microsoft
recommended coding standard.
But as you really intended to use inheritance, then you would want to write
your classes in a different way. Note that it is not a good idea to have
public fields; you would normally use private fields and then surface the
value through a public property.
class Animal
{
private string name; // private field
public string Name // public property
{
get { return name; }
set { name = value; }
}
class Canine : Animal
{
public string MyFunc()
{
return Name + " is our returned value"
}
}
Because the Name property is declared as public in Animal, code in derived
classes can see and use it as if it were in the same class. However, because
the name field is declared as private, code in any derived class cannot see
it.
Private visibility means that only code in the declaring class can see it.
Protected visibility means that code in derived classes can see it as well
as the declaring class.
Public visibility means that any code in any class, even those not derived
from the declaring class can see it.
I order to use the above classes, you would have to do something like this :
public void Test()
{
Canine aDog = new Canine();
aDog.Name = "Rex";
string aDogsName = aDog.MyFunc();
// or you could just do :
string aDogsName = aDog.Name;
}
Does this help clear the fog ?
Joanna
--
Joanna Carter [TeamB]
Consultant Software Engineer