C # Learning 9

Source: Internet
Author: User

C # Learning 9: class and Object

Class and constructor, create a circle class, the default radius is 0, count statistics have several circles


[Csharp]
// Circle. cs
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Namespace Cambridge
{
Class Circle
{
Public int radius;
Public static int count = 0; // only the class name can use count
Public Circle () // Constructor
{
Radius = 0;
Count ++;
}
Public Circle (int t) // overload Constructor
{
Radius = t;
Count ++;
}
Public double Area ()
{
Return Math. PI * radius;
}
}
}
// Program. cs
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Namespace Cambridge
{
Class Program
{
Static void Main (string [] args)
{
Circle c1, c2;
C1 = new Circle ();
Double area1 = c1.Area ();
Console. WriteLine (area1 );
C2 = new Circle (10 );
Double area2 = c2.Area ();
Console. WriteLine (area2 );
Console. WriteLine ("the number of circles is" + Circle. count );
}
}
}

Create a vertex class, enter the coordinates of the vertex, and find the distance from the vertex to the origin

[Csharp]
// Point. cs
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Namespace Cambridge
{
Class Point
{
Private int x;
Private int y;
Public Point ()
{
X = 0;
Y = 0;
}
Public Point (int a, int B)
{
X =;
Y = B;
}
Public double Dis (Point p) // The parameter is an object
{
Int xdif = x-p. x;
Int ydif = y-p. y;
Return Math. Sqrt (xdif * xdif + ydif * ydif );
}
}
}
// Program. cs
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Namespace Cambridge
{
Class Program
{
Static void Main (string [] args)
{
Int x1 = int. Parse (Console. ReadLine ());
Int y1 = int. Parse (Console. ReadLine ());
Point p1 = new Point ();
Point p2 = new Point (x1, y1 );
Double ans = p2.Dis (p1 );
Console. WriteLine ("the distance between this point and the origin is" + ans );
}
}
}

Classification

Partial classification definition, in short, is to use partial classification definition so that the class definition spans multiple files.

For example, you can put fields, attributes, and constructors in one file, and put methods in another file.

Therefore, you only need to use the partial keyword for the class in each file that contains the definition of the partial category.

Create two classes in a project: Circle1.cs, Circle2.cs, and Program. cs. You can call these two classes.

[Csharp]
// Circle1.cs
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Namespace cam
{
Partial class Circle
{
Public Circle ()
{
This. radius = 0;
}
Public Circle (int t)
{
This. radius = t;
}
}
}
// Circle2.cs
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Namespace cam
{
Partial class Circle
{
Public int radius;
Public double Area ()
{
Return Math. PI * this. radius * this. radius;
}
}
}
// Program. cs
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Namespace cam
{
Class Program
{
Static void Main (string [] args)
{
Circle c1 = new Circle ();
Double area1 = c1.Area ();
Console. WriteLine (area1 );
Circle c2 = new Circle (10 );
Double area2 = c2.Area ();
Console. WriteLine (area2 );
}
}
}

Anonymous class

The anonymous class can temporarily store data. The life cycle is only in this method, and the method ends, and the life cycle of this type is gone.

[Csharp]
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Namespace com
{
Class Program
{
Static void Main (string [] args)
{
Var Obj1 = new {Name = "Tom", Age = 18 };
Console. WriteLine (Obj1.Name + "" + Obj1.Age );
Var Obj2 = new {Name = "Jerry", Age = 18 };
Console. WriteLine (Obj2.Name + "" + Obj2.Age );
Obj1 = Obj2;
Console. WriteLine (Obj1.Name + "" + Obj1.Age );
}
}
}


 

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.