this post was submitted on 05 Aug 2024
20 points (100.0% liked)
Learn Programming
1625 readers
2 users here now
Posting Etiquette
-
Ask the main part of your question in the title. This should be concise but informative.
-
Provide everything up front. Don't make people fish for more details in the comments. Provide background information and examples.
-
Be present for follow up questions. Don't ask for help and run away. Stick around to answer questions and provide more details.
-
Ask about the problem you're trying to solve. Don't focus too much on debugging your exact solution, as you may be going down the wrong path. Include as much information as you can about what you ultimately are trying to achieve. See more on this here: https://xyproblem.info/
Icon base by Delapouite under CC BY 3.0 with modifications to add a gradient
founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
“Is this a reasonable analogy”
Kind of. That concept is sometimes referred to as the Factory Pattern. You give it some raw materials and it spits out some products.
While in this question it does use a class, classes are more basic than that.
I think of a Class like a blueprint for a car. And each car that is actually built from that blueprint as an Object. The blueprint can have settings, like paint colour, seat fabric and the like.
For the question in the OP, you can imagine you design a form in Word that you print out. The form has 2 blank fields; Question and Answer. This is a Class. You can call it QuestionClass (terrible name, used for clarity)
When you print those forms you are creating an Object. Also known as creating an Instance of the Class. Also known as Instantiating.
Then you can fill in the questions and answers on each sheet/Object.
You can, however, when designing your Class make it so you MUST supply the question and the answer for this instance when you create it, rather than add them later. You give the class some initial values. You call this an Initialiser.
Now take a step back. Make a new function that takes in a list of Question-Answer pairs and spits out a bunch of the QuestionClass Objects.
That’s a big chunk of the work done.