类和对象的案例

编写一个学生类,输出学生的相关信息,学生类:姓名,年龄,班级,爱好、显示所有人的信息
public class MyTest {
public static void main(String[] args) {
Student student = new Student();
student.name = "小明";
student.age=17;
student.className = "一班";
student.hobby = "打游戏";

student.show();
}
}

class Student{
// 定义相关的属性 姓名 爱好 班级 年龄
String name;
int age;
String className;
String hobby;

// 定义相关的行为,打印学生信息
public void show(){
System.out.println("Student{" + "name='" + name + '\'' + ", age=" + age + ", className='" + className + '\'' + ", hobby='" + hobby + '\'' + '}');

}

}


编写一个教员类,输出相关的信息
教员类:姓名,专业方向,教授课程,教龄,显示教员个人信息
package com.Student;

public class MyTest02 {
public static void main(String[] args) {
Teacher teacher = new Teacher();
teacher.name = "王老师";
teacher.major = "计算机";
teacher.course = "课程呢个";
teacher.teacherAge = "20";

teacher.show();
}
}

class Teacher{
// 姓名
String name;
// 专业
String major;
// 课程
String course;
// 教龄
String teacherAge;

public void show(){
System.out.println("Teacher{" +
"name='" + name + '\'' +
", major='" + major + '\'' +
", course='" + course + '\'' +
", teacherAge='" + teacherAge + '\'' +
'}');
}
}
定义一个管理类
定义一个管理员类Adminisistrator
定义其相关的属性和方法
定义一个测试类TestAdministrator
创建两个管理员的类对象,并输出他们的信息
public class Adminisistrator {

String username;
String password;


/**
* 判断账号密码是否正确
* @return
*/
public boolean login(){
System.out.println("登录账号");
if ("root".equals(username) && "root".equals(password)){
return true;
}
return false;
}

public void logout(){
System.out.println("推出登录");
}
}
public class TestAdminisistrator {
public static void main(String[] args) {
Adminisistrator adminisistrator = new Adminisistrator();
adminisistrator.username = "root";
adminisistrator.password = "root";
adminisistrator.login();
System.out.println(adminisistrator.toString());

Adminisistrator adminisistrator2 = new Adminisistrator();
adminisistrator2.username = "qwe";
adminisistrator2.password = "213";
adminisistrator.login();
System.out.println(adminisistrator2.toString());

}
}


posted @ 2022-06-14 11:05  我滴妈老弟  阅读(137)  评论(0)    收藏  举报