Click to See Complete Forum and Search --> : Has,Contains,Is a part of(UML desingning in C#)
mehdi62b
March 8th, 2005, 08:58 AM
Hi Experts,
I want to implement HAS in C#,I think I should use private nested class for this relation,or whetere other things are possible?
for example a car Has door,motor,pedal,...
public class car
{
private Motor motor=null;
class Motor{
}
private Pedal pedal=null;
class Pedal{
}
private Door door=null;
class Door{
}
}
am I right?
if I declare Door,Pedal,Motor classes outside the car class,does it make sence?
cilu
March 8th, 2005, 09:08 AM
if I declare Door,Pedal,Motor classes outside the car class,does it make sence?
Let's take the motor for example: can you use it for something else than a car? For instance a byke has also a motor, so perhaps you can reuse the Motor class. You can provide a generic Moror class (perhaps an interface) and derive/implemlent it in two specialized class, CarMotor and BykeMotor. In this case, yes, it makes a lot of sense to put it outside.
You can apply the same reason to the rest of the car's parts.
May I ask what data will the Pedal or Door encapsulate? What fields/methods/properties?
mehdi62b
March 8th, 2005, 10:25 AM
abstract class Motor{
}
public class car
{
private CarMotor motor=null;
class CarMotor:Motor{
}
private Pedal pedal=null;
class Pedal{
}
private Door door=null;
class Door{
}
}
yes,I just wanted to know for Has we should declare them as nested classes or It is not important that much?
May I ask what data will the Pedal or Door encapsulate? What fields/methods/properties?I summarized my example
boudino
March 9th, 2005, 02:07 AM
I think - you should not use nested classes for this.
1.) Not all modeling concepts must have its counterpart in programing language.
2.) Nested class breaks encapsulation, because it can access private members of its outer class. In your case: pedals will able to manipulate the whole car, althought they should be able to operate only breaking system.
mehdi62b
March 9th, 2005, 10:17 AM
I learnt many things about nested classes
thanks friends.
cilu
March 9th, 2005, 10:18 AM
Nested class breaks encapsulation, because it can access private members of its outer class. In your case: pedals will able to manipulate the whole car, althought they should be able to operate only breaking system.
This is a very good point.
I summarized my example
I know that. I was currios what functionality do you want to give to Pedal, for example. If it isn't too much, perhaps it doesn't worth creating another class. But that depends on details you have not provided.
mehdi62b
March 10th, 2005, 01:24 AM
Nested class breaks encapsulation, because it can access private members of its outer class. In your case: pedals will able to manipulate the whole car, althought they should be able to operate only breaking system.I have a question here,I think nested classes just can access static private memebers of its outer class(not non satatic because we should instansiate it)
this is my code,
abstract class Motor
{
}
abstract class Pedal
{
}
public class car
{
private CarMotor motor=null;
class CarMotor:Motor
{
int speed=0;
public int Speed
{
get
{
return speed;
}
}
public void IncreaseSpeed(int a)
{
speed+=a;
}
}
private GasPedal pedal=null;
class GasPedal:Pedal
{
bool released;
public bool Released
{
get
{
return released;
}
set
{
released=value;
}
}
}
private Door door=null;
class Door
{
}
public void PushPedal(int a)
{
pedal.Released=false;
motor.IncreaseSpeed(a);
}
}
I have a PushPedal method in car class,it was my designing,ofcource I could declare those nested classes outside the car class but because I just want to let car class use CarMotor class I used nested classes.
I dont know it was a good idea or not?
boudino
March 10th, 2005, 03:02 AM
I have a question here,I think nested classes just can access static private memebers of its outer class(not non satatic because we should instansiate it)
You are wrong. You can access private instance members of outer class:
public class car
{
private Door door=null;
class Door
{
public void SetTo(car aCar)
{
aCar.door = this;
}
}
}
[QUOTE=mehdi62b]I have a PushPedal method in car class,it was my designing,ofcource I could declare those nested classes outside the car class but because I just want to let car class use CarMotor class I used nested classes.
I dont know it was a good idea or not?[/QOUTE]
I am sorry, but I don't sitll undestrood why do you need nested classes. Having no nested classes, car class will still be able to use CarMotor.
mehdi62b
March 10th, 2005, 04:27 AM
:thumb: I thought you meant something like this,
public class car
{
private Door door=null;
class Door
{
car c=new car();
//other methods
}
}
this will give StackOverFlow exception (but inside a method or if my memebr is satic,no problem)
about nested classes because I dont want to let other classes in my assembelly have an access CarMotor,just car class should access CarMotor.
mehdi62b
March 10th, 2005, 08:17 AM
boudino,
you were right!
:wave:
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.