Java programming question 4

CSDN Q & A 2022-02-13 05:56:04 阅读数:389

java programming question

java

img




Take the answer :
abstract class Stud{ abstract void eat(); abstract void study(); }class Undergraduate extends Stud{ void eat() { System.out.println(" Undergraduates are eating ..."); } void study() { System.out.println(" Undergraduates are learning ..."); }}class Graduate extends Stud{ void eat() { System.out.println(" Graduate students are eating ..."); } void study() { System.out.println(" Graduate students are studying ..."); }}public class Inherit student class { public static void main(String[] args) { Stud s1= new Undergraduate(); s1.eat(); s1.study(); Stud s2 = new Graduate(); s2.eat(); s2.study(); }}


Other answers 2:
public class Person { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public Person(String name) { this.name = name; } public String display () { return " full name :" + this.name; }}
public class Teacher extends Person { private String professionalTitle; public String getProfessionalTitle() { return professionalTitle; } public void setProfessionalTitle(String professionalTitle) { this.professionalTitle = professionalTitle; } public Teacher(String name, String professionalTitle) { super(name); this.professionalTitle = professionalTitle; } public String display () { return " full name :" + super.getName() + "---" + " The title :" + this.professionalTitle; }}
public class Student extends Person { private int studentNumber; public int getStudentNumber() { return studentNumber; } public void setStudentNumber(int studentNumber) { this.studentNumber = studentNumber; } public Student(String name, int studentNumber) { super(name); this.studentNumber = studentNumber; } public String display () { return " Student number :" + this.studentNumber; }}
public class Undergraduate extends Student { private int grade; public int getGrade() { return grade; } public void setGrade(int grade) { this.grade = grade; } public Undergraduate(String name, int studentNumber, int grade) { super(name, studentNumber); this.grade = grade; } public String display () { return " full name :" + super.getName() + "---" + " Student number :" + super.getStudentNumber() + "---" + " grade :" + this.grade; }}
public class Test { public static void main(String[] args) { Person p1 = new Teacher(" Daniel ", " professor "); System.out.println( p1.display() ); Person p2 = new Undergraduate(" Kaidi ", 1, 1); System.out.println( p2.display() ); }}

Copy the code and beautify yourself , This can save some space
Running effect :

img

copyright:author[CSDN Q & A],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/02/202202130556013481.html