20165318 2017-2018-2 "Java program design" Fifth Week study summary study summary
- When using idea, because the code I wrote earlier is using GBK encoding, when you open it, the code will be garbled in Chinese because it defaults to UTF-8.
Summary of the contents of the textbook: The seventh chapter inner class and Exception class
First, inner class and anonymous class
-
Inner class:
1. Inner classes can use member variables and methods of an outer-nested class.
2. Class variables and class methods cannot be declared in the class body.
3. The inner class is used only for outer-nested classes.
4. Class declarations can use the static modifier, and the static inner class cannot manipulate instance member variables in an outer-nested class.
Note : A non-intrinsic class cannot be a static class.
-
Anonymous class:
1. The anonymous class must be an internal class.
2. You can use member variables and methods of an outer-nested class.
3. Class variables and class methods cannot be declared.
4. The construction method of the parent class must be used.
5. You can use the method of the parent class directly or override it.
Create an object with a subclass of bank (anonymous class)
new Bank () {// Use the super class constructor Anonymous class body}; // ** Note: add semicolon at the end **
Anonymous classes related to interfaces
new Computable () {the class body of the anonymous class that implements the interface // ** The class body must override all methods in the interface **};
Second, the Exception class
-
-
An exception is a number of errors that may occur while the program is running.
-
-
public static int parseInt(String s)You can convert a string in the format "number" to int type data.
-
-
try - catchStatement
`try { 包含可能发生异常的语句 } catch(ExceptionSubClass1 a) { ... } catch(ExceptionSubClass1 b) { ... }
-
-
Once atrypartial exception is thrown, or a method that may throw an exception object is called, and the method throws an exception object, thetrypart will immediately end execution and turn to the correspondingcatchsection. The program can place the exception after the processing in thecatchsection.
-
-
try - catchStatements can be madecatchup of several components, respectively, to handle the corresponding exception that occurs.
-
-
Thecatchexception class in each parameter is aExceptionsubclass that represents a subset oftrypossible exceptions that cannot have a parent-child relationship, otherwise a parent-class argument is reservedcatch.
-
-
To customize the exception class:
1. Define your own class of exceptions (Exceptionsubclass);
2. In the method of possible exception, declare a number of exceptions to be produced using the throws keyword, and in the method body specifically give the operation to produce an exception, throw the exception object with throw .
3. Use block statements in your program totry-catchinvoke methods that may have an exception.
-
- finallyStatement
- If a statement is executed in thetry-catchstatementreturn, thefinallychild statement is still executed.
- try-catchExecutes the program exit code, executes the statement, andSystem.exit(0)does not execute thefinallychild statement.
Third, assert
-
When the program is formally running, the assertion statement is automatically closed and the file name can be usedjava -ea;
-
During the debugging phase of the program, the assertion statement plays a normal role;
- Two forms of assertion statements
- assert bolleanException: WhenbolleanExceptionThe value is true, the program resumes execution from the assertion statement, and when the value is False, the program stops execution from the assertion statement.
- assert blooeanException:messageException: WhenbolleanExceptionThe value is true, the program resumes execution from the assertion statement, and when the value is False, the program stops execution from the assertion statement and outputsmessageExceptionthe value of the expression.
Tenth. Input, output stream
-
The input stream points to the source called it, and the program reads the data from the source through the input stream
-
The direction of the output stream is called its destination, and the program transmits the data to the destination via the output stream
First, theFile class
-
The object of the file class is primarily used to obtain information about the files themselves, and does not involve read and write operations on the files.
-
Construction Method:
File(String filename)
File(String directoryPath,String filename)
File(File dir,String filename)
-
File properties: Some of the methods of the file class are often used to obtain some information about the files themselves.
-
Create directory: TheFile object calls the methodpublic boolean mkdir()to create a directory that successfully returns TRUE, otherwise (the directory already exists) returns false.
-
Run the executable file:Runtime ec = Runtime.getRuntime();.
-
Use the input stream step:
1. Set the source of the input stream;
2. Create an input stream that points to the source;
3. Let the input stream read the data from the source;
4. Close the input stream;
Note: The program musttry-catchcreate an input stream in the block section of the statement andtrycatchdetect and handle the exception in the Block section.
-
The output stream step is similar to the input stream
-
READ: Input stream readint readmethod, output stream readvoid writemethod.
-
Close stream: Input streamin.close();, output streamvoid.close();.
-
Buffered streams
1.BufferedReaderBufferedWriterobjects created with classes are called buffered inputs, output streams, which enhance the ability to read and write files.
2.BufferedReaderstreams andBufferedWriterstreams, both the source and destination must be character input stream and output stream.
3. Construction methodBufferedReader(Reader in);``BufferedWriter(Writer out);.
-
Random stream
1. TheRandomAccessFilestream created by the class is called a random stream, which can read the file's data from the stream, or it can write data to a file through this stream.
2.RandomAccessFilewhen the stream points to a file, the file is not refreshed.
-
Array flow
-
Data flow
-
Object Flow
-
Using the scanner class and regular expressions to parse a file is characterized by a time exchange for space, which is relatively slow to parse, but saves memory
Problems and solving process in textbooks
-
Question 1: Why anonymous classes are used:
-
WORKAROUND: If the parameter type of a method is Class A, the user wants to pass a subclass object to the method, but the system does not provide a subclass that meets the requirements, then the user can consider using an anonymous class when writing code.
Problems and solutions during the debugging of code
-
Issue 1: Run the example7_2 times wrong
-
Resolution process: After the inspection, it is found that using the output method, the habitual use of theSystem.out.printlncorrect code isSystem.out.printf.
-
Issue 2: The following issues occur when you run example7_7
-
Resolution: After careful examination of the code, it was found that a "}" was written at the time of encoding, causing the main class to not be recognized, using the code formatting in idea and no errors were found.
-
Question 3: Write your own After class exercise fourth, using assertions, should be when the input number is greater than 100 or less than 0 o'clock, terminate execution, but the actual operation has the following error:
- WORKAROUND: When you re-learn the assertion-related knowledge point, it is found that execution is stopped when the assertion evaluates to True when the program continues execution, false. And I read the first time did not remember clearly, just to judge the conditions to write the reverse, resulting in the program can not run, after correcting, it can be normal operation. Results such as:
-
Issue 4: When debugging example10_2, there is no output after running.
-
Solve the process: reading the relevant knowledge, found that I enter the stream open directory without the.javatype of file, in this directory to add the.javatype of file, it can run normally.
-
Issue 5: When debugging Example10_4, run the following error:
-
Resolution: The problem is that the path to the open file is incorrect, andExample10_4.javasrc/Example10_4.javait will work as expected.
-
Issue 6: When running EXAMPLE10_17, the dialog does not work, the actual output of the dialog box and the book has a gap
- Resolution process: You need to click "Open" in the "File" drop-down menu in the upper left corner to get the effect on the book. At the same time I found that if the "italic _gb2312" changed to "UTF-8" will not affect the operation of the window.
Code Cloud Link
Https://gitee.com/besti-is-java-2018/20165318_sun_xiaoxuan
Code
20165318 2017-2018-2 "Java Programming" Fifth Week study summary