package com.itheima;
import java.util.Scanner;
abstract class Person{
void speak(){}
}
class Teacher extends Person{
private String name;
void setName(String name){
this.name = name;
}
String getName(){
return name;
}
void speak(){
System.out.println("这道题怎么写");
}
}
class Student extends Person{
private String name;
void setName(String name)
{
this.name = name;
}
String getName(){
return name;
}
void speak(){
System.out.println("不会");
}
}
public class Main{
public static void main(String[] args)
{
Teacher teacher = new Teacher();
Student student = new Student();
teacher.setName("老王");
Scanner in = new Scanner(System.in);
String choose = in.next();
student.setName(choose);
System.out.println(teacher.getName() + "向"+student.getName()+"提问");
System.out.print(teacher.getName()+"说:");
teacher.speak();
System.out.print(student.getName()+"说:");
student.speak();
}
}
java基础案例4-4学生和老师
最新推荐文章于 2024-09-19 21:47:53 发布
该代码示例展示了Java中抽象类`Person`的使用,派生出`Teacher`和`Student`类。在主函数中,创建了`Teacher`和`Student`对象,并实现了它们之间的简单交互,如设置姓名和提问回答。
摘要由CSDN通过智能技术生成