201771010135杨蓉庆《面对对象程序设计(java)》第九周学习总结(代码片段)

yrqy yrqy     2023-01-12     676

关键词:

第7章 异常、日志、断言和调试

1、实验目的与要求

(1) 掌握java异常处理技术;

(2) 了解断言的用法;

(3) 了解日志的用途;

(4) 掌握程序基础调试技巧;

一、理论知识

1、异常:在程序的执行过程中所发生的异常事件,它 中断指令的正常执行。

2、Java的异常处理机制可以控制程序从错误产生的 位置转移到能够进行错误处理的位置。

3、异常分类:Java把程序运行时可能遇到的错误分为两类:

–非致命异常:通过某种修正后程序还能继续执行。 这类错误叫作异常。如:文件不存在、无效的数组 下标、空引用、网络断开、打印机脱机、磁盘满等。 Java中提供了一种独特的处理异常的机制,通过异 常来处理程序设计中出现的错误。

–致命异常:程序遇到了非常严重的不正常状态,不 能简单恢复执行,是致命性错误。如:内存耗尽、 系统内部错误等。这种错误程序本身无法解决。

4、Java中的异常类可分为两大类:

- Error类层次结构描述了Java 运行时系统的内部错误 和资源耗尽错误。应用程序不应该捕获这类异常,也 不会抛出这种异常 。

- Exception Exception类:重点掌握的异常类。Exception层次结 构又分解为两个分支:一个分支派生于 RuntimeException;另一个分支包含其他异常。

5、 编译器要求程序必须对这类异常进行处理 (checked),称为已检查异常。

6、声明抛出异常:如果一个方法可能会生成一些异 常,但是该方法并不确切知道如何对这些异常事 件进行处理,此时,这个方法就需声明抛出这些 异常。

 “一个方法不仅需要告诉编译器将要返回什么值 ,还要告诉编译器可能发生什么异常”。

7、声明抛出异常在方法声明中用throws子句中来指 明。例如: – public FileInputStream(String name ) throws FileNotFoundException

8、以下4种情况需要方法用throws子句声明抛出异常:

–方法调用了一个抛出已检查异常的方法。

–程序运行过程中可能会发生错误,并且利用throw语句 抛出一个已检查异常对象。

–程序出现错误。例如,a[-1] = 0;

–Java虚拟机和运行时库出现的内部异常。

9、一个方法必须声明该方法所有可能抛出的已检查异常,而 未检查异常要么不可控制(Error),要么应该避免发生 (RuntimeException)。如果方法没有声明所有可能发生 的已检查异常,编译器会给出一个错误消息。

10、当Java应用程序出现错误时,会根据错误类型产 生一个异常对象,这个对象包含了异常的类型和 错误出现时程序所处的状态信息。把异常对象递 交给Java编译器的过程称为抛出。

11、程序运行期间,异常发生时,Java运行系统从异常 生成的代码块开始,寻找相应的异常处理代码,并 将异常交给该方法处理,这一过程叫作捕获。

12、catch块是对异常对象进行处理的代码; ? 每个try代码块可以伴随一个或多个catch语句,用于处理 try代码块中所生成的各类异常事件; ? catch语句只需要一个形式参数指明它所能捕获的异常类 对象,这个异常类必须是Throwable的子类,运行时系统 通过参数值把被抛出的异常对象传递给catch块; ? catch块可以通过异常对象调用类Throwable所提供的方法。

日志:

全局日志记录(global logger) 

Logger.getGlobal().info("test"); 

Logger.getGlobal().serLevel(Level.OFF);

可以使用getLogger方法创建或获取记录器,未被任何变量引用的日志记录器可能会被垃圾回收,所以可以使用静态变量存储日志记录器的一个引用 

private static final Logger myLogger = Logger.getLogger("com.mycompany.myapp");

、实验内容和步骤

 

实验1:用命令行与IDE两种环境下编辑调试运行源程序ExceptionDemo1、ExceptionDemo2,结合程序运行结果理解程序,掌握未检查异常和已检查异常的区别。

//异常示例1
public class ExceptionDemo1 
    public static void main(String args[]) 
        int a = 0;
        System.out.println(5 / a);
    

//异常示例2
import java.io.*;

