day_9:继承、super、重写、Object类、final

 继承 

  继承中的构造方法:       

      * 1.创建子类对象,调用父类构造方法的目的:将父类的属性复制一份放在子类的对象中,子类就拥有父类的属性
      * 2.创建子类对象,先调用的是子类构造方法
    * 创建子类对象,会调用父类构造方法吗?会
    * 创建子类对象,会创建父类对象吗?不会
    * 创建子类对象,会创建父类引用吗?不会
    * 创建子类对象,先调用父类构造方法还是子类构造方法?子类构造方法
    * 创建子类对象,先执行完父类构造方法还是子类构造方法?父类构造方法

public class Chinese extends Person{
    
    public Chinese() {
        super();//调用父类的无参构造
        System.out.println("调用子类构造方法");
    }
}

  super   

    经验:就算你写了有参构造,就要把无参构造写上(有了有参构造,系统不会默认实现无参构造),
    * 因为第三阶段的时候使用很多第三方框架,三方框架底层很喜欢使用反射的技术来去创建对象,
    * 利用反射技术会找到该类的无参构造,如果这时你没有无参构造的情况下,会报错!!!!
  * super.属性 : 调用父类的一般属性
  * super.方法 : 调用父类的一般方法
  * super() : 调用父类相应的一般构造方法

super(name, age, sex);//调用父类的有参构造
System.out.println(super.info);//调用父类的属性
System.out.println(super.getName());//调用父类的方法

  重写/复写

      应用场景:父类方法不满足子类需求时,考虑重写
             条件:
                    1.子父类关系的子类中重写
                    2.重写方法的返回值、方法名、参数列表必须和父类方法一致
                    3.访问修饰符不能比父类的方法更严格

  Object类

    Object:基类、超类,是所有类的祖先类,如果一个类没有明确指定父类,都默认继承Object类
         
            Object:
                    toString()底层源码:
                         public String toString() {
                            return getClass().getName() + "@" + Integer.toHexString(hashCode());
                        }
                        Integer.toHexString(int值):把十进制的数字转换为十六进制
                    getClass():获取字节码文件的对象
                    hashCode():获取当前对象的hash值
                    equals()底层源码:比较两个对象的内存地址是否相同
                        public boolean equals(Object obj) {
                            return (this == obj);
                      }

  修饰符

    

  final

    final -- 最终:
                  修饰变量:常量
                    ps:
                         1.不能被重新赋值
                          2.常量命名规范:全部大写,单词之间用下划线隔开
                    生命周期:
                          创建:代码执行到那一行
                          存放地点:常量池
                          销毁:项目结束时
                 修饰方法:方法不能被重写
                 修饰类:类不能被继承
         
          面试题:String 为什么是final类
                  1.字符串的不变性(字面值常量是字符串,要遵循不可变,那么String类就用final修饰)
                  2.用final修饰的类,JVM会认为此类里的方法属于内联函数,直接调用效率更高

  static final

    public static final int NAME = 1;
    public static final int SEX = 2;
    public static final int AGE = 3;
    public static final int CLASS_ID = 4;
    public static final int ID = 5;    

二维数组版学生管理系统

package com.cmlx.text03;

public final class IsStu {
    public static boolean isStu(String name, String age, String sex, String classId, String id) {

        return true;
    }

    public static boolean isName(String name) {
        if (name.length() < 2 || name.length() > 6) {
            return false;
        }
        return true;
    }

    public static boolean isAge(String age) {
        int age1 = Integer.parseInt(age);
        if (age1 < 0 || age1 > 100) {
            return false;
        }

        return true;
    }

    public static boolean isSex(String sex) {
        if (!sex.equals("男") && !sex.equals("女")) {
            return false;
        }
        return true;
    }

    public static boolean isClassId(String classId) {
        if (classId.length() != 4) {
            return false;
        }
        return true;
    }

