20165223 Java programming Fifth week of study summary

Source: Internet
Author: User

Summary of the contents of textbook Learning seventh chapter
    • Inner class
    • Anonymous class
    • Exception class
    • Assertion
Tenth Chapter Essentials
    • File class
    • File byte/character input, output stream
    • Buffered streams
    • Random stream
    • Array flow
    • Data flow
    • Object Flow
    • Serialization and object cloning
    • Parsing files using scanner
    • File lock
Problems in teaching materials learning and the solution process I. Internal class
    • Java key members : member variables, methods, inner classes

    • Inner class : Defines another class in one class, the defined class is called an inner class

    • outer-Nested class: An outer class that contains the inner class of the class called Inner classes

    • important relationships between the inner class and the outer-nested class :
      • The member variables of the outer-nested class are still valid in the inner class, and methods within the inner class can also call methods in the outer-nested class
      • The class body in an inner class cannot declare class variables and class methods. The class body of an outer-nested class can declare an object with an inner class
      • The inner class is used only for its outer-nested classes
    • The inner class corresponds to the bytecode file name format :外嵌类名$内部类名

    • Inner classes can be decorated as static inner classes, but non-intrinsic classes cannot be static classes

Second, anonymous class
    • Anonymous classes related to subclasses
      • Anonymous classes: Subclasses remove the class body after class declaration
      • Form:

        new Bank() {匿名类的类体};
      • Characteristics:
        • Anonymous classes are subcategories
        • Anonymous classes can also inherit methods from the parent class or override the parent class's methods
        • An anonymous class must be an inner class that creates an object directly in a class with an anonymous class
        • Anonymous classes can access member variables and methods in outer-nested classes, and cannot declare static member variables and static methods in the class body of an anonymous class
        • When you create an object with an anonymous class, you use the constructor of the parent class directly
    • Anonymous classes related to interfaces
      • Anonymous class: Class that implements the interface removes the class declaration after the class body
      • Form:

        new Computable() {实现接口匿名类的类体};
Third, the Exception class
    • Exceptions : Errors that may occur during a program's operation, exception classes used as exception handling, and program opportunities to handle errors

    • Common exception Classes :
      • throwthrowing Exception An instance of a subclass with a keyword indicates an exception occurred
      • Method parseInt() uses the thrown object when executing an throw NumberFormatException exception
      • The stream object calls read the method to throw an IOException exception object when reading a file that does not exist
      • ......
    • Try-catch statements
      • Format:

        try {包含可能发生异常的语句}catch(ExceptionSubClass1 e) {...}catch(ExceptionSubClass2 e) {...}
      • Java uses try-catch to handle exceptions

    • Custom exception Classes
      • Differences throw and throws keywords, reference blog: The difference between keyword throw and throws in Java
        • throw: The function is to throw an exception, typically inside a block of code
        • throws: is a method may throw an exception declaration (used when declaring a method, indicating that the method might throw an exception)
Iv. assertions
    • Syntax format for assertion statements

      assert booleanExpression;assert booleanExpression:messageException;
    • Enable and close assertion statements

      java -ea mainClass
V. File class
  • Properties of the file

    File Class function
    Public String GetName () Get the name of the file
    public boolean canRead () Determine if the file is readable
    public boolean canWrite () Determine if a file can be written
    public Boolean exists () Determine if a file exists
    public long Length () Get the length of a file
    Public String GetAbsolutePath () Gets the absolute path of the file
    Public String getParent () Gets the parent directory of the file
    public boolean isfile () Determine if the file is an ordinary file
    public boolean isdirectory () Determine if a file is a directory
    public boolean Ishidden () Determine if a file is a hidden file
    Public long LastModified () Gets the time the file was last modified
  • To Create a directory :public boolean mkdir()

  • to list files in a directory :

    Calling Methods function
    Public string[] List () Return all files in a directory as a string
    Public file[] Listfiles () Returns all files in a directory as a file object
    Public string[] List (filenamefilter filter) Returns all files that are contained in a directory by a specified type in a string
    Public file[] Listfiles (filefilter filter) Returns all files containing the specified type in the directory as a file object
  • The file was created in the delete
    • To create a file object with the files class:File file =new file("C://myletter","letter.txt")
    • To delete a file:file.delete();
  • Run the executable file
    • Use java.lang the declaration of Runtime类 an object in a package:Runtime ec;
    • Use the GetRuntime () static method of the class to create this object:ec = Runtime.getRuntime;
Vi. input/output stream
  • use the input stream step :
    • Set the source of the input stream
    • To create an input stream to a source
    • Let the input stream read the data in the stream
    • Close the input stream
  • To use the output stream step :
    • Give the destination of the output stream
    • Create an output stream to a destination
    • Let the output stream write data to the destination
    • Turn off the output stream
  • file byte input stream :
    • Construction method: FileInputStream(String name); ,FileInputStream(File file);
    • To read bytes using the input stream:read
    • To close a stream:close()
  • file byte output stream :
    • Construction Method: FileInputStream (String name); , fileinputstream (file file);
    • The
    • reads bytes using the input stream:
    write Method
    void write (int n) output stream call this method to write data to the destination
    void Write (byte b[]) output stream call this method to write to the destination a byte array
    void Write (byte b[],int off,int len) In a given byte array starting at offset off, take Len bytes to write to destination
    void Close () close output stream
    • Close stream: close ()
  • file character input stream, file character input stream :
    • Construction Method:

      FileReader(String filename): FileReader(File filename);FileWrite(String filename):FileWrite(File filename);FileWrite(String filename,boolean append);FileWrite(File filename,boolean append);
Vii. Other flows
    • Random Flow:RandomAccessFile
    • byte array stream: ByteArrayInputStream ,ByteArrayoutputStream
    • Character array flow: CharArrayReader ,CharArrayWrite
    • Data flow: DataInputStream ,DataoutputStream
    • Object flow: ObjectInputStream ,ObjectoutputStream
Problems in code debugging and the resolution process

After installing idea last week, I used idea to write code, and I had to say that the completion function of idea has made it easy to use, with a key run to run automatically, and debug debugging is clearer and easier. The seventh chapter of the code does not appear any problem, the understanding is not very complex, but the tenth chapter code is mostly in the gun, there are a lot of problems, the following a little bit to solve.

    • Example10_1.java

A problem occurred running:

Problem found:

Solve the problem:

    • Example10_2.java

A problem occurred running:

Solve the problem:

(method i) Copy the Java file to the previous level folder to read out

(Method Two) the "." In the source code Change to "src/."

    • Example10_3.java

Problem found:

Solve the problem:

    • Summary : Idea in the path is very important!! Do not set up may cause many errors, such as not reading the file and other issues
Code Hosting

See blog: https://gitee.com/BESTI-IS-JAVA-2018/20165223cn.git

Learning progress Bar
lines of code (new/cumulative) Blog Volume (Add/accumulate) Learning Time (new/cumulative) Important Growth
Goal 3000 rows 30 Articles 400 hours
Week Five 1324/200 3/11 20/20 Gradually deepen
Resources
    1. Instructional Videos
    2. Debugging Java programs with JDB

20165223 Java programming Fifth week of study summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.