public class ExceptionDemo2 
    public static void main(String args[]) 
     
          FileInputStream fis=new FileInputStream("text.txt");//JVM自动生成异常对象
          int b;
          while((b=fis.read())!=-1)
          
              System.out.print(b);
          
          fis.close();
      

代码如下:1、

public class ExceptionDemo1 
    public static void main(String args[]) 
        int a = 0;
        if(a==0) 
            System.out.println("除数为零");
            
        else 
System.out.println(5 / a);
    

结果如下:

技术分享图片

2、

import java.io.*;
public class ExceptionDemo2 
    public static void main(String args[]) throws IOException 
    
         FileInputStream fis=new FileInputStream("text.txt");//JVM自动生成异常对象
         int b;
         while((b=fis.read())!=-1)
         
             System.out.print(b);
         
         fis.close();
     

或者是

import java.io.*;
public class ExceptionDemo2 
    public static void main(String args[])  
    
        try 
            File file=new File("text.txt");
         FileInputStream fis=new FileInputStream(file);//JVM自动生成异常对象
         BufferedReader in=new BufferedReader(new FileReader(file));
         String b;
         while((b=in.readLine())!=null)
         
             System.out.print(b);
         
         fis.close();
     
        catch (Exception e) 
            System.out.print("cuowu");
        
            
        

 

结果如下:1技术分享图片

2、技术分享图片

实验2 导入以下示例程序,测试程序并进行代码注释。

测试程序1:

  • 在elipse IDE中编辑、编译、调试运行教材281页7-1,结合程序运行结果理解程序;
  • 在程序中相关代码处添加新知识的注释;
  • 掌握Throwable类的堆栈跟踪方法;
package stackTrace;

import java.util.*;

/**
 * A program that displays a trace feature of a recursive method call.
 * @version 1.01 2004-05-10
 * @author Cay Horstmann
 */
public class StackTraceTest//方法调用过程

   /**
    * Computes the factorial of a number
    * @param n a non-negative integer
    * @return n! = 1 * 2 * . . . * n
    */
   public static int factorial(int n)
   
      System.out.println("factorial(" + n + "):");
      Throwable t = new Throwable();//调用类的getStackTrace方法
      StackTraceElement[] frames = t.getStackTrace();
      for (StackTraceElement f : frames)
         System.out.println(f);//打印出f
      int r;
      if (n <= 1) r = 1;
      else r = n * factorial(n - 1);
      System.out.println("return " + r);
      return r;//计算过程
   

   public static void main(String[] args)
   
      Scanner in = new Scanner(System.in);
      System.out.print("Enter n: ");
      int n = in.nextInt();
      factorial(n);//输出阶乘
   

结果如下:

技术分享图片

测试程序2:

l Java语言的异常处理积极处理方法和消极处理两种方式

l 下列两个简答程序范例给出了两种异常处理的代码格式。在elipse IDE中编辑、调试运行源程序ExceptionalTest.java,将程序中的text文件更换为身份证号.txt,要求将文件内容读入内容,并在控制台显示;

掌握两种异常处理技术的特点

//积极处理方式  

import java.io.*;

 

class ExceptionTest

    public static void main (string args[])

  

       try

           FileInputStream fis=new FileInputStream("text.txt");

      

       catchFileNotFoundExcption e)

           …… 

    ……

   

//消极处理方式

 

import java.io.*;

class ExceptionTest

    public static void main (string args[]) throws  FileNotFoundExcption

    

        FileInputStream fis=new FileInputStream("text.txt");

    

 代码如下:

//积极处理方式  
  
  import java.io.*;
  import java.io.BufferedReader;
  import java.io.FileReader;
  class ExceptionTest 
   public static void main (String args[])
 
    File fis=new File("身份证号.txt");
        try
 
            FileReader fr = new FileReader(fis);
            BufferedReader br = new BufferedReader(fr);
            //捕获并处理异常語句
            try 
                String s, s2 = new String();
                while ((s = br.readLine()) != null) 
                    s2 += s + "
 ";
                
                br.close();
                System.out.println(s2);
             catch (IOException e) 
                // TODO Auto-generated catch block
                e.printStackTrace();
            
         catch (FileNotFoundException e) 
            // TODO Auto-generated catch block
            e.printStackTrace();
        
 
   
