Small Knowledge (iii): Class

Source: Internet
Author: User

Reference page:

Http://www.yuanjiaocheng.net/entity/tixijiegou.html

Http://www.yuanjiaocheng.net/entity/setenvrionment.html

Http://www.yuanjiaocheng.net/entity/createdatamodel.html

Http://www.yuanjiaocheng.net/Spring/first.html

Http://www.yuanjiaocheng.net/entity/modelbrowser.html

A class is an abstract concept.

Dog classes, for example, describe some of the traits that dogs have, weight, height, age, and yelling, and so on.

public class Dog

{

String dogbreed;//Dog species

int weight; Weight

int height; Increase

int age; Age

public void Bellow ()

{

Console.WriteLine ("Wang Wang");

}

}

This dog class is abstract and describes some characteristics, not a specific dog.

Let's define a neighbor's dog "Big Ha", which belongs to the Dogs category.

Dog Big ha = new Dog ();

Instantiate the object of creating "big Kazakhstan", now "big Kazakhstan" is concrete existence. After the "Big Kazakhstan" object can be described in detail. such as "Big Kazakhstan" dog species is husky, weight 35 kilograms and so on.

Tai ha dogbreed = "Husky";

Da ha. Weight = 35;

......

Let the post "big ha" Roar

Da ha bellow (); This is the way a dog screams.

Note: You cannot assign attributes directly to the dog class. Just like int = 8; , it doesn't make sense. In the case of a particular dog, you cannot say how much the dog weighs, how tall, or how many dogs are planted.

The greatest benefit of a class is that it encapsulates the properties and behavior of an entity in a separate unit of code. According to the above example, dog species, height, weight, age attributes, and growl methods are encapsulated in the dog class.

Access types are public, private, Protected protected (inherited accessible), internal internal, Protected internal internally protected (inherited accessible). Available for classes, fields, methods, properties, constructors.

A class can contain: Fields, properties, constructors, methods.

Class member Methods:

Grammar:

Access type return type method name (accept parameter,)

{Method Body}

Access type Default public

Such as:

public void Method (int i, string s,.......)

{Method Body}

Multiple methods: Same as method name, parameter is different

public string d (int i, int j)

{ }

public void D (string i)

{ }

public void D (int i)

{ }

public int d (Char d)

{

return 0;

}

Class constructor:

Initializes the member variable when new creates the object. The function name and class name of the constructor are the same. You can have multiple constructors, like multiple methods.

Cases:

Class Dog

{

Public Dog (String Dogbreed,int Weight)

{

Weight = weight;

This.dogbreed = Dogbreed;

}

Public Dog (String dogbreed)

{

This.dogbreed = Dogbreed;

}

String dogbreed;//Dog species

int weight; Weight

}

The example above contains two multiple constructors that accept different parameters.

This represents the Dogbreed field for the current instance (referencing the current object), the dog instance.

Class member variable (field):

In the example above, dogbreed, weight is the class member variable. It can be initialized at the time of definition without having to perform initialization in the constructor for each variable.

Class member properties:

C # provides get;set; accessors. Encapsulates the data of a class with attributes.

Cases:

private int height;

public int Height

{

get {return weight;} read

set {weight = value;} Assigned to

}

In the example above, the other classes cannot read the height directly, only through the accessor access operation.

The access type can be set before get set.

Object destruction:

Destructors, destructor declarations in C #:

~textclass ()

{

Destruction processing

}

Can also be written as:

Textclass.finalize ()

{

Destruction processing

Base.finalize ();

}

The above is my personal study document. If there is a mistake in this article, please point it out. Thank you!

Reference:C # programming Practical treasure. Tsinghua University Press, Fu Qiang Ding ning and other writers. Chapter Three.

Small Knowledge (iii): 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.