20220723 第一组 于芮 父类与子类的继承2(第十五天)
小白成长记——第十五天
今天结束了面向对象中的继承的学习,总体来说,知识还是不够巩固,还需要多加理解,经过了两天的学习,更加的了解了面向对象的含义,经过两天的学习,可以发现自己的逻辑漏洞,每天都有新的知识,对编程的世界更加了解,来看看今天的学习笔记吧!
protected受保护的
final关键字(最终的,终极的)
final可修饰的结构
1.类 public final class
最终类,终极类,不能被继承
2.属性 private final string name
常量,不能重新赋值,不能只声明不赋值
常量的命名规则(单词所有字母大写,如果是多个单词,用下划线分割)
3.方法 private void show
不可以被子类重写
方法重写:标明注解@(@override )
object(祖先类,最顶级的父类)---11个方法
如果一个类没有明确写出父类是谁,默认继承父类就是object
1.hostcode;
public native int hostcode();
返回值是对象运行时的内存地址。
2.equals();和双等号一样,比地址
integer中有一个缓冲区,把byte范围内的数存起来了
3.tostring();转换成字符串--全类名(有包)
当我们直接使用对象时,默认调用tostring方法,为了让子类重写
4.finalize();垃圾回收的方法
5.clone();克隆,必须实现cloneable接口
看了今天的学习笔记,当然还有一个实际的案例,今天的案例我们做了一个简单的教务系统,通过今天的案例,来巩固今天的知识,看看这个案例吧!
1 public class Demo { 2 public static void main(String[] args) { 3 Teacher teacher = new Teacher("xyz",40,"789789","8001"); 4 Student s1 = new Student("aaa",20,"123456","1001",teacher); 5 Student s2 = new Student("bbb",20,"123457","1002",teacher); 6 Student s3 = new Student("ccc",20,"123458","1003",teacher); 7 Student s4 = new Student("ddd",20,"123459","1004",teacher); 8 Student s5 = new Student("eee",20,"123450","1005",teacher); 9 Student [] stus = new Student[]{s1,s2,s3,s4,s5}; 10 teacher.setStudents(stus); 11 Teacher teacher2 = new Teacher("qwer",40,"456456","8002"); 12 Student s6 = new Student("fff",22,"223456","2001",teacher2); 13 Student s7 = new Student("ggg",22,"223457","2002",teacher2); 14 Student s8 = new Student("hhh",22,"223458","2003",teacher2); 15 Student s9 = new Student("iii",22,"223459","2004",teacher2); 16 Student s10 = new Student("jjj",22,"223450","2005",teacher2); 17 Student [] stus2 = new Student[]{s6,s7,s8,s9,s10}; 18 teacher2.setStudents(stus2); 19 20 Teacher [] teachers = new Teacher[]{teacher,teacher2}; 21 Student [] students = new Student[]{s1,s2,s3,s4,s5,s6,s7,s8,s9,s10}; 22 23 School school = new School(); 24 school.setTeachers(teachers); 25 school.setStudents(students); 26 27 // school.showTeachers(); 28 school.showStudents(); 29 30 31 32 33 34 35 } 36 }
1 public class Person { 2 private String name; 3 private int age; 4 private String cardID; 5 6 public Person(String name,int age,String cardID) { 7 this.name = name; 8 this.age=age; 9 this.cardID=cardID; 10 } 11 12 public String getName() { 13 return name; 14 } 15 16 public void setName(String name) { 17 this.name = name; 18 } 19 20 public int getAge() { 21 return age; 22 } 23 24 public void setAge(int age) { 25 this.age = age; 26 } 27 28 public String getCardID() { 29 return cardID; 30 } 31 32 public void setCardID(String cardID) { 33 this.cardID = cardID; 34 } 35 @Override 36 public boolean equals(Object obj) { 37 Person person = (Person) obj; 38 if(this == person){ 39 return true; 40 } 41 if(this.cardID.equals(person.cardID)){ 42 return true; 43 } 44 return false; 45 } 46 47 @Override 48 public String toString() { 49 return "Person{" + 50 "name='" + name + '\'' + 51 ", age=" + age + 52 ", cardId='" + cardID + '\'' + 53 '}'; 54 } 55 }
1 public class School { 2 private Student[] students; 3 private Teacher[] teachers; 4 5 public void School(Student[] students, Teacher[] teachers) { 6 this.students = students; 7 this.teachers = teachers; 8 } 9 10 public Student[] getStudents() { 11 return students; 12 } 13 14 public void setStudents(Student[] students) { 15 this.students = students; 16 } 17 18 public Teacher[] getTeachers() { 19 return teachers; 20 } 21 22 public void setTeachers(Teacher[] teachers) { 23 this.teachers = teachers; 24 } 25 public void showTeachers(){ 26 for (Teacher teacher : teachers) { 27 System.out.println(teacher.teach()); 28 29 } 30 } 31 public void showStudents(){ 32 for(Student student:students){ 33 System.out.println(student.Study()); 34 } 35 } 36 }
1 public class Student extends Person { 2 public Student(String name, int age, String cardID) { 3 super(name, age, cardID); 4 this.stuNo=stuNo; 5 this.teacher=teacher; 6 7 } 8 9 private String stuNo; 10 private Teacher teacher; 11 12 public Student(String name, int age, String cardID, String stuNo, Teacher teacher) { 13 super(name,age,cardID); 14 this.stuNo=stuNo; 15 this.teacher=teacher; 16 } 17 18 public String getStuNo() { 19 return stuNo; 20 } 21 22 public void setStuNo(String stuNo) { 23 this.stuNo = stuNo; 24 } 25 26 public Teacher getTeacher() { 27 return teacher; 28 } 29 30 public void setTeacher(Teacher teacher) { 31 this.teacher = teacher; 32 } 33 34 public String Study(){ 35 return this + ",\n 老师是:" +teacher; 36 } 37 @Override 38 public String toString() { 39 return super.toString() + "Student{" + 40 "stuNo='" + stuNo + "}"; 41 } 42 }
1 package T0723; 2 import java.util.Arrays; 3 4 public class Teacher extends Person { 5 6 public Teacher(String name, int age, String cardID ,String terNo) { 7 super(name, age, cardID); 8 this.terNo=terNo; 9 } 10 private String terNo; 11 private Student[] students; 12 13 public String getTerNo() { 14 return terNo; 15 } 16 17 public void setTerNo(String terNo) { 18 this.terNo = terNo; 19 } 20 21 public Student[] getStudents() { 22 return students; 23 } 24 25 public void setStudents(Student[] students) { 26 this.students = students; 27 } 28 public String teach(){ 29 return this + ",\n" + Arrays.toString(students); 30 } 31 @Override 32 public String toString() { 33 return super.toString() + "Teacher{" + 34 "terNo='" + terNo; 35 } 36 }