假设有下面接口的定义,编写类实现这个接口,并使用该接口实现多态。

2024-12-18 11:59:14
推荐回答(3个)
回答1:

interface IStudent {
void DoHomework();//做作业
}

class Student1 implements IStudent {
public void DoHomework(){
System.out.println("中学生学习");
}
}

class Student2 implements IStudent {
public void DoHomework(){
System.out.println("大学生学习");
}
}

public class TestStudy {
public static void main(String[]args) {
IStudent s1 = new Student1();
IStudent s2 = new Student2();
s1.DoHomework();
s2.DoHomework();
}
}

我要分 哈哈

回答2:

public class Student implements IStudent
{
public void DoHomework();
}

大概是这样的,部分关键字记不准了

回答3:

#include
#include
using namespace std;
class student
{
private:
string sno;
string sname;
public:
student(string sno,string sname)
{
this -> sno = sno;
this -> sname = sname;
}
virtual void DoHomework()
{
cout<<"do himself's homework"< }
virtual void printstudent()
{
cout<<"this is a base for class student"< cout< }
};

class m_student:public student
{
private:
string schoolname;
public:
m_student(string sno,string sname,string schoolname):student(sno,sname)
{
this -> schoolname = schoolname;
}
void DoHomework()
{
cout<<"do my English homework"< cout<<"do my Mathmetic homework"< }
void printstudent()
{
cout<<"this is a middle school student"< cout< }
};

class c_student:public student
{
private:
string schoolname1;
public:
c_student(string sno,string sname,string schoolname1):student(sno,sname)
{
this -> schoolname1 = schoolname1;
}
void DoHomework()
{
cout<<"do my English homework"< cout<<"do my Major homework"< }
void printstudent()
{
cout<<"this is a college student"< cout< }
};

void main()
{
student student1("15","shane");
student1.DoHomework();
student1.printstudent();
m_student student2("15","nicky","xx一中");
student2.DoHomework();
student2.printstudent();
c_student student3("16","bryan","XX大学");
student3.DoHomework();
student3.printstudent();
}