Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 04-12-2009
Member
 
Join Date: Jun 2009
Posts: 59
Communication between two classes

I have a little problem of structure. Basic but apparently I must be half asleep I see no direct solution. I want to communicate two classes without including one inside the other. A simple example with a class A1 and A2 are each derived from A (which is not required but it goes in my example).
Code:
A is a class "Person"
Code:
A1 is a "character" and has an attribute and Watch a method getTime ()
Code:
A2 is a "character" and has an attribute and a method Agenda getAppointment (DateTime time)
A1 and A2 can not do anything without each other (one has the time and other planning), they must be able to talk. Include attribute A1 of A2 (or vice versa) seems to me quite inconsistent, they are two distinct classes, there is no hierarchy. How?
Reply With Quote
  #2  
Old 04-12-2009
Member
 
Join Date: Nov 2005
Posts: 1,323
Re: Communication between two classes

If you do not want to include each other, you just run the input.
Code:
protected void MyFunction () {

    A1 _class1 = new A1 ();
    A2 _class2 = new A2 ();

    Datetime _time = _class1 . getTime ( ) ;
    Datetime _date = _class2 . getAppointment ( ) ;
}
After I do not know how you plan to use such information, but this solution requires the creation of accessor methods or public if there is no inheritance.

OR even establish static method. But attention to static, it can be treacherous if you do not master this modifier.
Reply With Quote
  #3  
Old 04-12-2009
Member
 
Join Date: Jun 2009
Posts: 59
Re: Communication between two classes

It's just what I'm going to end up. It still bothers me a perspective structure. After all, there is no superior to A1 and A2, which allows them to coordinate, they should be able to do naturally.

Or rather to create a class Telephone, which enables communication. Structure and a simulated protocol com. Well, it'd be a lot of bother for not much. I'll stay in a superior solution coordinator.

The static does not serve me much good, the method getTime () depends on the Watch attribute that can change according to People.
Reply With Quote
  #4  
Old 04-12-2009
Member
 
Join Date: Nov 2005
Posts: 1,323
Re: Communication between two classes

And have you thought about polymorphism? Through the abstract character class and your class A1 and implement the method getTime () and A Method getAppointment (). It may be a solution I just thought there
Reply With Quote
  #5  
Old 04-12-2009
Member
 
Join Date: Nov 2008
Posts: 1,185
Re: Communication between two classes

I'm not sure I understand what you want to do, but I'll still try to offer you some work. If A1 and A2 can not do anything without each other, it does not seem inconsistent to add each of these classes a reference to an object of another class form of property for example:
Code:
class A1
{
     public A2 RefA2 { get ; set ; }
     ...
}

class A2
{
     public A1 RefA1 { get ; set ; }
     ...
}
and when you create instances, you are connected to:
Code:
A1 aA1 = new A1 ();
A2 aA2 = new A2 ();

aA1. RefA2 = aA2;
aA2. RefA1 = aA1;
in your class A1, you can now call public functions (or features protected) object A2, and vice versa

Well, ok, the names "RefA1" and "RefA2" are not top, but good, "A1" and "A2" is not great either speaking
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Communication between two classes"
Thread Thread Starter Forum Replies Last Post
What are objects and classes in C++? Tionontati Software Development 3 28-12-2010 08:46 AM
What are an Autoloading Classes in PHP? Flaco Software Development 5 06-03-2010 06:49 AM
Sealed Classes in C# Reegan Software Development 5 27-11-2009 09:16 AM
C # using classes in C + + klite Software Development 3 01-10-2009 10:15 AM
Classes in VB.NET Samir_1 Software Development 2 10-04-2009 12:07 AM


All times are GMT +5.5. The time now is 11:23 AM.