C# ----- class

C#是一种面向对象编程的语言,所以类的运用是非常实用的。class的使用与c++基本一样,下面可以看一下C#对于class类的使用例程

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace ConsoleApplication1
 8 {
 9     class student
10     {
11         private int age=5;
12         public int Age
13         {
14             get{return age;}
15             set { if (value >= 0) age = value; }
16         }
17     }
18     class rectangle
19     {
20         double length, width;
21         public void create(double x,double y)
22         {
23             length = x;
24             width = y;
25         }
26         public double getArea()
27         {
28             return (length * width);
29         }
30         public void show()
31         {
32             Console.WriteLine("Length={0},width={1},Area={2}",length,width,getArea());
33         }
34     }
35     class Program
36     {
37         static void Main(string[] args)
38         {
39             student s=new student();
40             s.Age = 63;
41             Console.WriteLine("age={0}", s.Age);
42             rectangle r1=new rectangle();
43             r1.create(12.5, 8.2);
44             r1.getArea();
45             r1.show();
46             Console.ReadKey();
47         }
48     }
49 }

运行结果:

age=63
Length=12.5,width=8.2,Area=102.5

 

End. 谢谢!

posted on 2019-09-10 15:29  Milo_lu  阅读(190)  评论(0编辑  收藏  举报

导航