C # classes, objects, methods, properties (excerpt) thanks to the blog Park, thanks to the provider. Thank me Zhang Shuai brother.

Source: Internet
Author: User
Tags modifiers

C # classes, objects, methods, and properties in a detailed

First, related concepts:

1. Objects: Entities in the real world (objects of all things)

2. Class: A collection of objects with similar properties and methods

3, object-oriented Programming features: Package inheritance polymorphism

4. Three elements of an object: the attribute (what the object is), the method (what the object can do), the event (how the object responds)

☆ Interrelationships: Classes are abstractions of objects, objects are instances of classes, classes are abstract classifications, objects are concrete things.

For example, if the car is a class, a person's Mercedes-Benz is an object, the car's color quality is its properties, start, stop these actions can be defined as a vehicle method.

Ii. definition and grammar of classes

1. Define the class:

[Modifier] class name

{

Class Member

}

2. Class access modifier:public Internal

A) Public: accessible domain is the program where the access is located and any referenced program access is not restricted

Grammar:

public class class name

{

Class Member

}

b) Internal: Accessible domain definition in scope (default access modifier)

Grammar:

(internal) class name

{

Class Member

}

3. Class Members: data members and fields

A) Data members: Fields and Constants

Fields: Variables

Declaration: Type field Name

Cases:

public class Persion
{
public string name;
}

b) Method Member (method is the behavior of the class, tell us what the class can do,)

Example method:

Modifier return value type method name (parameter list)

{

Method body

}

Modifiers: such as: public, private, protected, internal

Return value type: If the method has no return value, use void

Cases:

public class EXAMPLE01//instance method

{

public void SayHello ()//define a method with no parameters and no return

{

Console.WriteLine ("Hello There")

}

static void Main ()

{

EXAMPLE01 obja=new Example01 ()

Obja.sayhello ()//Create object and Invoke method

}

}

static method:

Modifier static return value type method name (parameter list)//Add static keyword

{

Method body

}

Cases:

public class EXAMPLE01//instance method

{

public static void SayHello ()//define a static method

{

Console.WriteLine ("Hello There")

}

static void Main ()

{

Example01.sayhello ()//Do not need to create an object

}

}

the difference between a static method and an instance method : A static method is only related to a class, it does not depend on the existence of an object, and an instance method is used after the object exists.

4. Access modifiers for members:public, private, protected, internal

A) public: publicly owned members

b) Private: Private Member

c) Protected: protection of Members

D) Internal: internal Members

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceconsoleapplication2{ Public classEmployee {Private floatsum;  Public intDay ;  Public floatwage; //Define method output payroll information         Public voidShow () {sum= Day *wage; Console.WriteLine ("working time: {0}, daily wage: {1}, total wage: {2}", day,wage,sum); }    }    classProgram {Static voidMain (string[] args) {Employee Employee=NewEmployee (); Employee.day= -; Employee.wage= -; //Employee.sum: Cannot access because it is a private member//Invoke method Real wagesemployee.        Show (); }    }} 

Iii. instanced object: Keyword: new

Grammar:

Class object Name =new Class ();

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text; namespaceConsoleApplication1 { Public classCar {Private stringCarname; Private stringCartype; Private intPrice ;  Public stringCarname {Get{returnCarname;} Set{Carname =value;} }          Public stringCartype {Get{returnCartype;} Set{Cartype =value;} }                  Public intPrice {Get{returnPrice ;} Set{Price =value;} }            Public voidaction () {Console.WriteLine ("a car named {0}, the model number is {1}, the price is: {2}", Carname,cartype,price); }               }       //create an instance and Access fields and methods    classProgram {Static voidMain (string[] args) {             //create an instance of the car classCar vehicle =Newcar (); //Assigning a value to an instanceVehicle. Carname ="Mercedes"; Vehicle. Cartype="XZ001"; Vehicle. Price=1000000; //Calling Methodsvehicle.action (); }     } }

Iv. attributes

1.

A) concept: a member of a field used to access a class

b) attribute use: ensure data security, data validation

2. Statement:

Access modifier data Type property name

{

Get{return field;} Read accessor, through which the external user can read the value of the property

set{field =value;} Write accessor, through which the external user can assign a value to a property, the value entered by the user is stored in the value keyword, and the input value can be verified

}

Cases:

public class Student

{

private int age; Private fields to prevent direct user access

public int Age

{

Get{return age;} Used to read the value of age

Set

{

if ((value>=18) && (value<=45))

Age=value; Assign to age and verify

Else

age=18;

}

}

}

3. Special attributes:

A) Read-only properties:

Public Data Type property name

{

Get{return field;}

}

b) Automatic attributes:

Public data Type property name {Get;set;}

V. Parameters of the method

1. Value parameter: pass by value

2. Reference parameter: Pass the address of the argument in memory to the method, pass it by address

3. Output parameters: Pass back a result from the method

Keywords: out

4, array parameter: The parameter is only allowed to be a set of arrays, when the method parameter with the params keyword, is the method with the array type parameter (using reference pass)

C # classes, objects, methods, properties (excerpt) thanks to the blog Park, thanks to the provider. Thank me Zhang Shuai brother.

Related Article

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.