C # Supplements's Trivia (iii): Class

Source: Internet
Author: User
is a concept of abstraction.

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;//breed int weight;//weight int height;//elevation int age;//Old public void bellow () {Con Sole.  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) {RET  Urn 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;//the 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}

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 () {destructor}

Can also be written as:

 

Textclass.finalize () {destructor processing base.finalize (); }

The above is the C # supplements of the Small Knowledge (c): The content of the class, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.