Homework after class

Source: Internet
Author: User

Name: Su Yue class: Letter 1405-1 Class No. 20142956

In the section on classes and objects, I found that I learned more about some of the foundational issues of the Java course, as if everything was clear to me.

In this chapter, there are many hands-on brain problems, such as:

Why is the following code not compiled? Where did it go wrong?

From this we can see that if you define a custom in your programming, the compiler will not start the default customizations. And that's the problem with Foo in this program.

During the initialization of Java, it is best not to have the initialization block, to ensure that a field is initialized only once, and when you construct a function, if you call this function to generate an instance of Initializeblockclass obj=new Initializeblockclass (), whose obj is called the initialized value, and obj = new initializeblockclass (300); It is the value that you enter.

2. Class date{

static{

SYSTEM.OUT.PRINTLN ("100");

}

{

SYSTEM.OUT.PRINTLN ("200");

}

Public Date ()

{

SYSTEM.OUT.PRINTLN ("parameter-free constructor of date");

}

}

Class Cat extends date{

static{

System.out.println ("300");

}

{

SYSTEM.OUT.PRINTLN ("400");

}

Public Mid ()

{

SYSTEM.OUT.PRINTLN ("Cat parameter-free constructor");

}

Public Mid (String msg)

{

Calling overloaded constructors in the same class through this

This ();

System.out.println ("Mid with parametric constructor, its parameter value:" + msg);

}

}

Class Leaf extends cat{

static{

System.out.println ("500");

}

{

System.out.println ("600");

}

Public Leaf ()

{

A constructor that invokes a string argument in the parent class through Super

Super ("Java Initialization sequence demo");

System.out.println ("The constructor that executes the leaf");

}

}

public class Teststaticinitializeblock

{

public static void Main (string[] args)

{

New Leaf ();

}

}

4. The order of static initialization:

If more than one parent class is associated with a subclass of inheritance, static initialization is performed only once, and when the object of the subtype is created, the execution of the parent class's static initialization block is also caused.

Class Root

{

static{

SYSTEM.OUT.PRINTLN ("Static initialization block of Root");

}

{

System.out.println ("Root's normal initialization block");

}

Public Root ()

{

SYSTEM.OUT.PRINTLN ("Root parameter-free constructor");

}

}

Class Mid extends Root

{

static{

SYSTEM.OUT.PRINTLN ("Static initialization block of mid");

}

{

SYSTEM.OUT.PRINTLN ("Normal initialization block for mid");

}

Public Mid ()

{

SYSTEM.OUT.PRINTLN ("Non-parametric constructor for mid");

}

Public Mid (String msg)

{

Calling overloaded constructors in the same class through this

This ();

System.out.println ("Mid with parametric constructor, its parameter value:" + msg);

}

}

Class Leaf extends Mid

{

static{

SYSTEM.OUT.PRINTLN ("Static initialization block of the Leaf");

}

{

SYSTEM.OUT.PRINTLN ("ordinary initialization block of the Leaf");

}

Public Leaf ()

{

A constructor that invokes a string argument in the parent class through Super

Super ("Java Initialization sequence demo");

System.out.println ("The constructor that executes the leaf");

}

}

public class Teststaticinitializeblock

{

public static void Main (string[] args)

{

New Leaf ();

}

}

5, Packagedemo;

public class tongji{

Private Staticint N;

Public Tongji () {

N++;//new N plus 1 for an object;

Public Staticint Getnum () {

Return n;//returns the value of n

}

public static void Main ()

{

Tongji T1 = new Tongji ();

Tongji t2 = new Tongji ();

Tongji T3 = new Tongji ();//create object;

System.out.println ("Number of common objects:" +tongji.getnum ())}}

Homework after class

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.