• 达拉草201771010105《面向对象程序设计(java)》第六周学习总结


     达拉草201771010105《面向对象程序设计(java)》第六周学习总结

    第一部分:理论知识

    1.类、超类和子类

    类继承的格式: class 新类名extends已有类名
    一般来说,子类比超类拥有的功能更加丰富

    super是一个指示编译器调用超类方法的特有关键字, 它不是一个对象的引用,不能将super赋给另一个对 象变量。super关键字一般有两个用途:一是调用超 类的方法(格式:super.方法名()),二是调用 超类的构造器(格式:super())。 

    继承层次:从一个超类扩展而来的类集合称为继承层次 。在继承层次中,从 某个类到其祖先的路径被称为该类的继承链 。 Java不支持多继承。

    多态性的概念;多态性泛指在程序中同一个符号在不同的情况 下具有不同解释的现象。 超类中定义的域或方法,被子类继承之后,可 以具有不同的数据类型或表现出不同的行为。这使得同一个域或方法在超类及其各个子类中 具有不同的语义。 超类中的方法在子类中可方法重写。

    动态绑定:又称为运行时绑定。意思是说,程序在运行时 会自动选择调用哪个方法。 

    阻止继承:Final类和方法

    不允许继承的类称为final类,在类的定义中用final修饰 符加以说明。如果一个类声明为final,属于它的方法会被自动设为 final,但不包括域(如果域定义为final,在对象构造以 后,final域就不能再修改了)。String类是final类的一个例子。不能扩展该类。

    强制类型转换:如果要把一个超类对象赋给一个子类对象变量,就必须进 行强制类型转换。其格式为: 子类对象=(子类)(超类对象)

    抽象类:

    a.为了提高程序的清晰度,包含一个或多个抽象方法 的类本身必须被声明为抽象类。除了抽象方法之外 ,抽象类还可以包含具体数据和具体方法。

    b.抽象方法充当着占位的角色,它们的具体实现在子 类中。扩展抽象类可以有两种选择:一种是在子类 中实现部分抽象方法,这样就必须将子类也标记为 抽象类;另一种是实现全部抽象方法,这样子类就 可以不是抽象类。此外,类即使不含抽象方法,也 可以将类声明为抽象类。

    c.抽象类不能被实例化,即不能创建对象,只能产生 子类。可以创建抽象类的对象变量,只是这个变量 必须指向它的非抽象子类的对象。

    2. Object:所有类的超类 

     Object类是Java中所有类的祖先——每一个类都由它扩 展而来。在不给出超类的情况下,Java会自动把Object 作为要定义类的超类。 

    可以使用类型为Object的变量指向任意类型的对象。但 要对它们进行专门的操作都要进行类型转换。

    equals方法:定义子类的equals方法时,可调用超类的equals方法。 super.equals(otherObject)

    hashCode方法:Object类中的hashCode方法导出某个对象的散列 码。散列码是任意整数,表示对象的存储地址。 两个相等对象的散列码相等。

    3. 泛型数组列表 

    Java中,利用ArrayList类,可允许程序在运行 时确定数组的大小。ArryList是一个采用类型参数的泛型类。为指定 数组列表保存元素的对象类型,需要用一对尖括 号将数组元素对象类名括起来加在后面。
    数组列表的操作:
    a.ArrayList定义  b.添加新元素  c.统计个数  d.调整大小  e.访问  f.增加与删除 

    4.参数数量可变的方法

    用户自己可以定义可变参数的方法,并将参数指 定为任意类型,甚至是基本类型。

    5. 枚举类

    声明枚举类 publicenumGrade{A,B,C,D,E}; 它包括一个关键字enum,一个新枚举类型的名字 Grade以及为Grade定义的一组值,这里的值既 非整型,亦非字符型。
    声明枚举类
    枚举类说明 –枚举类是一个类,它的隐含超类是java.lang.Enum。 枚举值并不是整数或其它类型,是被声明的枚举类的 自身实例,例如A是Grade的一个实例。 枚举类不能有public修饰的构造函数,构造函数都是 隐含private,编译器自动处理。 –枚举值隐含都是由public、static、final修饰的,无 须自己添加这些修饰符。 在比较两个枚举类型的值时,永远不需要调用equals 方法,直接使用"=="进行相等比较。

     第二部分:实验部分

    实验六继承定义与使用

    实验时间 2018-9-28

    1、实验目的与要求

    (1) 理解继承的定义;

    (2) 掌握子类的定义要求

    (3) 掌握多态性的概念及用法;

    (4) 掌握抽象类的定义及用途;

    (5) 掌握类中4个成员访问权限修饰符的用途;

    (6) 掌握抽象类的定义方法及用途;

    (7)掌握Object类的用途及常用API;

    (8) 掌握ArrayList类的定义方法及用法;

    (9) 掌握枚举类定义方法及用途。

    2、实验内容和步骤

    实验1: 导入第5章示例程序,测试并进行代码注释。

    测试程序1:

    Ÿ   在elipse IDE中编辑、调试、运行程序5-1 (教材152页-153页) ;

    Ÿ   掌握子类的定义及用法;

    Ÿ   结合程序运行结果,理解并总结OO风格程序构造特点,理解Employee和Manager类的关系子类的用途,并在代码中添加注释。

     1.ManagerTest类

     1 package inheritance;
     2 
     3 /**
     4  * This program demonstrates inheritance.
     5  * @version 1.21 2004-02-21
     6  * @author Cay Horstmann
     7  */
     8 public class ManagerTest
     9 {
    10    public static void main(String[] args)
    11    {
    12       // construct a Manager object
    13       Manager boss = new Manager("Carl Cracker", 80000, 1987, 12, 15);
    14       boss.setBonus(5000);
    15 
    16       Employee[] staff = new Employee[3];//构造了一个Employee数组
    17 
    18       // fill the staff array with Manager and Employee objects
    19 
    20       staff[0] = boss;//父类对象变量可以引用子类对象
    21       staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
    22       staff[2] = new Employee("Tommy Tester", 40000, 1990, 3, 15);
    23       //生成三个雇员对象
    24       
    25       // print out information about all Employee objects
    26       for (Employee e : staff)
    27          System.out.println("name=" + e.getName() + ",salary=" + e.getSalary());
    28    }
    29 }

    2.Employee类

     1 package inheritance;
     2 
     3 import java.time.*;
     4 
     5 public class Employee
     6 {
     7    private String name;//private定义了一个只能在该类中访问的字符串变量
     8    private double salary;
     9    private LocalDate hireDay;
    10 
    11    public Employee(String name, double salary, int year, int month, int day)//定义变量
    12    {
    13       this.name = name;//将局部变量的值传递给成员变量
    14       this.salary = salary;
    15       hireDay = LocalDate.of(year, month, day);
    16    }//一个构造器,构造器与类同名
    17 
    18    public String getName()
    19    {
    20       return name;
    21    }//访问器
    22 
    23    public double getSalary()
    24    {
    25       return salary;
    26    }//访问器
    27 
    28    public LocalDate getHireDay()
    29    {
    30       return hireDay;
    31    }//访问器
    32 
    33    public void raiseSalary(double byPercent)
    34    {
    35       double raise = salary * byPercent / 100;
    36       salary += raise;
    37    }
    38 }

    3.Manager类

     1 package inheritance;
     2 
     3 public class Manager extends Employee//扩展了一个子类Manager
     4 {
     5    private double bonus;
     6 
     7    /**
     8     * @param name the employee's name
     9     * @param salary the salary
    10     * @param year the hire year
    11     * @param month the hire month
    12     * @param day the hire day
    13     */
    14    public Manager(String name, double salary, int year, int month, int day)
    15    {
    16       super(name, salary, year, month, day);//调用了父类的构造器
    17       bonus = 0;
    18    }
    19 
    20    public double getSalary()
    21    {
    22       double baseSalary = super.getSalary();//调用父类的方法
    23       return baseSalary + bonus;//可以重写父类的方法
    24    }
    25 
    26    public void setBonus(double b)
    27    {
    28       bonus = b;
    29    }//更改器
    30 }

    实验结果:

    测试程序2:

    Ÿ   编辑、编译、调试运行教材PersonTest程序(教材163页-165页);

    Ÿ   掌握超类的定义及其使用要求;

    Ÿ   掌握利用超类扩展子类的要求;

    Ÿ   在程序中相关代码处添加新知识的注释。

    1.PersonTest类

     1 package abstractClasses;
     2 
     3 /**
     4  * This program demonstrates abstract classes.
     5  * @version 1.01 2004-02-21
     6  * @author Cay Horstmann
     7  */
     8 public class PersonTest
     9 {
    10    public static void main(String[] args)
    11    {
    12       Person[] people = new Person[2];//构造了一个Person数组
    13 
    14       // fill the people array with Student and Employee objects
    15       people[0] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
    16       people[1] = new Student("Maria Morris", "computer science");
    17       //将Employee和Student对象填充到Person引用数组
    18       
    19       
    20       // print out names and descriptions of all Person objects
    21       for (Person p : people)
    22          System.out.println(p.getName() + ", " + p.getDescription());
    23       //打印所有Person对象的名称和描述
    24    }
    25 }

    2.Person类

     1 package abstractClasses;
     2 
     3 public abstract class Person
     4 {
     5    public abstract String getDescription();//定义抽象类型Person
     6    private String name;
     7 
     8    public Person(String name)
     9    {
    10       this.name = name;//将局部变量的值赋给成员变量
    11    }
    12    //为子类Person类的构造器提供代码的构造器
    13 
    14    public String getName()
    15    {
    16       return name;
    17    }//访问器
    18 }

    3.Employee类

     1 package abstractClasses;
     2 
     3 import java.time.*;
     4 
     5 public class Employee extends Person//扩展了一个子类Person
     6 {
     7    private double salary;
     8    private LocalDate hireDay;
     9 
    10    public Employee(String name, double salary, int year, int month, int day)//定义变量
    11    {
    12       super(name);//调用了父类的构造器
    13       this.salary = salary;
    14       hireDay = LocalDate.of(year, month, day);//hireDay使用LocalDate的方法
    15    }
    16 
    17    public double getSalary()
    18    {
    19       return salary;
    20    }
    21 
    22    public LocalDate getHireDay()
    23    {
    24       return hireDay;
    25    }
    26 
    27    public String getDescription()
    28    {
    29       return String.format("an employee with a salary of $%.2f", salary);
    30    }
    31 
    32    public void raiseSalary(double byPercent)
    33    {
    34       double raise = salary * byPercent / 100;
    35       salary += raise;
    36    }
    37 }

    4.Student类

     1 package abstractClasses;
     2 
     3 public class Student extends Person//扩展了一个子类Student
     4 {
     5    private String major;
     6 
     7    /**
     8     * @param nama the student's name
     9     * @param major the student's major
    10     */
    11    public Student(String name, String major)
    12    {
    13       // pass n to superclass constructor
    14        //构造函数
    15       super(name);
    16       this.major = major;
    17    }
    18 
    19    public String getDescription()
    20    {
    21       return "a student majoring in " + major;
    22    }
    23 }

     实验结果:

    测试程序3:

    Ÿ   编辑、编译、调试运行教材程序5-8、5-9、5-10,结合程序运行结果理解程序(教材174页-177页);

    Ÿ   掌握Object类的定义及用法;

    Ÿ   在程序中相关代码处添加新知识的注释。

    1.EqualsTest类

     1 package equals;
     2 
     3 /**
     4  * This program demonstrates the equals method.
     5  * @version 1.12 2012-01-26
     6  * @author Cay Horstmann
     7  */
     8 public class EqualsTest
     9 {
    10    public static void main(String[] args)
    11    {
    12       Employee alice1 = new Employee("Alice Adams", 75000, 1987, 12, 15);
    13       Employee alice2 = alice1;
    14       Employee alice3 = new Employee("Alice Adams", 75000, 1987, 12, 15);
    15       Employee bob = new Employee("Bob Brandson", 50000, 1989, 10, 1);
    16 
    17       System.out.println("alice1 == alice2: " + (alice1 == alice2));
    18 
    19       System.out.println("alice1 == alice3: " + (alice1 == alice3));
    20 
    21       System.out.println("alice1.equals(alice3): " + alice1.equals(alice3));
    22 
    23       System.out.println("alice1.equals(bob): " + alice1.equals(bob));
    24 
    25       System.out.println("bob.toString(): " + bob);
    26 
    27       Manager carl = new Manager("Carl Cracker", 80000, 1987, 12, 15);
    28       Manager boss = new Manager("Carl Cracker", 80000, 1987, 12, 15);
    29       boss.setBonus(5000);
    30       System.out.println("boss.toString(): " + boss);
    31       System.out.println("carl.equals(boss): " + carl.equals(boss));
    32       System.out.println("alice1.hashCode(): " + alice1.hashCode());
    33       System.out.println("alice3.hashCode(): " + alice3.hashCode());
    34       System.out.println("bob.hashCode(): " + bob.hashCode());
    35       System.out.println("carl.hashCode(): " + carl.hashCode());
    36    }
    37 }

    2.Employee类

     1 package equals;
     2 
     3 import java.time.*;
     4 import java.util.Objects;
     5 
     6 public class Employee
     7 {
     8    private String name;//private定义了一个只能在该类中访问的字符串变量
     9    private double salary;
    10    private LocalDate hireDay;
    11    //创建私有属性
    12 
    13    public Employee(String name, double salary, int year, int month, int day)
    14    {
    15       this.name = name;
    16       this.salary = salary;
    17       hireDay = LocalDate.of(year, month, day);
    18    }
    19 
    20    public String getName()
    21    {
    22       return name;
    23    }
    24 
    25    public double getSalary()
    26    {
    27       return salary;
    28    }
    29 
    30    public LocalDate getHireDay()
    31    {
    32       return hireDay;
    33    }
    34    //访问器
    35 
    36    public void raiseSalary(double byPercent)
    37    {
    38       double raise = salary * byPercent / 100;
    39       salary += raise;
    40    }
    41 
    42    public boolean equals(Object otherObject)
    43    {
    44       // 快速测试这些对象是否相同
    45       if (this == otherObject) return true;
    46 
    47       // 如果显示参数为空,必须返回flase
    48       if (otherObject == null) return false;
    49 
    50       // 如果几个类不匹配,则他们不相同
    51       if (getClass() != otherObject.getClass()) return false;
    52 
    53       // 其他对象为非空Employee类
    54       Employee other = (Employee) otherObject;
    55 
    56       // 测试是不是有相同值
    57       return Objects.equals(name, other.name) && salary == other.salary && Objects.equals(hireDay, other.hireDay);
    58    }
    59 
    60    public int hashCode()
    61    {
    62       return Objects.hash(name, salary, hireDay); 
    63    }
    64 
    65    public String toString()//把其他类型的数据转换为字符串类型的数据
    66    {
    67       return getClass().getName() + "[name=" + name + ",salary=" + salary + ",hireDay=" + hireDay
    68             + "]";
    69    }
    70 }

    3.Manager类

     1 package equals;
     2 
     3 public class Manager extends Employee//扩展了一个子类Manager
     4 {
     5    private double bonus;//创建一个私有属性
     6 
     7    public Manager(String name, double salary, int year, int month, int day)//定义变量
     8    {
     9       super(name, salary, year, month, day);//调用了父类的构造器
    10       bonus = 0;
    11    }
    12 
    13    public double getSalary()
    14    {
    15       double baseSalary = super.getSalary();//调用父类的方法
    16       return baseSalary + bonus;
    17    }
    18 
    19    public void setBonus(double bonus)//更改器
    20    {
    21       this.bonus = bonus;
    22    }
    23 
    24    public boolean equals(Object otherObject)
    25    {
    26       if (!super.equals(otherObject)) return false;
    27       Manager other = (Manager) otherObject;
    28       // 用super.equals检查这个类和其他类是否属于同一个类
    29       return bonus == other.bonus;
    30    }
    31 
    32    public int hashCode()
    33    {
    34       return java.util.Objects.hash(super.hashCode(), bonus);
    35    }
    36 
    37    public String toString()//把其他类型的数据转换为字符串类型的数据
    38    {
    39       return super.toString() + "[bonus=" + bonus + "]";
    40    }
    41 }

    实验结果:

    测试程序4:

    Ÿ   在elipse IDE中调试运行程序5-11(教材182页),结合程序运行结果理解程序;

    Ÿ   掌握ArrayList类的定义及用法;

    Ÿ   在程序中相关代码处添加新知识的注释。

    1.ArrayList

     1 package arrayList;
     2 
     3 import java.util.*;
     4 
     5 /**
     6  * This program demonstrates the ArrayList class.
     7  * @version 1.11 2012-01-26
     8  * @author Cay Horstmann
     9  */
    10 public class ArrayListTest
    11 {
    12    public static void main(String[] args)
    13    {
    14       // 用三个Employee类填充staff数组列表
    15       ArrayList<Employee> staff = new ArrayList<>();//构造了一个ArrayList数组
    16 
    17       staff.add(new Employee("Carl Cracker", 75000, 1987, 12, 15));
    18       staff.add(new Employee("Harry Hacker", 50000, 1989, 10, 1));
    19       staff.add(new Employee("Tony Tester", 40000, 1990, 3, 15));
    20 
    21       // raise everyone's salary by 5%
    22       for (Employee e : staff)
    23          e.raiseSalary(5);
    24 
    25       //打印出所有Employee类的信息
    26       for (Employee e : staff)
    27          System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay="
    28                + e.getHireDay());
    29    }
    30 }

    2.Employee类

     1 package arrayList;
     2 
     3 import java.time.*;
     4 
     5 public class Employee
     6 {
     7    private String name;
     8    private double salary;
     9    private LocalDate hireDay;
    10    //创建私有属性
    11 
    12    public Employee(String name, double salary, int year, int month, int day)//定义变量
    13    {
    14       this.name = name;
    15       this.salary = salary;
    16       hireDay = LocalDate.of(year, month, day);
    17    }
    18 
    19    public String getName()
    20    {
    21       return name;
    22    }
    23 
    24    public double getSalary()
    25    {
    26       return salary;
    27    }
    28 
    29    public LocalDate getHireDay()
    30    {
    31       return hireDay;
    32    }
    33    //访问器
    34    
    35    public void raiseSalary(double byPercent)
    36    {
    37       double raise = salary * byPercent / 100;
    38       salary += raise;
    39    }
    40 }

    实验结果:

    测试程序5:

    Ÿ   编辑、编译、调试运行程序5-12(教材189页),结合运行结果理解程序;

    Ÿ   掌握枚举类的定义及用法;

    Ÿ   在程序中相关代码处添加新知识的注释。

     1 package enums;
     2 
     3 import java.util.*;
     4 
     5 /**
     6  * This program demonstrates enumerated types.
     7  * @version 1.0 2004-05-24
     8  * @author Cay Horstmann
     9  */
    10 public class EnumTest
    11 {  
    12    public static void main(String[] args)
    13    {  
    14       Scanner in = new Scanner(System.in);
    15       System.out.print("Enter a size: (SMALL, MEDIUM, LARGE, EXTRA_LARGE) ");
    16       String input = in.next().toUpperCase();
    17       Size size = Enum.valueOf(Size.class, input);
    18       System.out.println("size=" + size);
    19       System.out.println("abbreviation=" + size.getAbbreviation());
    20       if (size == Size.EXTRA_LARGE)
    21          System.out.println("Good job--you paid attention to the _.");      
    22    }
    23 }
    24 
    25 enum Size//枚举类型
    26 {
    27    SMALL("S"), MEDIUM("M"), LARGE("L"), EXTRA_LARGE("XL");
    28 
    29    private Size(String abbreviation) { this.abbreviation = abbreviation; }
    30    public String getAbbreviation() { return abbreviation; }
    31 
    32    private String abbreviation;
    33 }

    实验2编程练习1

    Ÿ   定义抽象类Shape:

    属性:不可变常量double PI,值为3.14;

    方法:public double getPerimeter();public double getArea())。

    Ÿ   让Rectangle与Circle继承自Shape类。

    Ÿ   编写double sumAllArea方法输出形状数组中的面积和和double sumAllPerimeter方法输出形状数组中的周长和。

    Ÿ   main方法中

    1)输入整型值n,然后建立n个不同的形状。如果输入rect,则再输入长和宽。如果输入cir,则再输入半径。
    2) 然后输出所有的形状的周长之和,面积之和。并将所有的形状信息以样例的格式输出。
    3) 最后输出每个形状的类型与父类型,使用类似shape.getClass()(获得类型),shape.getClass().getSuperclass()(获得父类型);

    思考sumAllArea和sumAllPerimeter方法放在哪个类中更合适?

    输入样例:

    3

    rect

    1 1

    rect

    2 2

    cir

    1

    输出样例:

    18.28

    8.14

    [Rectangle [width=1, length=1], Rectangle [width=2, length=2], Circle [radius=1]]

    class Rectangle,class Shape

    class Rectangle,class Shape

    class Circle,class Shape

    实验代码如下:

     1 package mannem;
     2 public class shape {
     3       double PI=3.14;
     4       double getPerimeter() {
     5           return 0;
     6       }
     7       double getArea(){
     8           return 0;
     9       }
    10 } 
     1 package mannem;
     2 
     3 import mannem.man;
     4 import mannem.Rectangle;
     5 import mannem.Circle;
     6 import java.math.*;
     7 import java.util.Scanner;
     8 
     9 public class man {
    10 
    11     public static void main(String[] args) {
    12         Scanner in = new Scanner(System.in);
    13         String rect = "rect";
    14         String cir = "cir";
    15         System.out.print("");
    16         int n = in.nextInt();
    17         shape[] score = new shape[n];
    18 
    19         for (int i = 0; i < n; i++) {
    20 
    21             String input = in.next();
    22             if (input.equals(rect)) {// System.out.print("rect:");
    23                 double length = in.nextDouble();
    24                 double width = in.nextDouble();
    25                 score[i] = new Rectangle( width,  length);
    26             }
    27             if (input.equals(cir)) {// System.out.print("cir:");
    28                 double radius = in.nextDouble();
    29                 score[i] = new Circle(radius);
    30             }
    31 
    32         }
    33         for (int i = 0; i < n; i++) {
    34             System.out.println(score[i]);
    35         }
    36 
    37         man c = new man();
    38         System.out.println(c.sumAllPerimeter(score));
    39         System.out.println(c.sumAllArea(score));
    40         for (shape s : score) {
    41             // System.out.println(s.getArea()+s.getPerimeter());
    42             System.out.println(s.getClass() + "     ,    " + s.getClass().getSuperclass());
    43         }
    44 
    45     }
    46 
    47     public double sumAllArea(shape score[]) {
    48         double sum = 0;
    49         for (int i = 0; i < score.length; i++)
    50             sum += score[i].getArea();
    51         return sum;
    52     }
    53 
    54     public double sumAllPerimeter(shape score[]) {
    55         double sum = 0;
    56         for (int i = 0; i < score.length; i++)
    57             sum += score[i].getPerimeter();
    58         return sum;
    59     }
    60 
    61 }
     1 package mannem;
     2 
     3 public class Rectangle extends shape {
     4    private int length;
     5    private int width;
     6    public Rectangle(int length,int width) {
     7        this.length=length;
     8        this.width=width;
     9    }
    10    public Rectangle(double width2, double length2) {
    11     // TODO 自动生成的构造函数存根
    12 }
    13 public double getPerimeter(){
    14     double Perimeter=2*(length+width);
    15             return Perimeter;
    16    }
    17    public double getArea() {
    18        double Area=length*width;
    19        return Area;
    20    }
    21    public String toString() {
    22    return getClass().getName()+"[width="+width+"]"+"[length="+length+"]";
    23    }
    24 }   
     1 package mannem;
     2 
     3 public class Circle extends shape{
     4       private int radius;
     5       public Circle(int r) {
     6           this.radius=r;
     7       }
     8       public Circle(double radius2) {
     9         // TODO 自动生成的构造函数存根
    10     }
    11     double getPerimeter() {
    12           double Perimeter= 2*PI*radius;
    13           return Perimeter;
    14       }//求周长
    15       double getArea() {
    16           double Area= PI*radius*radius;
    17           return Area;
    18       }
    19       public String toString() {
    20           return getClass().getName()+"[radius="+ radius +"]";
    21       }
    22 }

    实验结果:

    实验3编程练习2

    编制一个程序,将身份证号.txt 中的信息读入到内存中,输入一个身份证号或姓名,查询显示查询对象的姓名、身份证号、年龄、性别和出生地。

      1 package id;
      2 
      3 import java.io.BufferedReader;
      4 import java.io.File;
      5 import java.io.FileInputStream;
      6 import java.io.FileNotFoundException;
      7 import java.io.IOException;
      8 import java.io.InputStreamReader;
      9 import java.util.ArrayList;
     10 import java.util.Scanner;
     11 
     12 public class B{
     13     private static ArrayList<A> studentlist;
     14     public static void main(String[] args) {
     15         studentlist = new ArrayList<>();
     16         Scanner scanner = new Scanner(System.in);
     17         File file = new File("E:/身份证号.txt");
     18         try {
     19             FileInputStream fis = new FileInputStream(file);
     20             BufferedReader in = new BufferedReader(new InputStreamReader(fis));
     21             String temp = null;
     22             while ((temp = in.readLine()) != null) {
     23                 
     24                 Scanner linescanner = new Scanner(temp);
     25                 linescanner.useDelimiter(" ");    
     26                 String name = linescanner.next();
     27                 String number = linescanner.next();
     28                 String sex = linescanner.next();
     29                 String age = linescanner.next();
     30                 String province =linescanner.nextLine();
     31                 A student = new A();
     32                 student.setName(name);
     33                 student.setnumber(number);
     34                 student.setsex(sex);
     35                 student.setage(age);
     36                 student.setprovince(province);
     37                 studentlist.add(student);
     38 
     39             }
     40         } catch (FileNotFoundException e) {
     41             System.out.println("学生信息文件找不到");
     42             e.printStackTrace();
     43         } catch (IOException e) {
     44             System.out.println("学生信息文件读取错误");
     45             e.printStackTrace();
     46         }
     47         boolean isTrue = true;
     48         while (isTrue) {
     49 
     50             System.out.println("1.按姓名查询");
     51             System.out.println("2.按身份证号查询");
     52             System.out.println("3.退出");
     53             int nextInt = scanner.nextInt();
     54             switch (nextInt) {
     55             case 1:
     56                 System.out.println("请输入姓名");
     57                 String studentname = scanner.next();
     58                 int nameint = findStudentByname(studentname);
     59                 if (nameint != -1) {
     60                     System.out.println("身份证号:"
     61                             + studentlist.get(nameint).getnumber() + "    姓名:"
     62                             + studentlist.get(nameint).getName() +"    性别:"
     63                             +studentlist.get(nameint).getsex()   +"    年龄:"
     64                             +studentlist.get(nameint).getage()+"  地址:"
     65                             +studentlist.get(nameint).getprovince()
     66                             );
     67                 } else {
     68                     System.out.println("不存在该学生");
     69                 }
     70                 break;
     71             case 2:
     72                 System.out.println("请输入身份证号");
     73                 String studentid = scanner.next();
     74                 int idint = findStudentByid(studentid);
     75                 if (idint != -1) {
     76                     System.out.println("身份证号:"
     77                             + studentlist.get(idint ).getnumber() + "    姓名:"
     78                             + studentlist.get(idint ).getName() +"    性别:"
     79                             +studentlist.get(idint ).getsex()   +"    年龄:"
     80                             +studentlist.get(idint ).getage()+"   地址:"
     81                             +studentlist.get(idint ).getprovince()
     82                             );
     83                 } else {
     84                     System.out.println("不存在该学生");
     85                 }
     86                 break;
     87             case 3:
     88                 isTrue = false;
     89                 System.out.println("已退出");
     90                 break;
     91             default:
     92                 System.out.println("输入有误");
     93             }
     94         }
     95     }
     96 
     97 
     98     public static int findStudentByname(String name) {
     99         int flag = -1;
    100         int a[];
    101         for (int i = 0; i < studentlist.size(); i++) {
    102             if (studentlist.get(i).getName().equals(name)) {
    103                 flag= i;
    104             }
    105         }
    106         return flag;
    107 
    108     }
    109 
    110 
    111     public static int findStudentByid(String id) {
    112         int flag = -1;
    113 
    114         for (int i = 0; i < studentlist.size(); i++) {
    115             if (studentlist.get(i).getnumber().equals(id)) {
    116                 flag = i;
    117             }
    118         }
    119         return flag;
    120 
    121     }
    122     
    123     
    124 }
     1 package id;
     2 
     3 public class A {
     4 
     5     private String name;
     6     private    String number ;
     7     private    String sex ;
     8     private    String age;
     9     private    String province;
    10 
    11     
    12     public String getName() {
    13         return name;
    14     }
    15     public void setName(String name) {
    16         this.name = name;
    17     }
    18     public String getnumber() {
    19         return number;
    20     }
    21     public void setnumber(String number) {
    22         this.number = number;
    23     }
    24     public String getsex() {
    25         return sex ;
    26     }
    27     public void setsex(String sex ) {
    28         this.sex =sex ;
    29     }
    30     public String getage() {
    31         return age;
    32     }
    33     public void setage(String age ) {
    34         this.age=age ;
    35     }
    36     public String getprovince() {
    37         return province;
    38     }
    39     public void setprovince(String province) {
    40         this.province=province ;
    41     }
    42 
    43 
    44 }

    实验结果如下:

    实验总结:

    通过这次实验我学习到了java中继承的用法和作用,理解了继承的定义, 掌握子类的定义要求,掌握多态性的概念及用法, 掌握抽象类的定义及用途,掌握抽象类的定义方法及用途, 理解了ArrayList类的定义方法及用法, 知道了枚举类定义方法及用途。这次的实验让我们把书本上的知识运用到时间中让我们知道了自己的不足。

  • 相关阅读:
    快速排序中的partition函数的枢纽元选择,代码细节,以及其标准实现
    并发包的线程池第二篇--Executors的构造
    并发包的线程池第一篇--ThreadPoolExecutor执行逻辑
    Servlet学习笔记
    npm + webpack +react
    取消eclipse启动时的subclipse Usage弹窗
    关于webpack最好的文档
    WebStorm2016.1 破解 激活
    微信web调试工具
    webstorm下设置sass
  • 原文地址:https://www.cnblogs.com/dalacao/p/9749570.html
Copyright © 2020-2023  润新知