张季跃 201771010139《面向对象程序设计(java)》第六周学习总结

张季跃201771010139《面向对象程序设计(java)》第周学习总结

第一部分:理论知识学习部分

第五章 继承:

继承:用已有类来构建新类的一种机制。当定义了一个新类继承了一个类时,这个新类就继承了这个类的方法和域,同时在新类中添加新的方法和域以适应新的情况。l

继承是Java程序设计中的一项核心技术,也是 面向对象特征之一。

5.1l类、超类和子类

l1.类继承的格式:class  新类名extends已有类名

已有类称为:超类(superclass)、基类(base class) 或父类(parent  class)

–来自系统类库

 –用户自定义类

 新类称作:子类(subclass)、派生类(derived  class)或孩子类(child class)

l2.一般来说,子类比超类拥有的功能更加丰富3.2数据类型

   Java共有8种基本类型 :

  1. 继承层次:

l(1)从一个超类扩展而来的类集合称为继承层次 (inheritancehierarchy)。在继承层次中,从 某个类到其祖先的路径被称为该类的继承链 (inheritancechain)。 l

(2)Java不支持多继承

4. 动态绑定

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

5.阻止继承:final类和方法

l(1)不允许继承的类称为final类,在类的定义中用final修饰 符加以说明。 

2)类中的方法可定义为final的。这时子类就不能覆盖该方法。 l

(3)如果一个类声明为final,属于它的方法会被自动设为 final,但不包括域(如果域定义为final,在对象构造以 后,final域就不能再修改了)。 private  final  intMax = 100; l

(4)String类是final类的一个例子。不能扩展该类。

  1. 强制类型转换

l(1)如果要把一个超类对象赋给一个子类对象变量,就必须进 行强制类型转换。

2)类型转换必须在继承层次内进行;而且在超类到子类转换 之前,应先使用instanceof操作符进行继承链检查。

7.抽象类:

观察类的继承层次结构,位于上层的类更具通用性,甚至可能更加抽象。从某种角度看,祖先类更加通用,人们只将它作为派生其他类的基类,而不作为特定的实例类。

8.受保护访问:

(1)如果希望超类的某些方法或域允许被子类访问,就需要在超类定义时,将这些方法或域声明为protected。 l

(2)实际中要谨慎使用protected的属性。假设需要将设计的类 提供给其他程序员使用,而在这个类中设置了一些受保护 域,由于其他程序员可以由这个类再派生出新类,并访问 其中的受保护域。在这种情况下,如果对这个类进行修改 ,就必须通知所有使用这个类的程序员。这违背了OOP提倡 的数据封装原则。 l

(3)如果定义类时要限制某个方法的使用,就可以将它声明为protected。这表明子类得到信任,可以使用这个方法,而 其他类则不行。

5.2 Object:所有类的超类

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

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

(1)Object类中的equals方法用于测试某个对象是否同另一 个对象相等。它在Object类中的实现是判断两个对象是 否具有相同的引用。如果两个对象具有相同的引用,它 们一定是相等的。 l

(2)如果需要检测两个对象状态的相等性,就需要在新类的定义中需要覆盖equals方法。

(3)定义子类的equals方法时,可调用超类的equals方法。

  1. hashCode方法

(1)Object类中的hashCode方法导出某个对象的散列 码。散列码是任意整数,表示对象的存储地址。 l

(2)两个相等对象的散列码相等。

  1. toString方法

(1)Object类的toString方法返回一个代表该对象域值的字符串。 l

  1. toString方法返回字符串的格式:类名,然后在方括号中 列举域值。 l
  2. 通过getClass().getName()获得类名的字符串。
  3. toString的调用方式: l

(a)一个字符串与对象名通过操作符“+”连接起来,就会 自动调用toString方法。 

(b)如果x是任意一个对象,调用System.out.println(x), 就会直接地调用x.toString(),并打印输出字符串。 l

(2)定义子类的toString方法时,可先调用超类的toString方法。 

