Results 1 to 5 of 5
- 04-04-2011, 04:18 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 6
- Rep Power
- 0
How To Connet Between Two Classes
Hello!!
I Have A Question
I Feel It is Little Stupid :)
But I wanna To Know It
I wanna to know howa to connect between two classes
I have 3 examples Iwanna To know How to do it please :(
First example :
I Have Class Student and Class Courses
And the class Course need some variables of class Student ,How Can I Do that ???!!!
Second Example :
Also If i wanna to make a third class its name is grade class that connect between the course and student classes In other Words I wanna to make an object of class student and courses in the grade class But i wanna This object to be refrence for the both classes not A new one bcz I will Add values to both classes through that class
Third Example:
Last thing If i make class(Student ) and i want to make an array of that class at onther class(Department) What i should do ???
HOpe U Understand me
Im waiting for Ur Help
THnx In Advance
- 04-04-2011, 04:26 PM #2
Member
- Join Date
- Mar 2011
- Posts
- 94
- Rep Power
- 0
First example
Divide and conquer... I'll take the first one.
Define your class Student first. Define the data that is private to Student. Then define public get/set methods that other classes will use to access Student's data. For example:
In class Courses:Java Code:public class Student { private String name; ... public String getName() { return name; } public void setName(String s) { name = s; } ... }
Java Code:public class Courses { ... public void addStudent(Student s) { ... System.out.println("You added: " + s.getName()); } ... }
- 04-04-2011, 04:28 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
Please get rid of that huge font, the silly formatting, the purple subheaders and those stupid smilies; your question reads like a little kid's birthday party invitation.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-04-2011, 07:54 PM #4
Seconded. Also, use proper words.
db
-
Lol, and I have expected the "i"s to be written with love-hearts.
As for your second example, make a package and put all 3 classes in the same package.
Java Code:package school; public class Grade { private java.util.List<Student> studentList = new java.util.ArrayList<Student>(); Grade(Student[] students) { addStudents(students); } Grade(java.util.List<Student> students) { addStudents(students); } public void addStudent(Student s) { studentList.add(s); } public void addStudents(java.util.List<Student> s) { studentList.addAll(s); } public void addStudents(Student[] s) { studentList.addAll(java.util.Arrays.asList(s)); } }
All three are connected because Grade contains Students who have Courses
Similar Threads
-
Help with classes
By Exothesis in forum New To JavaReplies: 11Last Post: 10-11-2010, 02:00 AM -
classes
By exose in forum New To JavaReplies: 2Last Post: 09-30-2010, 04:21 PM -
Cant run my classes
By Assaf A in forum EclipseReplies: 1Last Post: 04-22-2008, 02:31 PM -
Using a JAR from other classes
By Joe2003 in forum Advanced JavaReplies: 1Last Post: 01-02-2008, 07:08 PM -
how to connet to MS SQL in java by using net.sourceforge.jtds.jdbc driver
By cambo in forum JDBCReplies: 0Last Post: 12-18-2007, 06:07 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks