其他分享
首页 > 其他分享> > 2022.07.23 王茜 学习笔记

2022.07.23 王茜 学习笔记

作者:互联网

   final关键字  

* public公有的
* protected受保护的
* 默认的,友好的
* private私有的
*final关键字
* final可以修饰的结构:
* 1、类      public final class
*           最终类,终级类
* 2、属性    private  final   String  name
*          常量,不能改变
*          常量的命名规则:单词的所有字母大写,如果是多个单词,中间用下划线分割
*          常量不能只声明不赋值
* 3、方法    private  final    void   show()
*          不能重写
*

   Object:最顶级父类   

祖先类:Object(最顶级父类)
*  如果一个类没有明确的写出它的父类是谁,那它的父类就是Object
*  
*  Object类中有11个方法
*  1.hashCode();它的返回值实际上就是对象运行时的内存地址。
*      hash算法:一般翻译“散列”,把任意长度的输入,通过一个散列算法变换成固定长度的输出,输出结果就是散列值。
*      密码加密MD5加密
*      SHA家族
*  2.equals(),和==没区别,比地址。为了让子类重写。
*  3.toString(),转换成字符串。当我们直接使用对象时,会默认调用toString()方法。
*              为了让子类重写
*  4.finalize(),垃圾回收的方法
*  5.clone(),克隆,必须实现Cloneable接口

今天我们学习了final关键字和Object的使用方法,为了巩固练习,我们做了下面这道题:

信息录入:
 录入老师信息,身份证号相同的就是同一个人。重写toString方法
 录入学生信息,身份证号相同的就是同一个人。重写toString方法
 
  一个老师教多个学生。
  分析:使用,has,is
  老师类中,有一个teach方法,最终会展示出他教哪些学生。
  学生类中,有一个study方法,最终会展示出他跟着哪个老师学习。
  学校类,有很多老师,很多学生,最终会展示出哪些老师和哪些学生。
   在Demo类中测试相关功能!
  1.创建一个老师,再来一个老师,比较一下。
  2.学生比较。
  3.老师教的学生信息

答案:

public class Demo
 public class Demo {

    public static void main(String[] args) {
        // 第一个老师和他的学生们
        Teacher teacher = new Teacher("xyz",40,"789789","8001");
        Student s1 = new Student("aaa",20,"123456","1001",teacher);
        Student s2 = new Student("bbb",20,"123457","1002",teacher);
        Student s3 = new Student("ccc",20,"123458","1003",teacher);
        Student s4 = new Student("ddd",20,"123459","1004",teacher);
        Student s5 = new Student("eee",20,"123450","1005",teacher);

//        System.out.println(s1);

        // 第一个老师和他的学生们的关系
        Student [] stus = new Student[]{s1,s2,s3,s4,s5};
        teacher.setStus(stus);

        // 第二个老师和他的学生们
        Teacher teacher2 = new Teacher("qwer",40,"456456","8002");
        Student s6 = new Student("fff",22,"223456","2001",teacher2);
        Student s7 = new Student("ggg",22,"223457","2002",teacher2);
        Student s8 = new Student("hhh",22,"223458","2003",teacher2);
        Student s9 = new Student("iii",22,"223459","2004",teacher2);
        Student s10 = new Student("jjj",22,"223450","2005",teacher2);

        // 第二个老师和他的学生们的关系
        Student [] stus2 = new Student[]{s6,s7,s8,s9,s10};
        teacher2.setStus(stus2);

        Teacher [] teachers = new Teacher[]{teacher,teacher2};

        // 所有学生的数组
        Student [] students = new Student[]{s1,s2,s3,s4,s5,s6,s7,s8,s9,s10};

        School school = new School();
        school.setTeacher(teachers);
        school.setStudents(students);

//        school.showTeachers();
        school.showStudents();
    }
}
public class Person
 public class Person {

    private String name;
    private Integer age;
    private String cardId;

    public Person() {
    }

    public Person(String name, Integer age, String cardId) {
        this.name = name;
        this.age = age;
        this.cardId = cardId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getCardId() {
        return cardId;
    }

    public void setCardId(String cardId) {
        this.cardId = cardId;
    }

    @Override
    public boolean equals(Object obj) {
        Person person = (Person) obj;
        if(this == person){
            return true;
        }
        if(this.cardId.equals(person.cardId)){
            return true;
        }
        return false;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", cardId='" + cardId + '\'' +
                '}';
    }
}
public class School
 public class School {

    private Teacher [] teacher;

    private Student [] students;

    public Teacher[] getTeacher() {
        return teacher;
    }

    public void setTeacher(Teacher[] teacher) {
        this.teacher = teacher;
    }

    public Student[] getStudents() {
        return students;
    }

    public void setStudents(Student[] students) {
        this.students = students;
    }

    public void showTeachers() {
        // 老师和他对应的学生的信息
        for (Teacher teacher1 : teacher) {
            System.out.println(teacher1.teach());
        }
    }
    public void showStudents() {
        // 学生和他对应的老师的信息
        for (Student student : students) {
            System.out.println(student.study());
        }
    }
}
public class Student extends Person
 public class Student extends Person {

    private String stuNo;

    private Teacher teacher;

    public Student() {
    }

    public Student(String name, Integer age, String cardId, String stuNo, Teacher teacher) {
        super(name, age, cardId);
        this.stuNo = stuNo;
        this.teacher = teacher;
    }

    public String study(){
        return this + ",\n 老师是:" + teacher;
    }

    public String getStuNo() {
        return stuNo;
    }

    public void setStuNo(String stuNo) {
        this.stuNo = stuNo;
    }

    public Teacher getTeacher() {
        return teacher;
    }

    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }

    @Override
    public String toString() {
        return super.toString() + "Student{" +
                "stuNo='" + stuNo + "}";
    }
}
public class Teacher extends Person
 import java.util.Arrays;

public class Teacher extends Person {

    private String teaNo;

    private Student [] stus;

    public Teacher() {
    }

    public Teacher(String name, Integer age, String cardId, String teaNo) {
        super(name, age, cardId);
        this.teaNo = teaNo;
    }

    public String teach() {

        return this + ",\n" + Arrays.toString(stus);
    }

    public String getTeaNo() {
        return teaNo;
    }

    public void setTeaNo(String teaNo) {
        this.teaNo = teaNo;
    }

    public Student[] getStus() {
        return stus;
    }

    public void setStus(Student[] stus) {
        this.stus = stus;
    }

    @Override
    public String toString() {
        return super.toString() + "Teacher{" +
                "teaNo='" + teaNo;
    }
}

 

标签:return,String,23,Teacher,teacher,Student,2022.07,public,王茜
来源: https://www.cnblogs.com/wx1019/p/16512761.html