The nested constructor "new A(new B(x))" [message #1759725] |
Mon, 17 April 2017 15:20 |
Ricky Robert Messages: 4 Registered: December 2016 |
Junior Member |
|
|
Hello guys...
So I'm currently self-teaching Java and I'm stuck with this tricky technic that my online youtuber "teacher" used, I just can't make any sense of it...
I was doing this programming exercice as the youtuber instructed. It consist of writing a Question Generator program which should display a certain number of "capital game questions", based on a 2-dimensional ArrayList of country names and their capital names, then we formulate a question sentence such as "what's the capital of the following country: France." and the user enters his answer etc etc.
I did it in a very simple manner with only 2 classes. Then I checked how this youtuber did and I am stuck with his version ever since. In his simple version he got 5 classes, he differentiated GUIquiz and ConsoleQuiz in order to display messages in a GUI window or in the console, an abstract class and a QG (QuestionGenerator) class. If I simplify the whole package it looks like this (assume that we want it to display in GUI windows):
Class Test {
____main(...) {
________GUIquiz quiz = new GUIquiz ( new QG(2));
____}
}
Class GUIquiz extends QuizAbs {
____public Guiquiz ( QG g ) {
________super ( g );
____}
}
Class ConsoleQuiz extends QuizAbs {...} //(almost the same as GUIquiz)
Class QuizAbs {
____private QG g;
____public QuizAbs (QG g ) {
________this g = g;
____}
}
Class QG {
____private int nb_q; // stands for number of questions
____public QG ( int nb_q ) {
________this nb_q = nb_q;
____}
}
I can't understand the content in this main() method, I mean if it was just A a = new A( ), no problem, but here...
- So how many objects does the line in the main() generate in fact? at least 3 I guess?
- So the program first instantiates an object of QG and passes the value 2 to the nb_q variable in QG class, then instantiates an object of GUIquiz...? -
- But in GUIquiz its constructor seems to have a variable of the QG class? How's that happening? I mean GUIquiz inherits from QuizAbs but not from QG right ? o_0 !!!
- QG g means that this g variable can have any property and access to any method in QG class right?
I know my description may be long but I'm so disturbed by this exercice I cannot progress if I leave it behind...
Thank you very much in advance for your enlightenments! Have a good day fellows
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.09626 seconds