Object-oriented learning [Class-Anonymous class]

Source: Internet
Author: User

The anonymous class is like this word. It is a class without a name. It is a bit confusing to see this anonymous class, if you think that a class has no name, how can we use it to create an instance? The answer is that we can use VaR to declare an anonymous class. The created format is:
New anonymousclassname {Field 1, Field 2 ,........}

Note:

All fields in the anonymous class are public, which means that we can access this so-called Anonymous class at will;

There are many restrictions on this anonymous class, for example:

1. You must Initialize an anonymous class;

2. Only public fields can be included;

3. static fields cannot be declared;

4. It cannot be declared or implemented in it;

 

A simple example of an anonymous class:

VaR perinfo = new mytest {name = "Barney", age = 21 };

Console. writeline ("Name: {0}, age: {1}", perinfo. Name, perinfo. Age );

Console. Readline ();

 

/// // Result ////////////////////// ///////////////////////////////

Name: Barney, age: 21

........

......

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.