Results 1 to 5 of 5

Thread: Communication between two classes

  1. #1
    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?

  2. #2
    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.

  3. #3
    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.

  4. #4
    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

  5. #5
    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

Similar Threads

  1. What are objects and classes in C++?
    By Tionontati in forum Software Development
    Replies: 3
    Last Post: 28-12-2010, 08:46 AM
  2. What are an Autoloading Classes in PHP?
    By Flaco in forum Software Development
    Replies: 5
    Last Post: 06-03-2010, 06:49 AM
  3. Sealed Classes in C#
    By Reegan in forum Software Development
    Replies: 5
    Last Post: 27-11-2009, 09:16 AM
  4. C # using classes in C + +
    By klite in forum Software Development
    Replies: 3
    Last Post: 01-10-2009, 10:15 AM
  5. Classes in VB.NET
    By Samir_1 in forum Software Development
    Replies: 2
    Last Post: 10-04-2009, 12:07 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,063,789.06800 seconds with 17 queries