瀏覽代碼

(feat) Implement Exercise 3 - Student and Lecturer child classes

main
Rob Hallam 1 年之前
父節點
當前提交
22e9c99217
共有 1 個文件被更改,包括 52 次插入0 次删除
  1. +52
    -0
      week23.java

+ 52
- 0
week23.java 查看文件

@@ -163,4 +163,56 @@ class DemoBankAccount {
}
}

class Student extends Person {
private double gpa;

public double getGpa() {
return gpa;
}

public void setGpa(double gpa) {
this.gpa = gpa;
}

@Override
public String toString() {
return "Student [gpa=" + gpa + ", name=" + this.getName() + "]";
// Ah, we can't access private members of parent classes from the child class
}
}

class Lecturer extends Person {
private double salary;

public double getSalary() {
return salary;
}

public void setSalary(double salary) {
this.salary = salary;
}

@Override
public String toString() {
return "Lecturer [salary=" + salary + ", name=" + this.getName() + "]";
}
}

/**
* Exercise 3.1, 3.2, 3.3 - implement Student and Lcturer classes
*
*/
class DemoStudentLecturer {
public static void main(String[] args) {
Student adrian = new Student();
adrian.setName("Adrian");
adrian.setGpa(3.5);

Lecturer trefusis = new Lecturer();
trefusis.setName("Donald");
trefusis.setSalary(24199);

System.out.println(String.format("At a particular Oxbridge college, we have two people"));
System.out.println(String.format("%s\t\t&\t\t%s", adrian, trefusis));
}
}

Loading…
取消
儲存