package 异常;

//消极处理方式

import java.io.*;
class ExceptionTest 
    public static void main (String args[]) throws  IOException
       
        File fis=new File("身份证号.txt");
        FileReader fr = new FileReader(fis);
        BufferedReader br = new BufferedReader(fr);
        String s, s2 = new String();

            while ((s = br.readLine()) != null) 
                s2 += s + "
 ";
            
            br.close();
            System.out.println(s2);
       

 

结果:

技术分享图片

实验3: 编程练习

练习1

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

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

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

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

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

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

l 在以上程序适当位置加入异常捕获代码。

package ID;
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;

@SuppressWarnings("unused")
public class Main
    private static ArrayList<Person> Personlist;
    @SuppressWarnings("resource")
    public static void main(String[] args) 
        Personlist = new ArrayList<>();
        Scanner scanner = new Scanner(System.in);
        File file = new File("E:\\新建文件夹\\身份证号.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 Person = new Person();
                Person.setname(name);
                Person.setID(ID);
                Person.setsex(sex);
                int a = Integer.parseInt(age);
                Person.setage(a);
                Person.setbirthplace(place);
                Personlist.add(Person);

            
         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:查询人员中是否有你的同乡");
           
            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 < Personlist.size(); i++)
                
                    int p=Personlist.get(i).getage()-yourage;
                    if(p<0) p=-p;
                    if(p==d_value) System.out.println(Personlist.get(i));
                   */
                break;
            case 5:
           isTrue = false;
           System.out.println("退出程序!");
                break;
            default:
                System.out.println("输入有误");            
        
    
    public static int agenear(int age) 
     
       int j=0,min=53,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 ID;
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+"	"+sex+"	"+age+"	"+ID+"	"+birthplace+"
";


结果如下:

技术分享图片

技术分享图片

练习2

l 编写一个计算器类,可以完成加、减、乘、除的操作;

利用计算机类,设计一个小学生100以内数的四则运算练习程序,由计算机随机产生10道加减乘除练习题,学生输入答案,由程序检查答案是否正确,每道题正确计10分,错误不计分,10道题测试结束后给出测试总分;

将程序中测试练习题及学生答题结果输出到文件,文件名为test.txt

l 在以上程序适当位置加入异常捕获代码。

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;


public class Demo 
    public static void main(String[] args) 

        Scanner in = new Scanner(System.in);
        jf counter=new jf();
        PrintWriter out = null;
        try 
            out = new PrintWriter("text.txt");
         catch (FileNotFoundException e) 
            // TODO Auto-generated catch block
            e.printStackTrace();
        
        int sum = 0;

        
        
        for (int i = 1; i <=10; i++) 
            int a = (int) Math.round(Math.random() * 100);
            int b = (int) Math.round(Math.random() * 100);
            int m= (int) Math.round(Math.random() * 3);

            
           switch(m)
           
           case 0:
               System.out.println(i+": "+a+"/"+b+"=");
               
               while(b==0)  b = (int) Math.round(Math.random() * 100); 
               
            int c0 = in.nextInt();
            out.println(a+"/"+b+"="+c0);
            if (c0 == jf.division(a, b)) 
                sum += 10;
                System.out.println("恭喜答案正确");
            
            else 
                System.out.println("抱歉,答案错误");
            
            
            break;
            
           case 1:
               System.out.println(i+": "+a+"*"+b+"=");
               int c = in.nextInt();
               out.println(a+"*"+b+"="+c);
               if (c == counter.multiplication(a, b)) 
                   sum += 10;
                   System.out.println("恭喜答案正确");
               
               else 
                   System.out.println("抱歉,答案错误");
               
               break;
           case 2:
               System.out.println(i+": "+a+"+"+b+"=");
               int c1 = in.nextInt();
               out.println(a+"+"+b+"="+c1);
               if (c1 == counter.add(a, b)) 
                   sum += 10;
                   System.out.println("恭喜答案正确");
               
               else 
                   System.out.println("抱歉,答案错误");
               
               
               break ;
           case 3:
               System.out.println(i+": "+a+"-"+b+"=");
               int c2 = in.nextInt();
               out.println(a+"-"+b+"="+c2);
               if (c2 == counter.reduce(a, b)) 
                   sum += 10;
                   System.out.println("恭喜答案正确");
               
               else 
                   System.out.println("抱歉,答案错误");
               
               break ;

                
    
          
        System.out.println("成绩"+sum);
        out.println("成绩:"+sum);
         out.close();

         
    
    