(3)toString方法是非常重要的调试工具。标准类库中,多数类 定义了toString方法,以便用户获得对象状态的必要信息。

5.3泛型数组列表

l      1.Java中,利用ArrayList类,可允许程序在运行 时确定数组的大小。 l

  1. ArryList是一个采用类型参数的泛型类。为指定 数组列表保存元素的对象类型,需要用一对尖括 号将数组元素对象类名括起来加在后面。 
  2. 没有<>的ArrayList将被认为是一个删去了类型 参数的“原始”类型  
  3. 数组列表的操作

(1)a.ArrayList定义 l

  1. ArrayList<T> 对象=new ArrayList<T>();
  2. API:ArrayList的构造器 –ArrayList<T>()构造一个空数组列表 –ArrayList<T>(intinitialCapacity)构造一个具有 指定容量的空数组列表。

           2)API:booleanadd(T obj) 把元素obj追加到数组列表的结尾

           3)API:intsize() 返回数组列表中当前元素个数

           4)API:void  trimToSize() 把数组列表的存贮空间调整到当前大小

           5)API:void set(intindex, T obj) 将obj放入数组列表index位置,将覆盖这 个位置的原有内容。 

API:T get(intindex)            获得指定位置index的元素值

           6)API:booleanadd(intindex, T obj) 向后移动元素,在第n 个位置插入obj 

API:T remove(intindex); 将第n个位置存放的对象删除,并将后面的 元素向前移

5.4 对象包装器与自动打包

l    1.所有基本数据类型都有着与之对应的预定义类,它们被称 为对象包装器(wrapper)。 l

  1. 以下前6个对象包装器类都是从公共包装器类Number继承 而来。 Integer  Long  Float   Double   Short    Byte  Character   VoidBoolean l
  2. 对象包装器类是不可变的,即一旦构造了包装器,就不允许更改包装在其中的值。且对象包装器类还是final,因 此不能定义它们的子类。 l
  3. 使用对象包装器的好处:–基本类型转化为对象 –定义一些有用的基本方法(static方法)

JavaSE5.0中,可以自动的将基本数据类型转换 为包装器类的对象,将这种变换称为自动打包 (autoboxing)。

  1. 相反地,当对一个包装器类的对象进行赋值或算法运算时,将会自动地拆包。
  2. 打包和拆包是编译器认可的

5.5 参数数量可变的方法

l    1.在Java SE 5.0以前的版本中,每个Java方法都 有固定数量的参数。然而,现在的版本提供了可 以用可变的参数数量调用的方法(称为“可变参 ”方法)。

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

5.6 枚举类

      1.声明枚举类:它包括一个关键字enum,一个新枚举类型的名字 Grade以及为Grade定义的一组值,这里的值既 非整型,亦非字符型。

5.7 继承设计的技巧

①将公共操作和域放在超类。 

②不要使用受保护的域。

③使用继承实现“is-a”关系。 

④除非所有继承的方法都有意义,否则就不要 使用继承。 

⑤在覆盖方法时,不要改变预期的行为。 

⑥使用多态,而非类型信息。 ⑦不要过多地使用反射。

 

第二部分:实验部分

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类的关系子类的用途,并在代码中添加注释。

   测试程序:

package inheritance;

import java.time.*;

public class Employee

{

   private String name;

   private double salary;

   private LocalDate hireDay;

   public Employee(String name, double salary, int year, int month, int day)

   {

      this.name = name;

      this.salary = salary;

      hireDay = LocalDate.of(year, month, day);

   }

   public String getName()

   {

      return name;

   }

   public double getSalary()

   {

      return salary;

   }

   public LocalDate getHireDay()

   {

      return hireDay;

   }

   public void raiseSalary(double byPercent)

   {

      double raise = salary * byPercent / 100;

      salary += raise;

   }

}

package inheritance;

public class Manager extends Employee

{

   private double bonus;

   /**

    * @param name the employee's name

    * @param salary the salary

    * @param year the hire year

    * @param month the hire month

    * @param day the hire day

    */

