The second day of internship java advanced

Source: Internet
Author: User

Polymorphism

Static code block

When a class is loaded, static code blocks take precedence over static methods and are executed once. Static code blocks are often used for initialization.

Sequence of static code blocks

Example

Class Person extends Object {static String name; int num; Person () {name = "yang"; num = 22; System. out. println ("generate Person class");} static {System. out. println ("the static code block of the Person class starts execution") ;}} public class Java {public static void main (String args []) {Person a = new Person (); System. out. println ("generate Java class");} static {System. out. println ("Java class static code block starts execution ");}}

Use of abstract methods

Abstract classes can contain general and static attributes.

It can also contain abstract methods and general methods.

abstract class Person{String name;int age;abstract String briefInfo();public String greeting(){return "How are you?";}}class Student extends Person{Student(){name="Tom";age=20;}public String briefInfo(){return "My name is "+name+" and I am "+age+" years old";}}class Worker extends Person{Worker(){name="Bob";age=30;}public String briefInfo(){return name+"is my name "+ "and I am "+age+" years old";}}public class Java{public static void main(String args[]){Student aa=new Student();Worker bb=new Worker();System.out.println(aa.briefInfo());System.out.println(bb.briefInfo());System.out.println(aa.greeting());}}

For interfaces,

Only contains abstract methods and static data members. The declared static data members must first assign initial values, rather than static method instantiation.

A class can inherit only one parent class. When you need to inherit more, you need to use interfaces to inherit multiple interfaces.

interface Name{String name="Tom";public abstract String showName();}interface Age{    int age=20;    public abstract int showAge();}class Person implements Name,Age{public String showName(){return name;}public int showAge(){return age;}}public class Java{public static void main(String args[]){Person tom=new Person();System.out.println("I am "+tom.showName()+" and I am "+tom.showAge()+" years old!");}}

// Interface inheritance interface A {final int a = 10; public abstract int sayA ();} interface B {final int B = 20; public abstract int sayB ();} interface C extends B, A {final int c = 50; public abstract int sayC ();} class People implements A, B, C {public int sayA () {return ;} public int sayB () {return B;} public int sayC () {return c ;}} public class Java {public static void main (String args []) {People attributes = new People (); System. out. println (bytes. sayA () + "" + outputs. sayB () + "" + outputs. sayC () + "");}}

On-line transformation does not require forced type conversion

. Objects must be forwarded.

 

Example

Class Father {private String surname; public String showSurname () {return surname;} public String greet () {return "Hello I am father ";}} class Son extends Father {public String greet () {return "Hello I am son" ;}} public class Java {public static void main (String args []) {Father father = new Son (); // The parent class object is generated by the subclass object. This is only usable by father, child class son inherits the attributes and methods that are not rewritten and reviewed in father. Son son = (Son) father; System. out. println (father. greet (); System. out. println (son. greet ());}}

Instanceof () can be used to determine whether a class implements an interface or whether an instance object belongs to a class.

The objects created by the subclass are all instances of the parent class, but the parent class says that the created object is not an instance of the subclass. We can see the instance example.

Interface to define a unified standard.

Chapter 7 Exception Handling

Interface to define a unified standard.

Chapter 7 Exception Handling

Two ways to throw an exception:

1. The program throws an exception and throws an instance exception.

2. Specify a method to throw an exception

// Specify a method to throw an exception class. Sometimes the items of the exception class provided by java cannot meet the requirements, so you need to define the exception class yourself. Class DefaultException extends Exception // inherits the Exception class {DefaultException (String msg) {super (msg ); // call the constructor of the parent class} public class Java {public static void main (String args []) {try {throw new DefaultException ("custom exception class "); // throw a custom Exception class} catch (Exception e) {System. out. println (e. toString ();} finally {System. out. println ("program running ended ");}}}

Understanding of the two keywords throw and throws.

The keyword throw is required when someone throws an exception in the program.

If the method throws an exception, the try-catch-finally exception can be handled.

Written in the program code that calls this method.

Chapter 8 package and Access Permissions

For example, package and import a program.

The main advantage of packaging is to avoid errors caused by code name duplication during cooperative development.

// Package demo. java; public class DefaultException extends Exception // inherits the Exception class {public DefaultException (String msg) {super (msg ); // call the constructor of the parent class} // package demo of the second program. test; import demo. java. defaultException; // import files not in a package. Public class Main {public static void main (String args []) {try {throw new DefaultException ("custom error");} catch (Exception e) {System. out. println (e. toString ();} finally {System. out. println ("how are you! ") ;}} Use of the File to import Java. io. *; public class Main {public static void main (String args []) {File f = new File ("c:/1.txt"); if (f. exists () = false) {try {f. createNewFile ();} catch (Exception e) {System. out. println (e) ;}} System. out. println (f. getName ());}}

The RandomAccessFile class supports "random access" to jump to any text in the file to read data.

In addition, RandomAccessFile has great advantages in random reading of long record format files such as data.

Example

Stream operations:

1. Use the file stream to find a file

2. instantiate the subclass of the byte stream or the hidden stream through the object of the file class.

3. Read and Write Data in bytes (characters)

4. close the file stream.

Differences between character and word throttling

A character is equal to two bytes.

And shard stream are more suitable for reading Chinese Characters

Byte stream is more suitable for reading English

// FileInputStream and FileOutputStream use byte [] arrays to access data import java. io. *; public class Main {public static void main (String args []) {File file = new File ("c: \ 1.txt "); byte a [] = "hello china ". getBytes (); FileOutputStream out = null; FileInputStream in = null; try {out = new FileOutputStream (file); out. write (a); out. close ();} catch (Exception e) {System. out. println (e);} byte B [] = new byte [1024]; int all = 0; try {in = new FileInputStream (file); all = in. read (B); in. close ();} catch (Exception e) {System. out. println (e);} finally {System. out. println (new String (B, 0, all); // you need to write the length of the Byte here. out. println (new String (B ));}}}

These two effects are generated after running.

Stream Application

This avoids frequent conversion between characters and bytes.

Package demo. test; // FileInputStream and FileOutputStream are import java that uses byte [] arrays to access data. io. *; public class Main {public static void main (String args []) throws IOException {BufferedReader cin = new BufferedReader (new nputStreamReader (System. in); String ss = null; ss = cin. readLine (); system. out. println (ss );}}

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.