zoukankan      html  css  js  c++  java
  • 201871010109胡欢欢《面向对象程序设计(java)》第八周学习总结 201871010109

    博文正文开头:(2分)

    项目

    内容

    这个作业属于哪个课程

     https://www.cnblogs.com/nwnu-daizh/

    这个作业的要求在哪里

       https://www.cnblogs.com/nwnu-daizh/p/11435127.html

    作业学习目标

    1. (1) 掌握接口定义方法;

      (2) 掌握实现接口类的定义要求;

      (3) 掌握实现了接口类的使用要求;

      (4) 掌握程序回调设计模式;

      (5) 掌握Comparator接口用法;

      (6) 掌握对象浅层拷贝与深层拷贝方法;

      (7) 掌握Lambda表达式语法;

      (8) 了解内部类的用途及语法要求。

     

     

     

     

     

     

     

     

     

     

     

    第一部分:总结第五章理论知识(30分)

    Java中类于类之间的关系以及代码示例:
    一、继承关系

    继承指的是一个类继承另外的一个类的功能,在继承中可以增加它自己的新功能。继承关系通过关键字extends明确标识。
    二、实现关系

    实现指一个class类实现interface接口的功能,实现是类与接口之间最常见的关系。在Java中此类关系通过关键字implements明确标识。
    三、依赖关系

    依赖就是一个类A使用到了另一个类B,这种关系是具有偶然性的、临时性的,类B的变化会影响到类A。
    四、关联关系

    关联体现的是两个类之间语义级别的一种强依赖关系,比依赖更强、不存在依赖关系的偶然性、临时性,而且双方的关系一般是平等的。关联可以单向、双向。表现在代码层面,为被关联类B以类的属性形式出现在关联类A中,也可能是关联类A引用了一个类型为被关联类B的全局变量。
    五、聚合关系

    聚合是关联关系的一种特例,它体现的是整体与部分的关系,即has-a的关系。整体与部分之间是可分离的,它们可以具有各自的生命周期,部分可以属于多个整体对象,也可以为多个整体对象共享。
    六、组合关系

    组合也是关联关系的一种特例,它体现的是一种contains-a的关系,这种关系比聚合更强,也称为强聚合。它同样体现整体与部分间的关系,但此时整体与部分是不可分的,整体的生命周期结束也就意味着部分的生命周期结束,比如人和人的大脑。
        注意: 继承与实现是类于类,或者类与接口之间的一种纵向关系,其他的四种关系体现的是类和类、或者类与接口间的引用、横向关系,是比较难区分的,有很多事物间的关系要想准确定位是很难的。前面也提到,这四种关系都是语义级别的,所以从代码层面并不能完全区分各种关系,但总的来说,后几种关系所表现的强弱程度依次为:组合>聚合>关联>依赖。
    1、接口:Java为了解决继承不可以多继承,使用了接口,一个类可以实现一个或者多个接口;
    2、Java中,接口不是类,而是对类的一组需求描述 ,由常量和一组抽象方法组成;
    3、接口中不包括变量和具体方法的实现;
    4、只要类实现了接口,则该类要遵从接口描述的统一格式进行定义,并且可以在任何需要接口的地方使用这个类得对象;

    七、接口与回调

    回调:回调是一种双向的调用模式,也就是说,被调用的接口被调用时也会调用对方的接口,例如A要调用B,B在执行完又要调用A。

    八、Comparator接口

    java.utils包下的Comparator接口。

    该接口代表一个比较器,java数组工具类和集合工具类中提供对sort方法排序就是使用Comparator接口来处理排序的。

    Comparator接口中有一个方法int compare(T o1, T o2)。这个方法返回值是int类型,如果返回值小于0,说明比较结果是o1<o2,如果返回值等于0,说明比较结果是o1=o2,如果返回值大于0,则说明比较结果是o1>o2。既然是接口,那么我们就可以实现它,来自定义其中对比较规则,即可实现在一个List列表中将元素按照某个属性进行排序。

    九、 对象克隆

    克隆:

    两种不同的克隆方法:浅克隆(shallowclone)和深克隆(deepclone) 。在Java语言中,数据类型分为基本数据类型和引用类型,其中引用数据类型主要包括类、接口、数组等复杂的数据类型。浅克隆和深克隆的主要区别在于是否支持引用类型的成员变量的复制。

    1、浅克隆的一般步骤:

    1).被复制的类需要实现Clonable接口。该接口为标记接口,里边不含任何的方法。

    2).覆盖clone()方法,访问修饰符设为public,方法中调用super.clone()方法得到需要的复制对象。

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

    测试程序1:

    l 编辑、编译、调试运行阅读教材214页-215页程序6-1、6-2,理解程序并分析程序运行结果;

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

    l 掌握接口的实现用法;

    l 掌握内置接口Compareable的用法。

    例题6.1程序代码如下:

    package interfaces;
    
    import java.util.*;
    
    /**
     * This program demonstrates the use of the Comparable interface.//这个程序演示了可比较接口的使用
     * @version 1.30 2004-02-27
     * @author Cay Horstmann
     */
    public class EmployeeSortTest
    {
       public static void main(String[] args)
       {
          var staff = new Employee[3];
          //新建一个Employee 数组对象 给staff所引用,数组大小为三
          staff[0] = new Employee("Harry Hacker", 35000);
          staff[1] = new Employee("Carl Cracker", 75000);
          staff[2] = new Employee("Tony Tester", 38000);
    
          Arrays.sort(staff);//数组排序
    
          // print out information about all Employee objects
          //打印所有员工对象的信息
          for (Employee e : staff)
             System.out.println("name=" + e.getName() + ",salary=" + e.getSalary());
       }
    }

    运行结果:

    测试示例程序:

    interface  A
    {
      double g=9.8;
      void show( );
    }
    class C implements A
    {
      public void show( )
      {System.out.println("g="+g);}
    }
    
    class InterfaceTest
    {
      public static void main(String[ ] args)
      {
           A a=new C( );
           a.show( );
           System.out.println("g="+C.g);
      }
    }

    测试程序3:

    l 在elipse IDE中调试运行教材223页6-3,结合程序运行结果理解程序;

    l 26行、36行代码参阅224页,详细内容涉及教材12章。

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

    掌握回调程序设计模式;

    例题6.3程序代码如下:

    package timer;
    
    /**
       @version 1.02 2017-12-14
       @author Cay Horstmann
    */
    
    import java.awt.*;
    import java.awt.event.*;
    import java.time.*;
    import javax.swing.*;
    
    public class TimerTest
    {  
       public static void main(String[] args)
       {  
          var listener = new TimePrinter();
    
          // construct a timer that calls the listener构造一个调用侦听器的计时器
          // once every second每秒一次
          var timer = new Timer(1000, listener);
          timer.start();//构造一个定时器,
    
          // keep program running until the user selects "OK"使时间一直运行直到使用者停止
          JOptionPane.showMessageDialog(null, "Quit program?");//程序是否终止窗口
          System.exit(0);
       }
    }
    
    class TimePrinter implements ActionListener
    {  
       public void actionPerformed(ActionEvent event)
       {  
          System.out.println("At the tone, the time is " //输出这一刻的时间
             + Instant.ofEpochMilli(event.getWhen()));
          Toolkit.getDefaultToolkit().beep();//获得默认的工具箱,得到信息并发出一声铃响;
       }
    }

    测试程序4:

    l 调试运行教材229页-231页程序6-4、6-5,结合程序运行结果理解程序;

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

    l 掌握对象克隆实现技术;

    掌握浅拷贝和深拷贝的差别。

    例题6.4程序代码如下:

    package clone;
    
    /**
     * This program demonstrates cloning.//这个程序演示了克隆
     * @version 1.11 2018-03-16
     * @author Cay Horstmann
     */
    public class CloneTest
    {
       public static void main(String[] args) throws CloneNotSupportedException
       {
          var original = new Employee("John Q. Public", 50000);
          original.setHireDay(2000, 1, 1);
          Employee copy = original.clone();//对原先数据进行克隆
          copy.raiseSalary(10);
          copy.setHireDay(2002, 12, 31);
          System.out.println("original=" + original);//输出原始数据
          System.out.println("copy=" + copy);//输出克隆后的数据
       }
    }

    例题6.5程序代码:

    package clone;
    
    import java.util.Date;
    import java.util.GregorianCalendar;
    
    public class Employee implements Cloneable
    {
       private String name;
       private double salary;
       private Date hireDay;
    
       public Employee(String name, double salary)
       {
          this.name = name;
          this.salary = salary;
          hireDay = new Date();
       }
    
       public Employee clone() throws CloneNotSupportedException
       {
          // call Object.clone()克隆程序
          Employee cloned = (Employee) super.clone();
    
          // clone mutable fields克隆可变字段
          cloned.hireDay = (Date) hireDay.clone();
    
          return cloned;
       }
    
       /**
        * Set the hire day to a given date. 将租用日期设置为给定日期
        * @param year the year of the hire day
        * @param month the month of the hire day
        * @param day the day of the hire day
        */
       public void setHireDay(int year, int month, int day)
       {
          Date newHireDay = new GregorianCalendar(year, month - 1, day).getTime();
          
          // example of instance field mutation
          //实例字段变异示例
          hireDay.setTime(newHireDay.getTime());
       }
    
       public void raiseSalary(double byPercent)
       {
          double raise = salary * byPercent / 100;
          salary += raise;
       }
    
       public String toString()
       {
          return "Employee[name=" + name + ",salary=" + salary + ",hireDay=" + hireDay + "]";
       }
    }

    实验2 导入第6章示例程序6-6,学习Lambda表达式用法。

    l 调试运行教材233页-234页程序6-6,结合程序运行结果理解程序;

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

    将27-29行代码与教材223页程序对比,将27-29行代码与此程序对比,体会Lambda表达式的优点

    例题6.6程序代码如下:

    package lambda;
    
    import java.util.*;
    
    import javax.swing.*;
    import javax.swing.Timer;
    
    /**
     * This program demonstrates the use of lambda expressions.这个程序演示了lambda表达式的使用
     * @version 1.0 2015-05-12
     * @author Cay Horstmann
     */
    public class LambdaTest
    {
       public static void main(String[] args)
       {
          var planets = new String[] { "Mercury", "Venus", "Earth", "Mars", 
             "Jupiter", "Saturn", "Uranus", "Neptune" };
          System.out.println(Arrays.toString(planets));
          System.out.println("Sorted in dictionary order:");
          Arrays.sort(planets);
          System.out.println(Arrays.toString(planets));
          System.out.println("Sorted by length:");
          Arrays.sort(planets, (first, second) -> first.length() - second.length());
          System.out.println(Arrays.toString(planets));
                
          var timer = new Timer(1000, event ->
             System.out.println("The time is " + new Date()));
          timer.start();//构造一个定时器,   
             
          // keep program running until user selects "OK"使时间一直运行直到使用者停止
          JOptionPane.showMessageDialog(null, "Quit program?");//程序是否终止窗口
          System.exit(0);         
       }
    }

    实验3: 编程练习

    编制一个程序,将身份证号.txt 中的信息读入到内存中;

    l 按姓名字典序输出人员信息;

    l 查询最大年龄的人员信息;

    l 查询最小年龄人员信息;

    输入你的年龄,查询身份证号.txt中年龄与你最近人的姓名、身份证号、年龄、性别和出生地;

    l 查询人员中是否有你的同乡。

    实验代码如下:

    package week8;
    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.Arrays;
    import java.util.Collections;
    import java.util.Scanner;
    
    public class Main{
        private static ArrayList<Person> Personlist;
        public static void main(String[] args) {
             Personlist = new ArrayList<>();
            Scanner scanner = new Scanner(System.in);
            File file = new File("D:\\java\\身份证号.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 ID = linescanner.next();
                    String sex = linescanner.next();
                    String age = linescanner.next();
                    String place =linescanner.nextLine();
                    Person  people = new Person();
                    people.setname(name);
                    people.setID(ID);
                    people.setsex(sex);
                    int a = Integer.parseInt(age);
                    people.setage(a);
                    people.setbirthplace(place);
                    Personlist.add(people);
    
                }
            } 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:查询人员中是否有你的老乡");
                System.out.println("4:输入你的年龄,查询年龄与你最近人的所有信息");
                System.out.println("5:退出程序");
                
                int nextInt = scanner.nextInt();
                switch (nextInt) {
                case 1:
                    Collections.sort( Personlist);
                    System.out.println( Personlist.toString());
                    break;
                case 2:
                    
                    int max=0,min=100;int j,k1 = 0,k2=0;
                    for(int i=1;i< Personlist.size();i++)
                    {
                        j= Personlist.get(i).getage();
                       if(j>max)
                       {
                           max=j; 
                           k1=i;
                       }
                       if(j<min)
                       {
                           min=j; 
                           k2=i;
                       }
    
                    }  
                    System.out.println("年龄最大:"+ Personlist.get(k1));
                    System.out.println("年龄最小:"+ Personlist.get(k2));
                    break;
                case 3:
                    System.out.println("place?");
                    String find = scanner.next();        
                    String place=find.substring(0,3);
                    String place2=find.substring(0,3);
                    for (int i = 0; i < Personlist.size(); i++) 
                    {
                        if( Personlist.get(i).getbirthplace().substring(1,4).equals(place)) 
                            System.out.println(Personlist.get(i));
    
                    } 
    
                    break;
                case 4:
                    System.out.println("年龄:");
                    int yourage = scanner.nextInt();
                    int near=agenear(yourage);
                    int d_value=yourage-Personlist.get(near).getage();
                    System.out.println(""+Personlist.get(near));
               /*     for (int i = 0; i < Peoplelist.size(); i++)
                    {
                        int p=Personlist.get(i).getage()-yourage;
                        if(p<0) p=-p;
                        if(p==d_value) System.out.println(Peoplelist.get(i));
                    }   */
                    break;
                case 5:
               isTrue = false;
               System.out.println("退出程序!");
                    break;
                default:
                    System.out.println("输入有误");
                }
            }
        }
        public static int agenear(int age) {
         
           int min=25,d_value=0,k=0;
            for (int i = 0; i <  Personlist.size(); i++)
            {
                d_value= Personlist.get(i).getage()-age;
                if(d_value<0) d_value=-d_value; 
                if (d_value<min) 
                {
                   min=d_value;
                   k=i;
                }
    
             }    return k;
            
         }
    
     
    }
    复制代码
    复制代码
    package week8;
    
    public  class  Person implements Comparable<Person > {
    private String name;
    private String ID;
    private int age;
    private String sex;
    private String birthplace;
    
    public String getname() {
    return name;
    }
    public void setname(String name) {
    this.name = name;
    }
    public String getID() {
    return ID;
    }
    public void setID(String ID) {
    this.ID= ID;
    }
    public int getage() {
    
    return age;
    }
    public void setage(int age) {
        // int a = Integer.parseInt(age);
    this.age= age;
    }
    public String getsex() {
    return sex;
    }
    public void setsex(String sex) {
    this.sex= sex;
    }
    public String getbirthplace() {
    return birthplace;
    }
    public void setbirthplace(String birthplace) {
    this.birthplace= birthplace;
    }
    
    public int compareTo(Person  o) {
       return this.name.compareTo(o.getname());
    
    }
    
    public String toString() {
        return  name+"\t"+sex+"\t"+age+"\t"+ID+"\t"+birthplace+"\n";
        }

    运行结果如下:

    实验总结:通过本次实验,我基本掌握了接口的概念和实现方法,能够理解这一部分知识,也学会了接口的简单实用,接口和继承存在一定的相似性,但是接口可以支持多继承。初步的了解了克隆的概念。在实验的过程中,加深了对接口的定义以及其方法的实现的理解。自己的编程能力得到了提升。

    package interfaces;
    
    import java.util.*;
    
    /**
     * This program demonstrates the use of the Comparable interface.//这个程序演示了可比较接口的使用
     * @version 1.30 2004-02-27
     * @author Cay Horstmann
     */
    public class EmployeeSortTest
    {
       public static void main(String[] args)
       {
          var staff = new Employee[3];
          //新建一个Employee 数组对象 给staff所引用,数组大小为三
          staff[0] = new Employee("Harry Hacker", 35000);
          staff[1] = new Employee("Carl Cracker", 75000);
          staff[2] = new Employee("Tony Tester", 38000);
    
          Arrays.sort(staff);//数组排序
    
          // print out information about all Employee objects
          //打印所有员工对象的信息
          for (Employee e : staff)
             System.out.println("name=" + e.getName() + ",salary=" + e.getSalary());
       }
    }
  • 相关阅读:
    hdu 4114 Disney's FastPass 状压dp
    lightoj 1381
    bzoj 2428: [HAOI2006]均分数据 随机化
    bzoj 3969: [WF2013]Low Power 二分
    套题:wf2013 (1/8)
    hdu 4119 Isabella's Message 模拟题
    hdu 4118 Holiday's Accommodation 树形dp
    UESTC 2015dp专题 N 导弹拦截 dp
    UESTC 2015dp专题 j 男神的约会 bfs
    UESTC 2015dp专题 H 邱老师选妹子 数位dp
  • 原文地址:https://www.cnblogs.com/1763088787h/p/11716021.html
Copyright © 2011-2022 走看看