   public Manager(String name, double salary, int year, int month, int day)

   {

      super(name, salary, year, month, day);

      bonus = 0;

   }

   public double getSalary()

   {

      double baseSalary = super.getSalary();

      return baseSalary + bonus;

   }

   public void setBonus(double b)

   {

      bonus = b;

   }

}

package inheritance;

/**

 * This program demonstrates inheritance.

 * @version 1.21 2004-02-21

 * @author Cay Horstmann

 */

public class ManagerTest

{

   public static void main(String[] args)

   {

      // construct a Manager object

      Manager boss = new Manager("Carl Cracker", 80000, 1987, 12, 15);

      boss.setBonus(5000);

      Employee[] staff = new Employee[3];

      // fill the staff array with Manager and Employee objects

      staff[0] = boss;

      staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1);

      staff[2] = new Employee("Tommy Tester", 40000, 1990, 3, 15);

      // print out information about all Employee objects

      for (Employee e : staff)

         System.out.println("name=" + e.getName() + ",salary=" + e.getSalary());

   }

}

测试结果:

 

测试程序2:

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

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

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

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

   测试程序:

package abstractClasses;

import java.time.*;

public class Employee extends Person

{

   private double salary;

   private LocalDate hireDay;

   public Employee(String name, double salary, int year, int month, int day)

   {

      super(name);

      this.salary = salary;

      hireDay = LocalDate.of(year, month, day);

   }

   public double getSalary()

   {

      return salary;

   }

   public LocalDate getHireDay()

   {

      return hireDay;

   }

   public String getDescription()

   {

      return String.format("an employee with a salary of $%.2f", salary);

   }

   public void raiseSalary(double byPercent)

   {

      double raise = salary * byPercent / 100;

      salary += raise;

   }

}

package abstractClasses;

public abstract class Person

{

   public abstract String getDescription();

   private String name;

   public Person(String name)

   {

      this.name = name;

   }

   public String getName()

   {

      return name;

   }

}

package abstractClasses;

/**

 * This program demonstrates abstract classes.

 * @version 1.01 2004-02-21

 * @author Cay Horstmann

 */

public class PersonTest

{

   public static void main(String[] args)

   {

      Person[] people = new Person[2];

      // fill the people array with Student and Employee objects

      people[0] = new Employee("Harry Hacker", 50000, 1989, 10, 1);

      people[1] = new Student("Maria Morris", "computer science");

      // print out names and descriptions of all Person objects

      for (Person p : people)

         System.out.println(p.getName() + ", " + p.getDescription());

   }

}

package abstractClasses;

public class Student extends Person

{

   private String major;

   /**

    * @param nama the student's name

    * @param major the student's major

    */

   public Student(String name, String major)

   {

      // pass n to superclass constructor

      super(name);

      this.major = major;

   }

   public String getDescription()

   {

      return "a student majoring in " + major;

   }

}

测试结果:

 

测试程序3:

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

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

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

   测试程序:

package equals;

import java.time.*;

import java.util.Objects;

public class Employee

{

   private String name;

   private double salary;

   private LocalDate hireDay;

   public Employee(String name, double salary, int year, int month, int day)

   {

      this.name = name;

      this.salary = salary;

      hireDay = LocalDate.of(year, month, day);

   }

   public String getName()

   {

      return name;

   }

   public double getSalary()

   {

      return salary;

   }

   public LocalDate getHireDay()

   {

      return hireDay;

   }

   public void raiseSalary(double byPercent)

   {

      double raise = salary * byPercent / 100;

      salary += raise;

   }

   public boolean equals(Object otherObject)

   {

      // a quick test to see if the objects are identical

      if (this == otherObject) return true;

      // must return false if the explicit parameter is null

      if (otherObject == null) return false;

      // if the classes don't match, they can't be equal

      if (getClass() != otherObject.getClass()) return false;

      // now we know otherObject is a non-null Employee

      Employee other = (Employee) otherObject;

      // test whether the fields have identical values

      return Objects.equals(name, other.name) && salary == other.salary && Objects.equals(hireDay, other.hireDay);

   }