    public static boolean isId(String id) {
        if (id.length() != 2) {
            return false;
        }
        return true;
    }

}
IsStu.java
package com.cmlx.text03;

public class StuType {
    
    public static final int NAME = 1;
    public static final int AGE = 2;
    public static final int SEX = 3;
    public static final int CLASS_ID = 4;
    public static final int ID = 5;

}
StuType.java
package com.cmlx.text03;

public class TestSms {

    public static void main(String[] args) {
        add("赤名莉香", "13", "女", "1807", "18");
        add("赤名莉香", "13", "女", "1802", "17");
        add("赤名莉香", "13", "女", "1803", "16");
        add("赤名莉香", "13", "女", "1804", "15");
        add("赤名莉香", "13", "女", "1805", "12");
        delete("1803", "16");
        update("1802", "17", 5, "10");
        printStu();
    }

    public static String[][] stus = new String[3][5];
    public static int point = 0;
    
    //修改
    public static boolean update(String classId,String id,int type,String value) {
        //验证信息合法性
        if(!IsStu.isClassId(classId) || !IsStu.isId(id)) {
            return false;
        }
        //验证是否有该学生
        int x = querry(classId, id);
        if(x == -2|| x==-1) {
            return false;
        }
        //修改
        switch (type) {
        case StuType.NAME:
            if(!IsStu.isName(value)) {
                return false;
            }
            stus[x][0] = value;
            break;
            
        case StuType.AGE:
            if(!IsStu.isAge(value)) {
                return false;
            }
            stus[x][1] = value;
            break;
            
        case StuType.SEX:
            if(!IsStu.isSex(value)) {
                return false;
            }
            stus[x][2] = value;
            break;
            
        case StuType.CLASS_ID:
            if(!IsStu.isName(value)) {
                return false;
            }
            stus[x][3] = value;
            break;
            
        case StuType.ID:
            if(!IsStu.isId(value)) {
                return false;
            }
            stus[x][4] = value;
            break;

        default:
            break;
        }
        
        return true;
    }
    
    //删除
    public static boolean delete(String classId, String id) {
        // 判断信息是否合法
        if (!IsStu.isClassId(classId) || !IsStu.isId(id)) {
            return false;
        }
        // 查询是否存在该学生
        int x = querry(classId, id);
        if (x == -2 || x == -1) {
            return false;
        }
        // 删除
        for (int i = x; i < point - 1; i++) {
            stus[x] = stus[x + 1];
        }
        point--;
        return true;
    }

    public static boolean add(String name, String age, String sex, String classId, String id) {
        // 判断学生信息是否合法
        if (!IsStu.isStu(name, age, sex, classId, id)) {
            return false;
        }
        // 判断是否有该学生
        int x = querry(classId, id);
        if (x == -2 || x != -1) {
            return false;
        }
        // 扩容
        KuoRong();

        // 添加
        stus[point][0] = name;
        stus[point][1] = age;
        stus[point][2] = sex;
        stus[point][3] = classId;
        stus[point][4] = id;
        point++;
        return true;
    }

    // 打印
    public static void printStu() {
        for (int i = 0; i < point; i++) {
            for (int j = 0; j < stus[i].length; j++) {
                System.out.print(stus[i][j] + "\t");
            }
            System.out.println();
        }
    }

    // 扩容
    private static void KuoRong() {
        String[][] newStr = new String[stus.length * 2][5];
        for (int i = 0; i < point; i++) {
            newStr[i] = stus[i];
        }
        stus = newStr;
    }

    // 查询
    public static int querry(String classId, String id) {
        if (!IsStu.isClassId(classId) || !IsStu.isId(id)) {
            return -2;
        }
        for (int i = 0; i < point; i++) {
            if (stus[i][3].equals(classId) && stus[i][4].equals(id)) {
                return i;
            }
        }
        return -1;
    }

}
TestSms.java

 

 

 

   

posted @ 2018-10-25 16:15  cmlx  阅读(128)  评论(0)    收藏  举报