Person类中有什么属性。
public class Person{
String name;
int age;
public Person(){}
public Person(String name,int age){
this.name=name;
this.age=age;
}
}
public class Student extends Person{
int classNum;
public Student(String name,int age,int classNum){
this.name=name;
this.age=age;
this.classNum=classNum;
}
public void info(){
System.out.println(name +"是学生,"+age+"岁,这学期共选了"+classNum+"门课");
}
}
public class Teacher extends Person{
String cName;
public Teacher (String name,int age,String cName){
this.name=name;
this.age=age;
this.cName=cName;
}
public void info(){
System.out.println(name +"是teache,"+age+"岁,教"+cName+"课");
}
}
import java.util.Scanner;
public class Main{
public static void main(String []args){
Scanner in=new Scanner(System.in);
String type=in.next();
if("Teacher".equals(type)){
Teacher teacher=new Teacher(in.next(),in.nextInt(),in.next());
teacher.info();
}else if("Student".equals(type)){
Student student=new Student(in.next(),in.nextInt(),in.nextInt());
student.info();
}
}
}