   public int hashCode()

   {

      return Objects.hash(name, salary, hireDay); 

   }

   public String toString()

   {

      return getClass().getName() + "[name=" + name + ",salary=" + salary + ",hireDay=" + hireDay

            + "]";

   }

}

package equals;

/**

 * This program demonstrates the equals method.

 * @version 1.12 2012-01-26

 * @author Cay Horstmann

 */

public class EqualsTest

{

   public static void main(String[] args)

   {

      Employee alice1 = new Employee("Alice Adams", 75000, 1987, 12, 15);

      Employee alice2 = alice1;

      Employee alice3 = new Employee("Alice Adams", 75000, 1987, 12, 15);

      Employee bob = new Employee("Bob Brandson", 50000, 1989, 10, 1);

      System.out.println("alice1 == alice2: " + (alice1 == alice2));

      System.out.println("alice1 == alice3: " + (alice1 == alice3));

      System.out.println("alice1.equals(alice3): " + alice1.equals(alice3));

      System.out.println("alice1.equals(bob): " + alice1.equals(bob));

      System.out.println("bob.toString(): " + bob);

      Manager carl = new Manager("Carl Cracker", 80000, 1987, 12, 15);

      Manager boss = new Manager("Carl Cracker", 80000, 1987, 12, 15);

      boss.setBonus(5000);

      System.out.println("boss.toString(): " + boss);

      System.out.println("carl.equals(boss): " + carl.equals(boss));

      System.out.println("alice1.hashCode(): " + alice1.hashCode());

      System.out.println("alice3.hashCode(): " + alice3.hashCode());

      System.out.println("bob.hashCode(): " + bob.hashCode());

      System.out.println("carl.hashCode(): " + carl.hashCode());

   }

}

package equals;

public class Manager extends Employee

{

   private double bonus;

   public Manager(String name, double salary, int year, int month, int day)

   {

      super(name, salary, year, month, day);

      bonus = 0;

   }

   public double getSalary()

   {

      double baseSalary = super.getSalary();

      return baseSalary + bonus;

   }

   public void setBonus(double bonus)

   {

      this.bonus = bonus;

   }

   public boolean equals(Object otherObject)

   {

      if (!super.equals(otherObject)) return false;

      Manager other = (Manager) otherObject;

      // super.equals checked that this and other belong to the same class

      return bonus == other.bonus;

   }

   public int hashCode()

   {

      return java.util.Objects.hash(super.hashCode(), bonus);

   }

   public String toString()

   {

      return super.toString() + "[bonus=" + bonus + "]";

   }

}

测试结果:

测试程序4:

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

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

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

   测试程序:

package arrayList;

import java.util.*;

/**

 * This program demonstrates the ArrayList class.

 * @version 1.11 2012-01-26

 * @author Cay Horstmann

 */

public class ArrayListTest

{

   public static void main(String[] args)

   {

      // fill the staff array list with three Employee objects

      ArrayList<Employee> staff = new ArrayList<>();

      staff.add(new Employee("Carl Cracker", 75000, 1987, 12, 15));

      staff.add(new Employee("Harry Hacker", 50000, 1989, 10, 1));

      staff.add(new Employee("Tony Tester", 40000, 1990, 3, 15));

      // raise everyone's salary by 5%

      for (Employee e : staff)

         e.raiseSalary(5);

      // print out information about all Employee objects

      for (Employee e : staff)

         System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay="

               + e.getHireDay());

   }

}

package arrayList;

import java.time.*;

public class Employee

{

   private String name;

   private double salary;

   private LocalDate hireDay;

   public Employee(String name, double salary, int year, int month, int day)

   {

      this.name = name;

      this.salary = salary;

      hireDay = LocalDate.of(year, month, day);

   }

   public String getName()

   {

      return name;

   }

   public double getSalary()

   {

      return salary;

   }

   public LocalDate getHireDay()

   {

      return hireDay;

   }

   public void raiseSalary(double byPercent)

