Android Learning Notes (iv) Java Fundamentals Review

Source: Internet
Author: User

First, the interface

1) The method defined in the interface is public and defaults to public, not default.

2) The implementation of the interface (implements) is a special inheritance, similar to the parent subclass of the relationship, can be transformed upward (very important).

3) A class can implement multiple interfaces, and an interface can inherit multiple interfaces.

Second, static variables/methods

1) If a method (or variable) is declared static, you can call it directly using the class name. Method name (or variable name).

2) Static methods can only use static variables.

Third, static Factory mode

Core idea: Encapsulate the code of the generated object (the method that calls the constructor using new) in the factory class, reducing the code repetition rate.

For example: The Hpprinter class and the Canonprinter class inherit from the printer class, both of which contain the print () method, but the implementation method is different. If you want to implement the print () method separately now, build the object separately, then call the method, and repeat this step if you add a different type of printer. The static factory pattern is to use a method (written to a printerfactory Class), pass in a parameter of type printer, and then execute the print () method. Use the upward transformation, as long as the subclass of printer is applicable.

Four, the package

Compile the Java file in the command-line environment, if the package name is declared, the command needs to be modified to:

Javac-d. Test.java ("." Refers to the new generated folder in the current directory, "-D" refers to directory)

And you need to add the package name when executing, such as

Java pack. Test

Five, the Authority

Public > Protected > Default > Private

1) public can be accessed outside the package, and default can only be accessed within the package.

2) Private cannot be accessed outside the class.

3) protected has the permissions that default has (but cannot be used to decorate classes) and can inherit across packages. subclasses and parent classes are not in a package, but subclasses can use member variables and methods that are decorated as protected in the parent class.

4) The public class can have at most one file and must have the same name as the Java file. There is no public class without this requirement.

5) If you inherit under insufficient permissions, the inheritance itself will not be error (compilation will not error), but the member variables and methods do not allow access, error when accessing.

Vi. anomalies

1) Throw: Throw exception

2) throws: It is possible to declare an exception, and once declared it does not need to be processed within the function, but can be handled by using Try...catch when calling the function.

Vii. internal classes and anonymous inner classes

1) Set B is the inner class of a, then the object of B is required before the object of a: a.b B = new A (). New B ();

2) Inner classes can use member variables for external classes, and each inner class has an external class corresponding to it.

3) anonymous inner class:

B.fun (new  A () {   publicvoid  dosomething () {   System.out.println ( "Anonymous internal class");      // A is an interface, where an object that implements the interface is generated directly. })
Eight, I/O

Note: The input and output are relative to the program

1) Byte stream

FileInputStream inherits from InputStream (abstract); FileOutputStream inherits from OutputStream (abstract).

Inputstream:int Read (byte[] b, int off, int len)

Outputstream:void write (byte[] b, int off, int len)

NULL ; Try {  new FileInputStream ("" e:/src/from.txt);   byte New byte [+];  Fis.read (buffer,0, buffer.length);      }
    • declares an input stream reference.
    • Generates an object that represents the input stream.
    • Generates an array of byte classes.
    • Invokes the Read method of the input stream object to read the data.

Note: Buffer obtains ASCII values, to print out characters, you need to

New= S.trim (); // calling the trim () method of a String object will remove the first and null characters of the string
2) How to read and write large files
 while (true) {  int temp = fis.read (buffer,0, buffer.length);   if (temp =-1) The {// return value indicates the number of reads, and 1 indicates the end of the read;      Break ;    } // each call to read () (or a similar readline ()) reading pointer moves    backwards }

Note: The FIS and Fos are closed in the Finally, and this statement is to be wrapped in the try...catch.

3) Character stream

FileReader inherits from reader (abstract), and FileWriter inherits from writer (abstract).

Unlike a byte stream, buffer is an array of type char.

4) node flow and processing flow

bufferedreader--character input processing stream

The initialization of the BufferedReader needs to be filereader (or other reader) as a parameter, which means that new functionality can be added on the basis of a normal node stream. This is called "Decorator mode".

Android Learning Notes (iv) Java Fundamentals Review

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.