学生类
import java.util.Scanner;
public class Student {
String name = "张三";
int age = 16;
String grade = "三年二班" ;
String fancy = "打篮球 唱歌 读书 冒险";
public void introduce(){
System.out.println("-----------------------------------------------------");
System.out.println("姓名:" + name + "\n年龄:" + age + "\n班级:" + grade);
System.out.println("兴趣爱好:" + fancy);
}
public void init(){
Scanner sc = new Scanner(System.in);
System.out.print("请输入姓名:");
name = sc.nextLine();
System.out.print("请输入年龄:");
age = sc.nextInt();
System.out.print("请输入班级:");
grade = sc.next();
System.out.print("请输入兴趣爱好:");
fancy = sc.next();
}
public static void main(String[] args){
Student student = new Student();
student.init();
student.introduce();
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/def52da552dabb1de4b158955b2aea99.png)
教师类,输出教师相关信息
import java.util.Scanner;
public class Teacher {
String name = "李四";
int age = 10;
String major = "计算机视觉" ;
String course = "计算机网络 操作系统 数据结构 数据库";
public void introduce(){
System.out.println("-----------------------------------------------------");
System.out.println("姓名:" + name + "\n教龄:" + age + "年\n专业方向:" + major);
System.out.println("教授课程:" + course);
}
public void init(){
Scanner sc = new Scanner(System.in);
System.out.print("请输入姓名:");
name = sc.nextLine();
System.out.print("请输入教龄:");
age = sc.nextInt();
System.out.print("请输入专业方向:");
major = sc.next();
System.out.print("请输入教授课程:");
course = sc.next();
}
public static void main(String[] args){
Teacher teacher = new Teacher();
teacher.introduce();
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/f41f3d2b1432dc845f558e307ba3bddc.png)