   {

      double raise = salary * byPercent / 100;

      salary += raise;

   }

}

测试结果:

测试程序5:

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

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

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

测试程序:

package enums;

import java.util.*;

/**

 * This program demonstrates enumerated types.

 * @version 1.0 2004-05-24

 * @author Cay Horstmann

 */

public class EnumTest

{  

   public static void main(String[] args)

   {  

      Scanner in = new Scanner(System.in);

      System.out.print("Enter a size: (SMALL, MEDIUM, LARGE, EXTRA_LARGE) ");

      String input = in.next().toUpperCase();

      Size size = Enum.valueOf(Size.class, input);

      System.out.println("size=" + size);

      System.out.println("abbreviation=" + size.getAbbreviation());

      if (size == Size.EXTRA_LARGE)

         System.out.println("Good job--you paid attention to the _.");      

   }

}

enum Size

{

   SMALL("S"), MEDIUM("M"), LARGE("L"), EXTRA_LARGE("XL");

   private Size(String abbreviation) { this.abbreviation = abbreviation; }

   public String getAbbreviation() { return abbreviation; }

   private String abbreviation;

}

测试结果:

实验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方法放在哪个类中更合适?

实验程序:

Shape:

package shape;

abstract class Shape { 

abstract double getPerimeter(); 

abstract double getArea(); 

}

class Rectangle extends Shape{ 

private int length;

private int width;

public Rectangle(int length, int width) {

    this.length = length;

    this.width = width;

}

double getPerimeter(){ 

return 2*(length+width);

}

double getArea(){

return length*width;

}

}

class Circle extends Shape{

private int radius;

public Circle(int radius) {

    this.radius = radius;

}

double getPerimeter(){

return 2 * Math.PI * radius;

}

double getArea(){

return Math.PI * radius * radius;

}

}

Test:

package shape;

import java.util.Scanner;

public class Test {

 public static void main(String[] args) {

  Scanner in = new Scanner(System.in);

  System.out.println("个数");

  int a = in.nextInt();

  System.out.println("种类");

  String rect="rect";

        String cir="cir";

  Shape[] num=new Shape[a];

  for(int i=0;i<a;i++){

   String input=in.next();

   if(input.equals(rect)) {

   System.out.println("长和宽");

   int length = in.nextInt();

   int width = in.nextInt();

         num[i]=new Rectangle(width,length);

         System.out.println("Rectangle["+"length:"+length+"  width:"+width+"]");

         }

   if(input.equals(cir)) {

         System.out.println("半径");

      int radius = in.nextInt();

      num[i]=new Circle(radius);

      System.out.println("Circle["+"radius:"+radius+"]");

         }

         }

         Test c=new Test();

         System.out.println("求和");

         System.out.println(c.sumAllPerimeter(num));

         System.out.println(c.sumAllArea(num));

         

         for(Shape s:num) {

             System.out.println(s.getClass()+","+s.getClass().getSuperclass());

             }

         }

           public double sumAllArea(Shape score[])

           {

           double sum=0;

           for(int i=0;i<score.length;i++)

               sum+= score[i].getArea();

               return sum;

           }

           public double sumAllPerimeter(Shape score[])

           {

           double sum=0;

           for(int i=0;i<score.length;i++)

               sum+= score[i].getPerimeter();

               return sum;

           }    

}

实验结果:

 

实验3: 编程练习2

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

实验程序:

package 实验3;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.ArrayList;

import java.util.Scanner;

public class ID{

    private static ArrayList<Student> studentlist;