public class Counter 
   private int a;
   private int b;
    public int  add(int a,int b)
    
        return a+b;
    
    public int   reduce(int a,int b)
    
        return a-b;
    
    public int   multiplication(int a,int b)
    
        return a*b;
    
    public int   division(int a,int b)
    
        if(b!=0)
        return a/b;
        else return 0;
    

    


 

结果如下:

技术分享图片

技术分享图片

 实验4:断言、日志、程序调试技巧验证实验。

实验程序1

/断言程序示例
public class AssertDemo 
    public static void main(String[] args)         
        test1(-5);
        test2(-3);
    
    
    private static void test1(int a)
        assert a > 0;
        System.out.println(a);
    
    private static void test2(int a)
       assert a > 0 : "something goes wrong here, a cannot be less than 0";
        System.out.println(a);
    

 结果是:

技术分享图片

如果注释语句test1(-5);后重新运行程序,结果是:

技术分享图片

实验程序2:

l  用JDK命令调试运行教材298页-300页程序7-2,结合程序运行结果理解程序;

l  并掌握Java日志系统的用途及用法。

package logging;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.logging.*;
import javax.swing.*;

/**
 * A modification of the image viewer program that logs various events.
 * @version 1.03 2015-08-20
 * @author Cay Horstmann
 */
public class LoggingImageViewer

   public static void main(String[] args)
   
      if (System.getProperty("java.util.logging.config.class") == null
            && System.getProperty("java.util.logging.config.file") == null)
      
         try
         
            Logger.getLogger("com.horstmann.corejava").setLevel(Level.ALL);
            final int LOG_ROTATION_COUNT = 10;
            Handler handler = new FileHandler("%h/LoggingImageViewer.log", 0, LOG_ROTATION_COUNT);
            Logger.getLogger("com.horstmann.corejava").addHandler(handler);
         
         catch (IOException e)
         
            Logger.getLogger("com.horstmann.corejava").log(Level.SEVERE,
                  "Can‘t create log file handler", e);
         
      

      EventQueue.invokeLater(() ->
            
               Handler windowHandler = new WindowHandler();
               windowHandler.setLevel(Level.ALL);
               Logger.getLogger("com.horstmann.corejava").addHandler(windowHandler);

               JFrame frame = new ImageViewerFrame();
               frame.setTitle("LoggingImageViewer");
               frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

               Logger.getLogger("com.horstmann.corejava").fine("Showing frame");
               frame.setVisible(true);
            );
   


/**
 * The frame that shows the image.
 */
class ImageViewerFrame extends JFrame

   private static final int DEFAULT_WIDTH = 300;
   private static final int DEFAULT_HEIGHT = 400;   

   private JLabel label;
   private static Logger logger = Logger.getLogger("com.horstmann.corejava");

   public ImageViewerFrame()
   
      logger.entering("ImageViewerFrame", "<init>");      
      setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

      // set up menu bar
      JMenuBar menuBar = new JMenuBar();
      setJMenuBar(menuBar);

      JMenu menu = new JMenu("File");
      menuBar.add(menu);

      JMenuItem openItem = new JMenuItem("Open");
      menu.add(openItem);
      openItem.addActionListener(new FileOpenListener());

      JMenuItem exitItem = new JMenuItem("Exit");
      menu.add(exitItem);
      exitItem.addActionListener(new ActionListener()
         
            public void actionPerformed(ActionEvent event)
            
               logger.fine("Exiting.");
               System.exit(0);
            
         );

      // use a label to display the images
      label = new JLabel();
      add(label);
      logger.exiting("ImageViewerFrame", "<init>");
   

   private class FileOpenListener implements ActionListener
   
      public void actionPerformed(ActionEvent event)
      
         logger.entering("ImageViewerFrame.FileOpenListener", "actionPerformed", event);

         // set up file chooser
         JFileChooser chooser = new JFileChooser();
         chooser.setCurrentDirectory(new File("."));

         // accept all files ending with .gif
         chooser.setFileFilter(new javax.swing.filechooser.FileFilter()
            
               public boolean accept(File f)
               
                  return f.getName().toLowerCase().endsWith(".gif") || f.isDirectory();
               

               public String getDescription()
               
                  return "GIF Images";
               
            );

         // show file chooser dialog
         int r = chooser.showOpenDialog(ImageViewerFrame.this);

         // if image file accepted, set it as icon of the label
         if (r == JFileChooser.APPROVE_OPTION)
         
            String name = chooser.getSelectedFile().getPath();
            logger.log(Level.FINE, "Reading file 0", name);
            label.setIcon(new ImageIcon(name));
         
         else logger.fine("File open dialog canceled.");
         logger.exiting("ImageViewerFrame.FileOpenListener", "actionPerformed");
      
   


