Understanding of enumeration types and Structure Types

Source: Internet
Author: User

Understanding of enumeration types and Structure Types

What is enumeration?

Enumeration is a set of literal values, each of which corresponds to one literal value.

What is enumeration useful?

For example, if we need to show the Four Seasons in the program, we may use 0, 1, 2, and 3 to indicate the spring, summer, autumn, and winter in the four seasons of the year. In this way, the season cannot be intuitively indicated, and such code cannot become robust code. Suppose we define an int type variable seaSon in the program and use it to store the seaSon. In this case, you can assign the int type number that exceeds the defined four seasons to seaSon.

Int seaSon = 5; // This line of code is valid and no error is reported. However, 5 represents the season of the year?

To solve these problems.

 

How to define Enumeration

Enum Season {Spring, Summer, Fall, Winter; // Spring = 0, Summer = 1, Fall = 2, Winter = 3. by default, the literal value of the first Member is 0 // the remaining member, increasing by 1. (except for the first one, It defaults to 0)} enum Season {Spring = 1, Summer, Fall = 4, Winter; // Spring = 1, Summer = 2, fall = 4, Winter = 5 ;}

By default, the literal value in the enumeration is of the int type. We can change it to another Integer type. Example: short, byte, sbyte, ushort, int, uint, long, ulong.

Enum Season: short {Spring, Summer, Fall, Winter} // change the literal value to the short type

How to Use enumeration types

Enum Season {Spring, Summer, Fall, Winter;} Season colorful = Season. fall; Season colorful_1 = Season. summer; // when initializing an enumeration type variable, only values defined in the enumeration can be assigned.

 

Enumeration Operators

Because the literal value in enumeration can only be of the integer type, all operators that apply to the integer type apply to the enumeration type. (Except bitwise AND shift ).

Static void doWork () {Season first = Season. spring; for (int I = 0; I <4; I ++) {Console. writeLine (first); // output Spring, Summer, Fall, Winter in sequence. first ++; // auto-increment operator} Console. writeLine (first); // at this time, first goes beyond the enumerated range, and f outputs 4. }

 

What is Structure

Similar to a class, it has its own fields, methods, and constructor.

What is the usefulness of the structure?

When there is very little data in a class, the memory resources consumed by creating this class are not proportional to the data stored in it. In this case, we can consider replacing classes with structures. Because the structure is a value type and is stored on the stack, the memory overhead is reduced.

How to define structure

structure Time{ private int hours,minutes,seconds;public Time(int hh, int mm,int ss){       this.hours=hh;      this.minutes=mm;     this.ss=ss;}pulbic int Hours(){      return this.hours;}}

Differences between structures and Classes

There are two differences between structures and classes:

1. You cannot define the default constructor by yourself. (There is no constructor parameter ). The following code does not return an error if it is a class.

Structure Time {public Time () {// an error is reported. The default constructor cannot be defined for the structure type .}}

During compilation, the compiler automatically generates a class if no constructor is found in the class. If there is a constructor in the class, it will not be automatically generated. The constructor will automatically generate the constructor if the constructor is not in the structure. Like the default constructor in the class, the constructor in the structure initializes the fields in the structure to null, 0, false based on the corresponding type. you can use a non-default constructor to change the field value. All fields in the initialization structure must be initialized.

Struct Time {private int hours, minutes, seconds; public Time (int hh, int mm) {this. hours = hh; this. minutes = mm; // an error is reported. seconds is not initialized }}

2. In the class, we can define fields for initialization, but not in the structure.

Struct time {private int hours = 0; // error. private int minutes; private int seconds;} cannot be initialized during definition ;}

 

Understanding structure Initialization

Time now = new Time (); // we have created a structure object now. The new Keyword calls the // default constructor of the Time Structure and initializes its field to 0. // because the structure is of the value type, we can also do this: // Time now; // However, an error will be reported when accessing any member in now. Because the fields in now are not initialized, they are all in the // uninitialized state.

We have created a structure type object now. The new Keyword calls the default constructor of the Time Structure and initializes its field to 0.
Because the structure is of the value type, we can also do this:
Time now;
However, if you access any member of now, an error is returned. Because the fields in now are not initialized, they are all uninitialized states.

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.