    public static void main(String[] args) {

        studentlist = new ArrayList<>();

        Scanner scanner = new Scanner(System.in);

        File file = new File("C:\\Users\\张季跃\\Desktop\\第六周实验报告\\身份证号.txt");

        try {

            FileInputStream fis = new FileInputStream(file);

            BufferedReader in = new BufferedReader(new InputStreamReader(fis));

            String temp = null;

            while ((temp = in.readLine()) != null) {

                

                Scanner linescanner = new Scanner(temp);

                

                linescanner.useDelimiter(" ");    

                String name = linescanner.next();

                String number = linescanner.next();

                String sex = linescanner.next();

                String year = linescanner.next();

                String province =linescanner.nextLine();

                Student student = new Student();

                student.setName(name);

                student.setnumber(number);

                student.setsex(sex);

                student.setyear(year);

                student.setprovince(province);

                studentlist.add(student);

            }

        } catch (FileNotFoundException e) {

            System.out.println("无导入文件");

            e.printStackTrace();

        } catch (IOException e) {

            System.out.println("文件出错");

            e.printStackTrace();

        }

        boolean isTrue = true;

        while (isTrue) {

            System.out.println("请选择你使用的查询方式");

            System.out.println("1.姓名查询");

            System.out.println("2.身份证号查询");

            System.out.println("3.退出");

            int nextInt = scanner.nextInt();

            switch (nextInt) {

            case 1:

                System.out.println("姓名:");

                String studentname = scanner.next();

                int nameint = findStudentByname(studentname);

                if (nameint != -1) {

                    System.out.println("    姓名:"

                            + studentlist.get(nameint).getName() +"身份证号:"

                            + studentlist.get(nameint).getnumber() + "    性别:"

                            +studentlist.get(nameint).getsex()   +"    年龄:"

                            +studentlist.get(nameint).getyaer()+"  地址:"

                            +studentlist.get(nameint).getprovince()

                            );

                } else {

                    System.out.println("查无此人");

                }

                break;

            case 2:

                System.out.println("身份证号:");

                String studentid = scanner.next();

                int idint = findStudentByid(studentid);

                if (idint != -1) {

                    System.out.println( "    姓名:"

                            + studentlist.get(idint ).getName() +"     身份证号:"

                            + studentlist.get(idint ).getnumber() +"    性别:"

                            +studentlist.get(idint ).getsex()   +"    年龄:"

                            +studentlist.get(idint ).getyaer()+"   地址:"

                            +studentlist.get(idint ).getprovince()

                            );

                } else {

                    System.out.println("查无此人");

                }

                break;

            case 3:

                isTrue = false;

                System.out.println("欢迎下次使用!");

                break;

            default:

                System.out.println("输入错误");

            }

        }

    }

    public static int findStudentByname(String name) {

        int flag = -1;

        int a[];

        for (int i = 0; i < studentlist.size(); i++) {

            if (studentlist.get(i).getName().equals(name)) {

                flag= i;

            }

        }

        return flag;

    }

    public static int findStudentByid(String id) {

        int flag = -1;

        for (int i = 0; i < studentlist.size(); i++) {

            if (studentlist.get(i).getnumber().equals(id)) {

                flag = i;

            }

        }

        return flag;

    }   

}

package 实验3;

public class Student {

    private String name;

    private String number ;

    private String sex ;

    private String year;

    private String province;

   

    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }

    public String getnumber() {

        return number;

    }

    public void setnumber(String number) {

        this.number = number;

    }

    public String getsex() {

        return sex ;

    }

    public void setsex(String sex ) {

        this.sex =sex ;

    }

    public String getyaer() {

        return year;

    }

    public void setyear(String year ) {

        this.year=year ;

    }

    public String getprovince() {

        return province;

    }

    public void setprovince(String province) {

        this.province=province ;

    }

}

实验结果:

 

第三部分:实验总结:

     在本周的学习中,我对继承有了初步的了解,知道了它的优点和缺点,并对继承的简单应用有了初步的掌握。

但尽管有老师的讲解和助教的指导,而且我在十一也有过学习,但我对于继承在实际中的应用还是十分不熟练,特别是在本次的实验中关于实验二和实验三继承部分的应用让我伤透了脑筋,自己编写的程序总是运行不过去,到最后只好参考一下其他同学,即使如此,也有搞不懂的地方,只好到学校后再请教同学。

猜你喜欢

转载自www.cnblogs.com/Alex-Morcer/p/9750243.html