Java Foundation Fifth Day

Source: Internet
Author: User
Tags modifiers

1. Text describes the role of static code blocks.


Static code blocks are executed when the class is loaded, and

and executes only once.


*/

/*

Class Static {

static {

System.out.println ("Static");

}

Public Static () {

System.out.println ("()");

}

}


Class Demo {

public static void Main (string[] arge) {

try {

Class.forName ("Static");

Class.forName ("Static");

} catch (Exception e) {

E.printstacktrace ();

}

}

}

*/


/*

2. What is the order in which the code blocks and constructors are called? is the declaration position of the method relevant? Why? What is the purpose of building blocks of code?

For:

Constructing code block invocation order takes precedence over the construction method, regardless of the declaration position of the method.

Building blocks of code: A. Common code that can hold multiple construction methods to increase the repeatability of the Code

B. Initialize the member variable


*/



/*

Class Static {

static {

System.out.println ("Static");

}

Public Static () {

System.out.println ("()");

}

{

System.out.println ("code block");

}

}


Class Demo {

public static void Main (string[] arge) {

try {

Class.forName ("Static");

Class.forName ("Static");

} catch (Exception e) {

E.printstacktrace ();

}

New Static ();

}

}




3. Will the static code block be executed when the class loads? How do I manually control class loading?

For:

Static code blocks are executed by default when the class loads.

Manual control class loading uses Class.forName ("ClassName"), which executes the code block when the class is loaded by default.



4. Define class Person,man,woman to form an inheritance relationship


*/

/*

Class Person {

}


Class Man extends Person {

}


Class Woman extends Person {

}




5. What is an abstract method? What is an abstract class? Do abstract classes have constructors and can they be overloaded?

For:

The abstract method is: There is no method body method. You must use the abstract adornment.

Abstract classes: Classes that contain abstract methods, that is, abstract classes, and abstract classes must also use abstract adornments.

Abstract classes have constructors and can be overloaded

*/

/*

Abstract class Person {

Person () {

}

Person (String a) {

System.out.println (a);

}

}


Class Man extends Person {

Man () {

Super ("Baba");

}

}

Class Demo {

public static void Main (string[] arge) {

New Man ();

}

}


6. What are the illegal combinations of abstract modifiers? and give a reasonable explanation?

For:

Abstract + static is an illegal modifier combination. Static modifiers make it easier to access public properties, abstract direct access without any meaning

Abstract + final is an illegal modifier combination. Final retouching is the ultimate, cannot change, abstract need to instantiate, need to manipulate

Abstract + private is an illegal modifier combination. Abstract classes require subclass inheritance to instantiate, and private cannot inherit




7.super and this role as well as usage and precautions?

For:

Super: Super class, similar in function to this.

Super (...): accesses the constructor of the superclass. can only be used on the first line. The default is super ();

This is a reference to the object itself only

This: a reference to the object itself.

This (...) Represents the constructor that invokes the current class. Can only be used in the first row. Default this ();


8. Defining the interface

1.IWhite

2.IRich

3.IBeanti

4. Define the class to implement the above three interfaces. (Womenstar)

5. Define the Local Tyrants class (Earchricher.marring (...))





*/


/*

5. Define the Local Tyrants class (Earchricher.marring (...))

For:


* Local tyrants marriage case, the object of marriage requires at least three characteristics of white and rich beauty, local tyrants marriage with anonymous inner class object implementation.

Interface Iwhite {

}


Interface Irich {

}


Interface Ibeanti {

}


Interface Iwhiteirichibeanti extends Iwhite,irich,ibeanti {

Static final String name = "a";

public abstract void Show ();

}


Class Womenstar implements Iwhiteirichibeanti {

public void Show () {}

}



Class Earchricher {

Iwhiteirichibeanti BFM;

Public Earchricher (Iwhiteirichibeanti BFM) {

THIS.BFM = BFM;

}

public void ShowName () {

Bfm.show ();

}


}


Class Demo {

public static void Main (string[] arge) {

Iwhiteirichibeanti BFM = new Iwhiteirichibeanti () {

public void Show () {

System.out.println ("My name is Tortuga");

}

};

Earchricher man = new Earchricher (BFM);

Man.showname ();

}

}


Java Foundation Fifth Day

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.