Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You're still missing the essential point: a <em>class</em> associates the data <em>with its methods.</em> So the method that sets Jim's teacher is part of the Student class, as is the method that sets Jim's grade.</p> <p>Think of it in natural language first. "Jim is a student; students have names, stident ID numbers, and classes. Classes have teachers and classrooms. Students receive grades for each class at the end of the term."</p> <p>So we have a class Student. Student, when you create an object from it, needs the students name and student ID, so the "constructor" needs to be something with a name string and a student ID -- call it a String too -- to initialize it. I don't know C#, so I'll make a pseudocde with braces...</p> <pre><code>class Student { String name; String studentID; Course crsList = List&lt;Course&gt;; // "list of courses." Student(String name, String studentID ){ // Make a new student, the "constructor" this.name = name; this.studentID = studentID; } addCourse(Course c){ // Add a course to the student's list this.crsList.append(c); // Adds c to end of crsList } } </code></pre> <p>This says "A Student has a name and ID, and we can add classes to their collection of classes."</p> <pre><code>class Teacher { String name; Course crsList = List&lt;Course&gt;; Teacher(String name){ this.name = name; } // ... more methods here. } </code></pre> <p>But we observe that teachers and students are both kinds of people, so we "refactor" this to put the common piece together</p> <pre><code>class Person { String name; Person(String name){ this.name = name; } // ... more methods } </code></pre> <p>Now we change Teacher and Student so we say</p> <pre><code>class Student : Person { String studentID; Student(String name, String studentID){ // Now we need to initialize the Person part super(name); // and the special Student part this.studentID = studentID; } } </code></pre> <p>which now says "a Student is a kind of Person who has an additional student ID, and who we can add classes to."</p> <p>You'd go on to define "addClass" for Teach, <em>but as part of the Teacher class</em>. Why? Because adding a class for a teacher means something different than adding one for a student. You'd add a method to Student that takes a Course and a Grade, finds the Course and assigns he grade somehow. (Pop quiz: is a Grade part of a Course or part of a Student?)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload