נתון פרויקט שהוגדרו בו שלוש מחלקות Employee, Manager, Intern ומחלקה Driver, כך:
נתון פרויקט שהוגדרו בו שלוש מחלקות Employee, Manager, Intern ומחלקה Driver, כך:
public class Employee {
private int seniority; // ותק
public Employee(int seniority) {
this.seniority = seniority;
}
public int GetSeniority() {
return this.seniority;
}
public virtual bool Check(Employee e) {
Console.WriteLine("EmpCheck");
return this.seniority > e.seniority;
}
}
public class Manager : Employee {
public Manager(int seniority): base(seniority) {
}
public bool Check(Manager m) {
Console.WriteLine("MgrCheck");
return this.GetSeniority() == m.GetSeniority();
}
}
public class Intern : Employee {
private int mentorId;
public Intern(int seniority, int mentorId): base(seniority){
this.mentorId = mentorId;
}
public override bool Check(Employee e) {
Console.WriteLine("IntCheck");
if (!(e is Intern)) {
return true;
}
Intern other = (Intern) e;
return this.mentorId == other.mentorId;
}
}
public class Driver
{
public static void Main(string[] args)
{
Employee e1 = new Manager(15);
Manager m1 = new Manager(15);
Employee e2 = new Employee(10);
Employee i1 = new Intern(2, 800);
Intern i2 = new Intern(2, 900);
Intern i3 = new Intern(5, 800);
(1) Console.WriteLine(e1.Check(m1));
(2) Console.WriteLine(m1.Check(e1));
(3) Console.WriteLine(m1.Check(m1));
(4) Console.WriteLine(i1.Check(e2));
(5) Console.WriteLine(i1.Check(i2));
(6) Console.WriteLine(i1.Check(i3));
(7) Console.WriteLine(e2.Check(i1));
}
}
סעיף א
עבור כל אחת מהפקודות 1–7, כתבו מה יהיה הפלט של הפקודה.
סעיף ב
נתונה הפקודה: Console.WriteLine(i1.Check(XXX) כאשר XXX הוא משתנה שהוגדר בפעולה הראשית main במחלקה Driver.
כתבו את כל המשתנים שאפשר לשים במקום XXX כדי שלאחר ביצוע הפקודה יודפס הפלט "IntCheck false".
שאלות ותגובות על השאלה
🎓 לא הבנתם משהו? קבלו הסבר נוסף ממרצה לתכנות
שאלו כאן — ותקבלו מענה מוסמך.