• 2,
  • 一个Java源文件中可以有多个类,但只能含有一个public类,且必须与文件重名
  • 覆盖:子类继承父类,重写了父类的方法后,运行时发生覆盖
  • static以及public的作用和影响
  1. public与子父类、包在属性继承中的影响

java中两个相同对象合并到一起 java系统里面两个类重名_java中两个相同对象合并到一起

java中两个相同对象合并到一起 java系统里面两个类重名_ci_02

     2.static

  • 代码块后退

 

    选中后按“Tab"键

 

  • 代码块前进

    选中后按”Shift+Tab"键

  • 发生覆盖必须具有相同的方法名,相同的返回值类型、相同的参数列表

发生覆盖需要的条件

  • switch () {

java中两个相同对象合并到一起 java系统里面两个类重名_System_03

import java.util.Scanner;

public class Person {
    private long ID;                   //编号
    private String Name;         //姓名
    private String Place;           //电子邮件
    private String className = "Person";    //类名

    //录入属性
    void Set(){
        System.out.print("请输入person编号ID和姓名,电子邮件: ");
        Scanner a = new Scanner(System.in);
        this.ID = a.nextLong();
        this.Name = a.next();
        this.Place = a.next();
    }

    public String toString() {
        System.out.println(this.className + " " + this.ID + " " + this.Name);
        return null;
    }

}
    class Student extends Person {
        long ID;
        String Name;
        String Place;
        String className = "Student";

        String Class;          //班级状态

        //录入属性
        void Set(){
            System.out.print("请输入student编号ID和姓名,电子邮件,班级状态: ");
            Scanner a = new Scanner(System.in);
            this.ID = a.nextLong();
            this.Name = a.next();
            this.Place = a.next();
            this.Class = a.next();
        }

        public String toString() {
            System.out.println(this.className + " " + this.ID + " " + this.Name);
            return null;
        }
    }

    class Employee extends Person {
        long ID;
        String Name;
        String Place;
        String className = "Employee";

        //录入属性
        void Set(){
            System.out.print("请输入employee编号ID和姓名,电子邮件: ");
            Scanner a = new Scanner(System.in);
            this.ID = a.nextLong();
            this.Name = a.next();
            this.Place = a.next();
        }

        public String toString() {
            System.out.println(this.className + " " + this.ID + " " + this.Name);
            return null;
        }
}

    class Faculty extends Employee {
        long ID;
        String Name;
        String Place;
        String className = "Faculty";

        String Project;         //主讲课程
        String Information;     //专业信息

        //录入属性
        void Set(){
            System.out.print("请输入faculty编号ID和姓名,电子邮件,主讲课程,专业信息: ");
            Scanner a = new Scanner(System.in);
            this.ID = a.nextLong();
            this.Name = a.next();
            this.Place = a.next();
            this.Project = a.next();
            this.Information = a.next();
        }

        public String toString() {
            System.out.println(this.className + " " + this.ID + " " + this.Name);
            return null;
        }
    }

    class Staff extends Employee {
        long ID;
        String Name;
        String Place;
        String className = "Staff";

        String Mission;         //职务信息

        //录入属性
        void Set(){
            System.out.print("请输入staff编号ID和姓名,电子邮件,职务信息: ");
            Scanner a = new Scanner(System.in);
            this.ID = a.nextLong();
            this.Name = a.next();
            this.Place = a.next();
            this.Mission = a.next();
        }

        public String toString() {
            System.out.println(this.className + " " + this.ID + " " + this.Name);
            return null;
        }
    }
import java.util.Scanner;

public class Application {
    public static void main(String[] args){
        Person[] Total = new Person[5];     // 存放随机生成的对象的数组

        //创建对象
        Student student = new Student();
        Faculty faculty = new Faculty();
        Staff staff = new Staff();

        //输入属性
        student.Set();
        faculty.Set();
        staff.Set();

        //随机生成1~3之间的整数,创建对象,并调用toString()方法输出
        int n;
        for(int i=0; i<5; i++){
           n = (int)(Math.random() *3+1);

           switch (n){
               case 1:Total[i]= student;Total[i].toString();break;
               case 2:Total[i]= faculty;Total[i].toString();break;
               case 3:Total[i]= staff;Total[i].toString();break;
           }
        }
    }
}

//如何使用Vector类对象

  •  Vector(向量类):实现自动增长的对象数组,可以向其中插入不同类的对象;对于不知或者不愿预先定义数组大小,并且需要频繁地进行查找,插入,删除工作的情况,可以考虑使用向量类;
  • 用法:
  1. java.util.vector
  2. 提供了三种构造方法 public vector() ;public vector(int initialcapacity,int capacityIncrement) ;public vector(int initialcapacity) ;使用第一种方法时系统会自动对向量进行管理,若使用后两种方法,系统将根据参数initialcapacity设定向量对象的容量(即向量对象可存储数据的大小),当真正存放的数据个数超过容量时,系统会扩充向量对象存储容量;
    参数capacityincrement给定了每次扩充的扩充值,当capacityincrement为0的时候,则每次扩充一倍,利用这个功能可以优化存储;
  •  main方法体
//创建对象
        Student student = new Student();
        Faculty faculty = new Faculty();
        Staff staff = new Staff();

        //输入属性
        student.Set();
        faculty.Set();
        staff.Set();

        //使用Vector
        Vector vec = new Vector();
        int n;
        for(int i=0; i<5; i++) {
            n = (int) (Math.random() * 3 + 1);

            switch (n) {
                case 1:
                    vec.addElement(student);student.toString();
                    break;
                case 2:
                    vec.addElement(faculty);faculty.toString();
                    break;
                case 3:
                    vec.addElement(staff);staff.toString();
                    break;
            }
        }
  • 运行结果

java中两个相同对象合并到一起 java系统里面两个类重名_java中两个相同对象合并到一起_04

  • 异常: Error类和Exception类的父类都是throwable类,区别:
    Error类一般是指与虚拟机相关的问题,如系统崩溃,虚拟机错误,内存空间不足,方法调用栈溢等。对于这类错误的导致的应用程序中断,仅靠程序本身无法恢复和和预防,遇到这样的错误,建议让程序终止。
    Exception类表示程序可以处理的异常,可以捕获且可能恢复。遇到这类异常,应该尽可能处理异常,使程序恢复运行,而不应该随意终止异常。
    Exception类又分为运行时异常(Runtime Exception)和受检查的异常(Checked Exception ),运行时异常;ArithmaticException,IllegalArgumentException,编译能通过,但是一运行就终止了,程序不会处理运行时异常,出现这类异常,程序会终止。而受检查的异常,要么用try...catch捕获,要么用throws字句声明抛出,交给它的父类处理,否则编译不会通过。
    常见的异常;
    ArrayIndexOutOfBoundsException 数组下标越界异常,
    ArithmaticException 算数异常 如除数为零
    NullPointerException 空指针异常
    IllegalArgumentException 不合法参数异常

java中两个相同对象合并到一起 java系统里面两个类重名_System_05

java中两个相同对象合并到一起 java系统里面两个类重名_System_06

java中两个相同对象合并到一起 java系统里面两个类重名_父类_07

  • 捕获和抛出异常
  1. 五个关键字:try,catch,finally,throw,throws
  2. 可以包含多个catch,如果要捕获多个异常,需要从小到大排列
  3. 快捷键:选中当前代码,ctrl+alt+t
  4. 对象.printStackTrace:打印错误的栈信息
  5. 主动抛出异常:throw,一般在方法中使用

java中两个相同对象合并到一起 java系统里面两个类重名_java中两个相同对象合并到一起_08

/*

  •  3,何为异常:是程序在运行时出现的会导致程序运行终止的错误,这种错误不能通过编译系统检查出来;
  • 进行异常处理的原因:
  1. 为了良好的用户体验,程序会捕获这个异常,给用户一个良好的出错提示;
  2. 若发生异常不处理,会导致程序中断,系统无法正常运行; 

    3.让程序具有更好的容错性,使程序更健壮 代码更易组织、清晰,复杂的工作任务更易实现,且更安全;

*/ 判断学生班级状态