Note of the C # Portal-eighth chapter

Source: Internet
Author: User

8.1 Object as a member

1. The members of a class are not only basic types that can make int,double, but can also be objects of other classes;

 classProgram {Static voidMain (string[] args) {Date D=NewDate1992,5, -,NewTime ( A, -,5)); }    }    classDate {intYear ; intmonth; intDay ;  Time t; //(1)classes that contain members of other objects         PublicDateintYearintMonthintDay , time t) {             This. Year =Year ;  This. Month =month;  This. Day =Day ;  This. T =T; }    }    classTime {intHouer; intminute; intsecond;  PublicTime (intHouer,intMinuteintsecond) {             This. Houer =Houer;  This. Minute =minute;  This. Second =second; }    }

8.2 Static Constants

(1) A static variable is a variable modified with the Satic keyword

(2) Static variables can only use the class name reference cannot use object invocation

    classProgram {Static voidMain (string[] args) {s S=NewS (); //s.number=5;//error (2) static variable can only use class name reference cannot use object invocationS.setsnumber ();            Console.WriteLine (S.number); S.number=4;            Console.WriteLine (S.number);                Console.readkey (); }    }    classS { Public Static intNumber//(1) A static variable is a variable modified with the Satic keyword         PublicS () { number=0; }         Public voidSetsnumber () { number=5; }    }

(3) Static method is the method of satic keyword modification

(4) A static method can only use a class name reference and cannot use an object reference

    classProgram {Static voidMain (string[] args)            {A.print (); A A=NewA (); //a.print (); error (2) A static method can only use a class name reference and cannot use an object reference        }    }    classA { Public Static voidPrint ()//(1) static method is the method of satic keyword modification{Console.WriteLine ("Print");        Console.readkey (); }       }

8.3 Constants

8.3.1 Const constant

(1) Const constant Definition method: access rights + const + Type + constant name = initial value;

(2) Const constants must be assigned at the same time as defined;

(3) const constants are implicitly static (cannot be decorated with the static keyword), so they can only be referenced using the class name

    classProgram {Static voidMain (string[] args) {Console.WriteLine (circle. PI);//(3) const constants are implicitly static (cannot be decorated with the static keyword), so they can only be referenced using the class nameConsole.readkey (); }    }    classCircle {//Public const double PI; (2) Const constants must be assigned at the same time as defined;//pi=3.14;         Public Const DoublePI =5;//(1) Const constant Definition method: access rights + const + Type + constant name = initial value;}

8.3.2 ReadOnly Constants

(1) ReadOnly constant definition method: Same as const

(2) ReadOnly constants are non-static constants, each object can have a different value, can be initialized in the constructor (also can be assigned at the time of definition);

(3) because the ReadOnly constant is non-static, like a normal variable, using an object reference, you cannot use the class name reference

    classProgram {Static voidMain (string[] args) {Hotel H1=NewHotel -); Hotel H2=NewHotel -); Console.WriteLine (h1.number);//(3) because the ReadOnly constant is non-static, like a normal variable, using an object reference, you cannot use the class name referenceConsole.WriteLine (H2.number); //Console.WriteLine (hotel.number);//ErrorConsole.readkey (); }    }}classhotel{ Public ReadOnly intNumber=0;//(1) readonly constant definition Method     PublicHotelintNumber//(2) ReadOnly constants are non-static constants, each object can have a different value, can be initialized in the constructor (also can be assigned at the time of definition);    {         This. Number =Number ; }

8.4 Overloading

(1) The function name is the same, the number of arguments or different types of parameters constitute overloading

(2) The principle of function overload invocation is "best Match", the function that the system or call parameter most matches

classProgram {Static voidMain (string[] args) {            //(2) The principle of function overload invocation is "best Match", the function that the system or call parameter most matchesConsole.WriteLine (Calculate.add (1,1)); Console.WriteLine (Calculate.add (1.1,1)); Console.WriteLine (Calculate.add (1,1,1));        Console.readkey (); }    }    classCalculate { Public Static intAddintAintb) {returnA +b; }     Public Static DoubleAddDoubleADoubleb//(1) The function name is the same, the number of arguments or different types of parameters constitute overloading    {        returnA +b; }     Public Static intAddintAintBintc) {returnA + B +C; }        }

8.5 to Be continued

Note of the C # Portal-eighth chapter

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.