/**
 * A handler for displaying log records in a window.
 */
class WindowHandler extends StreamHandler

   private JFrame frame;

   public WindowHandler()
   
      frame = new JFrame();
      final JTextArea output = new JTextArea();
      output.setEditable(false);
      frame.setSize(200, 200);
      frame.add(new JScrollPane(output));
      frame.setFocusableWindowState(false);
      frame.setVisible(true);
      setOutputStream(new OutputStream()
         
            public void write(int b)
            
             // not called

            public void write(byte[] b, int off, int len)
            
               output.append(new String(b, off, len));
            
         );
   

   public void publish(LogRecord record)
   
      if (!frame.isVisible()) return;
      super.publish(record);
      flush();
   

实验程序3:

l  用JDK命令调试运行教材298页-300页程序7-2,结合程序运行结果理解程序;

l  按课件66-77内容练习并掌握Elipse的常用调试技术。

技术分享图片

三:总结

本章我学习了有关于java异常处理技术,了解了断言的用法和日志的用途;在老师和学长得讲解下,我基本掌握了一些关于java异常处理技术得基础应用,了解到异常和捕获的用法,在适当的位置添加try……catch语句,实验方面,在上周实验的基础上添加、深化,得到新的表现形式,总得来说,还是有很大的收获,就是在一些语句上面还是,模棱两可,不是特别透彻。


201771010135杨蓉庆《面对对象程序设计(java)》第九周学习总结(代码片段)

第7章异常、日志、断言和调试1、实验目的与要求(1)掌握java异常处理技术;(2)了解断言的用法;(3)了解日志的用途;(4)掌握程序基础调试技巧;一、理论知识1、异常:在程序的执行过程中所发生的异常事件,它中断指令的正常执... 查看详情

201771010135杨蓉庆《面对对象程序设计(java)》第七周学习总结(代码片段)

学习目标1、深入理解OO程序设计的特征:继承、多态;2、熟练掌握Java语言中基于类、继承技术构造程序的语法知识;3、利用继承定义类设计程序,能够设计开发含有1个主类、2个以上用户自定义类的应用程序。1、实验目的与要... 查看详情

201771010135杨蓉庆《面对对象程序设计(java)》第十八周学习总结(代码片段)

1、实验目的与要求(1) 综合掌握java基本程序结构; (2) 综合掌握java面向对象程序设计特点;(3) 综合掌握java GUI 程序设计结构;(4) 综合掌握java多线程编程模型;(5) 综合编程练习。 2、实验内容和... 查看详情

杨蓉庆201771010135《面向对象程序设计(java)》第一周学习总结

第一部分:课程准备部分填写课程学习平台注册账号,平台名称注册账号博客园:www.cnblogs.com艾特大家程序设计评测:https://pintia.cn/艾特你代码托管平台:https://github.com/艾特你@m中国大学MOOC: https://www.icourse163.org/1063769427以下... 查看详情

201771010135杨蓉庆《面向对象程序设计(java)》第四周学习总结(代码片段)

学习目标1、掌握类与对象的基础概念,理解类与对象的关系;2、掌握对象与对象变量的关系;3、掌握预定义类的基本使用方法,熟悉Math类、String类、math类、Scanner类、LocalDate类的常用API;4、掌握用户自定义类的语法规则,包... 查看详情

201771010135杨蓉庆《面向对象程序设计(java)》第二周学习总结

