C # Advanced (ii) class

Source: Internet
Author: User

I. Overview of the Class

Class is the template that creates the object, each containing data, and provides a way to process and access the data. In other words, classes define each object, which is what data and functionality the "instance" contains.

For example, we define a "Doctor" class and instantiate one. Let's look at the following code:

using System;
namespace gosoa.com.cn
{
public class doctor
{
Public doctor () {}
Public Doc Tor (String Name,byte age)
{
this._name=name;
This._age=age;
}
private string _name;
Private byte _age;
public string Name
{
Get{return this._name;}
Set{this._name=value;}
}
Public byte of age
{
Get{return this._age;}
Set{this._age=value;}
}
public String dosth ()
{
Return "I will treat a person well ~ ~";
}
public static string Doanth ()
{Another static method executed by
return ";
}
}
public class Onedoctor
{
static void Main ()
{
Doctor dc=new doctor ();
DC. Name= "Dick";
DC. age=25;
Doctor dc2=new Doctor ("John", 35);
Console.WriteLine (DC. Name);
Console.WriteLine (DC. Age);
Console.WriteLine (DC2. Name);
Console.WriteLine (DC2. Age);
Console.WriteLine (Dc.dosth ());
Console.WriteLine (Doctor.doanth ());
}
}
}

In this example, the public class doctor declares a class. _name and _age are its two properties. Dosth () is one of its methods (that is, the behavior of the object). The Doctor dc=new Doctor () is used to instantiate a doctor class, and a similar instantiation of an object, resulting in a new doctor. Doctor Dc2=new doctor ("John", 35); it's another class that's instantiated, another doctor.

In the Doctor class, the two methods, public doctor () {} public doctor (String Name,byte age), are called constructors. is used to initialize the class, and is invoked automatically when each class is instantiated.

public string Name
        {
           get{return this._name;}
           set{this._name=value;}
        }

This code is used to set and get the properties of the class. It is similar to the GetName and SetName methods in Java. It's just getting easier in C #.

Note that a class is a reference type that is stored on the managed heap.

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.