Home > Back-end >  Don't ask a great god
Don't ask a great god

Time:12-30



Object-oriented assignment:

New a Person class, defined as an abstract class, contains the name: the name, age: the age, sex, sex, telephone: tel, occupation: pro properties, eat eat (), sleep the sleep () method -- a new Student class Student inherit the Person class, in addition to add professional major, class the className attribute, as well as learning study () method;

Override the toString method, the output of information of students;

New - the Teacher class inherit the Person class, in addition to add called the () method, and realize the curriculum Course interface;

Course interface: create access Course name, the method of the incoming Course parameters, output courses

CodePudding user response:


 
Public abstract class Person {

private String name;
private int age;
private String sex;
Private String tel;
Private String pro;

Public void eat () {
System. The out. Println (" eat ");
}

Public void sleep () {
System. The out. Println (" sleep ");
}

Public String getName () {
return name;
}

Public void elegantly-named setName (String name) {
this.name=name;
}

Public int getAge () {
Return the age;
}

Public void setAge (int age) {
this.age=age;
}

Public String getSex () {
Return sex;
}

Public void setSex (String sex) {
This. Sex=sex;
}

Public String getTel () {
Return tel;
}

Public void setTel (String tel) {
This. Tel=tel;
}

Public String getPro () {
Return pro;
}

Public void setPro (String pro) {
This. Pro=pro;
}

@ Override
Public String toString () {
Return "Person {+"
"Name='" + name +' \ '+'
"Age=" + age +
='" + ", sex sex +' \ ' '+
", tel='" + tel +' \ ' '+
", pro='" + pro +' \ ' '+
'} ';
}
}




Public class Student extends the Person {

Private String major;
Private String className.

Public void study () {
System. The out. Println (" study ");
}

Public String getMajor () {
The return of major;
}

Public void setMajor (String) major {
This. Major=major;
}

Public String getClassName () {
Return the className.
}

Public void setClassName (String className) {
Enclosing the className=className.
}

@ Override
Public String toString () {
Return "Student {+"
"Major='" + major +' \ '+'
", the className='" + className +' \ ' '+
"} "+ super. ToString ();
}
}



Public class the Teacher extends the Person implements Course {

Private String subject;


Public void called the () {
System. The out. Println (" called ");
}


@ Override
Public String getSubjectName (String name) {
Return this. The subject;
}
}



Public interface Course {


String getSubjectName (String name);

}

  • Related