第一部分:理论知识学习部分3.1标识符:由字母、下划线、美元符号和数字组成,且第一个符号不能为数字,可用作:类名、变量名、方法名、数组名、文件名等。有Hello、$1234、程序名、www_123等。(2)关键词:是java语言中已经被... 查看详情

201771010135杨蓉庆《面对对象程序设计(java)》第十六周学习总结(代码片段)

1、实验目的与要求(1) 掌握线程概念;(2) 掌握线程创建的两种技术;(3) 理解和掌握线程的优先级属性及调度方法;(4) 掌握线程同步的概念及实现技术;一、理论知识?线程的概念 (1)多线程是进程执行过程中... 查看详情

201771010135杨蓉庆《面向对象程序设计(java)》第六周学习总结(代码片段)

实验六 继承定义与使用1、实验目的与要求(1) 理解继承的定义;(2) 掌握子类的定义要求(3) 掌握多态性的概念及用法;(4) 掌握抽象类的定义及用途;(5) 掌握类中4个成员访问权限修饰符的用途;(6) 掌握... 查看详情

201771010142张燕/杨蓉庆实验十三图形界面事件处理技术(代码片段)

实验十三  图形界面事件处理技术实验时间 2018-11-22 基础知识: ?事件源(eventsource):能够产生事件的对象都可以成为事件源,如文本框、按钮等。一个事件源是一个能够注册监听器并向监听器发送事件对象... 查看详情

java面对对象程序设计——面对对象(代码片段)

重点掌握1.类是对一类事物描述,是抽象的、概念上的定义;对象是实际存在的该类事物的每个个体,因而也称为实例2.创建对象的格式是:类名对象名称=new类名();3.方法的声明格式是:[<修饰符>]<返... 查看详情

面对对象的程序设计

面向对象(Object-Oriented,OO)的语言有一个标志,那就是它们都有类的概念,而通过类可以创建任意多个具有相同属性和方法的对象。 一、理解对象:        第一种:基于Object对象1varperson=newObject(... 查看详情

day-8:面对对象编程

  面对过程的程序设计方法意在将函数分成子函数,再依次调用这些函数来解决问题。  而面对对象的程序设计方法,来源于自然界,类是实例的抽象,实例是类的具体。自定义出来的对象是类,而所有的数据都可以看成是... 查看详情

java面对对象程序设计——面对对象(代码片段)

重点掌握1.类是对一类事物描述,是抽象的、概念上的定义;对象是实际存在的该类事物的每个个体,因而也称为实例2.创建对象的格式是:类名对象名称=new类名();3.方法的声明格式是:[<修饰符>]<返... 查看详情

设计模式面对面之订阅模式

订阅模式订阅模式主要涉及到三种对象:订阅对象,发布对象,分发对象。案例没对这三种对象做抽像,大家可以根据应用场景去扩展。类图:常用的实现方式:订阅对象//订阅对象publicclassSubscribe{publicstringName;publicSubscribe(stringn... 查看详情

java面对对象入门-程序块初始化

Java实例初始化程序是在执行构造函数代码之前执行的代码块。每当我们创建一个新对象时,这些初始化程序就会运行。1.实例初始化语法用花括号创建实例初始化程序块。对象初始化语句写在括号内。publicclassDemoClass{//Thisisinitial... 查看详情

《设计模式之禅》笔记整理--面对对象设计六大原则

第一章、面对对象设计六大原则:(1)、单一职责原则:应该有且只有一个原因引起类的变更。为什么要用单一职责原则:(1)、类的复杂性降低,实现什么职责都有清晰明确的定义。           (2)、可读性提高,... 查看详情

java——面对对象

软件出现的目的:*用计算机的语言来描述世界*用计算机解决现实世界的问题面向对象的思想描述面向对象的世界面向对象设计和开发程序的好处:*交流更加流畅*提高设计和开发效率构造方法:构造方法是用来描述对象创建的... 查看详情

面对对象编程

面向对象编程标签(空格分隔):python基础---面向过程:根据业务逻辑从上到下写代码面向对象:将数据与函数绑定到一起,进行封装,这样能够更快速的开发程序,减少了重复代码的重写过程面向对象(object-oriented;简称:OO)至今... 查看详情