Java Basics-Day 5th

Source: Internet
Author: User

1. Code block

1. Local code block

Define {} Inside the function,

You cannot use the static adornment.

2. Building code Blocks

is also a member of a class, and a member variable and member function sibling.

is related to the member variable successively, but has nothing to do with the member function successively.

When constructing an object, the construction code block is called first and then the constructor.


3. Static code block

Constructs a code block + static modifier.

A static block of code executes when the class is loaded and executes only once.

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

Class.forName (String,boolean Initialize,classloader) can control whether the initialization occurs when loading.

2. Inheritance

1. Extract the content of the commonality.

2. Very good reuse.

3. The keyword used is extends

Class B extends A {...}

4.java can not be multiple inheritance, only single-RE inheritance.

5.super: Super class, function aspect similar to this.

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

6. Subclass overridden Method Permission > Parent class's method permission. (Permission can be enlarged, can be carried forward, cannot be reduced, cannot be privatized).

7.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 ();


3. Final (Final)

1. Modify the class to indicate that the class cannot be inherited.

2. The modification method, which indicates that the method cannot be overridden.

Private methods can be added to final, meaningless, and compiled through. (Impossible to happen)

3. Modifier variables, can only be assigned once. If you do not assign a value, the compilation does not pass.


4. Abstract (abstraction)

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

2. Abstract methods can only exist in abstract classes.

3. Abstract classes must also use the abstract modifier.

4. Which keywords cannot coexist with:

static: Static modifiers make it easier to access public properties, abstract direct access without any meaning

final : Final modification is ultimate, cannot change, abstract need to instantiate, need to manipulate

Private: Abstract class requires subclass inheritance to instantiate, private is not inherited

5. Abstract classes have a construction method that can be overloaded.


5. Interface (interface)

multiple inheritance is not directly supported in Java because of the uncertainty of the call. So Java improved the multi-inheritance mechanism and became a multi-implementation in Java.

interface-oriented programming to reduce coupling degree.

for members that are common to interfaces: and these members have fixed modifiers.

1. Global constants: public static final

2. Abstract method: Public abstract


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

Class Persion {

}

Class Man extends Persion {

}

Class Woman extends Persion {

}

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

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 (...))


Interface Iwhite {

public void Skinwhite ();

}

Interface Irich {

public void Walletrich ();

}

Interface Ibeauti {

public void Facebeauti ();

}

Define a white-rich interface to inherit three interfaces

Interface Whiterichbeauti extends Iwhite, Irich, Ibeauti {

}

Define a male local tyrants class

Class RichMan {

public void Marry (Whiterichbeauti woman) {

System.out.println ("Find love!");

}

}

Class Richmandemo {

public static void Main (string[] args) {

Anonymous internal classes, interface-oriented programming

Whiterichbeauti Womanstar = new Whiterichbeauti () {

public void Skinwhite () {

}

public void Walletrich () {

}

public void Facebeauti () {

}

};

RichMan man = new RichMan ();

Man.marry (Womanstar);

}

}


Java Basics-Day 5th

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.