Results 1 to 4 of 4

Thread: Use classes inside of other classes

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2011
    Posts
    57

    Use classes inside of other classes

    Hello forumers ! I've got a problem.

    The "exercice":

    1 class called "Person"
    name, last name and age of every person, with their getters and setters.
    1 class called "Codes"
    int code, String rank. Here I will put the codes for every person.
    1 main class called "Simulator"
    Instanciate new persons and show system.out.print messages.

    What I need is to instanciate "Codes" inside of "Person" (Like that: private Codes code and use it in the Simulator. For example:

    CLASS PERSON
    private String name = "";
    private int age = 0;
    private String lastname = "";
    private Codes code;

    [... getters ... setters ...]

    CLASS CODES
    private int code = 0;
    public void setCode(int c){
    code = c;
    }

    public int getCode(){
    return code;
    }

    CLASS SIMULATOR MAIN
    person p1 = new person();
    p1.setName("John");
    p1.setLastName("Petrucci");
    p1.setEdad(43);
    p1.Codes.code ?????

    System.out.println("Name: "+p1.getName()+" "+p1.getLastName()+" and the age is: "+p1.getAge());
    System.out.println("The code is: "p1.Codes.code?????" );


    Don't really know how to use classes inside another classes that aren't the main class.

    Please help !!

  2. #2
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Use classes inside of other classes

    You could do Inheritance


    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  3. #3
    Fanatic Member coolcurrent4u's Avatar
    Join Date
    Apr 2008
    Location
    *****
    Posts
    993

    Re: Use classes inside of other classes

    is it really necessary to use nested classes? another option is to use a package
    Programming is all about good logic. Spend more time here


    (Generate pronounceable password) (Generate random number c#) (Filter array with another array)

  4. #4
    Junior Member
    Join Date
    Jan 2010
    Posts
    20

    Re: Use classes inside of other classes

    Well, I'll hope you're still watching the thread -- I know it has some age on it.

    Your pseudo-code indicates that a Person object has a variable named 'code'; a standard way to access Code, then, is to include getCode() and setCode(Codes) methods in Person. Since your example is so simple, the result looks a bit odd; you end up with personA.getCode().getCode() to get the specific code for that person; presumably you would also be able to put personA.getCode().getRank() to get the rank for the person.

    All of this looks a little odd, perhaps because I don't know what the Codes class represents. If it only represents one Code, then I recommend dropping the 's' from its name. If the code and the rank always go together, then there are ways of storing just the code in the Person object and looking up the rank when you need it. But whether that's appropriate in your situation depends on things I don't know at this point.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width