C# 面向对象之类和方法

一、新建一个类,用来存放属性和方法( 属性和方法写在同一个类中)。

先新建一个类:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _9._02WeekendHomeWork
 8 {
 9     /*
10      * 创建人:庞
11      * 类型:P27 第一题
12      * 时间:2017.09.02
13      * 
14      * **/
15     public class Student
16     {
17         /// <summary>
18         /// 创建自我介绍的方法
19         /// </summary>
20         /// <param name="Class"></param>
21         /// <param name="Name"></param>
22         /// <param name="Age"></param>
23         /// <param name="Like"></param>
24         public void SelfIntroduction()
25         {
26             Console.WriteLine("我来自"+Class+"班,我叫"+Name+",我今年"+Age+"岁,我的爱好是"+Like+"");           
27         }
28         
29         #region 学生属性                
30         /// <summary>
31         /// 学生班级
32         /// </summary>
33         public string Class { get; set; }
34         
35         /// <summary>
36         /// 学生姓名
37         /// </summary>
38         public string Name { get; set; }
39         /// <summary>
40         /// 学生年龄
41         /// </summary>
42         public int Age { get; set; }
43         /// <summary>
44         /// 学生爱好
45         /// </summary>
46         public string Like { get; set; }
47         #endregion
48     }
49 }
View Code

然后在入口函数中将上面新建的类进行实例化,并给属性进行赋值,并调用函数(类和入口函数在两个页面上)。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _9._02WeekendHomeWork
 8 {
 9     /*
10      * 创建人:庞
11      * 类型:P27 第一题
12      * 时间:2017.09.02
13      * 
14      * **/
15     class Program
16     {
17         static void Main(string[] args)
18         {
19             //实例化
20             Student student1 = new Student();
21             student1.Class = "T806";
22             student1.Name = "小李";
23             student1.Age = 18;
24             student1.Like = "唱歌";
25             //调用函数
26             student1.SelfIntroduction();
27             //实例化
28             Student student2 = new Student();
29             student2.Class = "T803";
30             student2.Name = "小黄";
31             student2.Age = 20;
32             student2.Like = "nv";
33             //调用函数
34             student2.SelfIntroduction();
35             Console.WriteLine("请按任意键继续……");
36             Console.ReadKey();
37         }
38     }
39 }
View Code

显示效果:

 

 

二、新建一个类页面,然后在里面新建一个入口函数用来调用方法、实例化、赋值;在下面再新建一个类,用来存放属性和方法。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _9._02WeekendHomeWork.题2
 8 {
 9     /*
10     * 创建人:庞
11     * 类型:P27 第2题
12     * 时间:2017.09.02
13     * 
14     * **/
15     public class Class2
16     {
17         /// <summary>
18         /// 创建主程序入口函数
19         /// </summary>
20         /// <param name="args"></param>
21         static void Main(string[] args)
22         {
23             Console.WriteLine("请输入姓名:");
24             string name = Console.ReadLine();
25             Console.WriteLine("请输入年龄,如果您输入的年龄有误,默认为18岁:");
26             int age = Convert.ToInt32(Console.ReadLine());
27             Console.WriteLine("请输入您的爱好,爱好不能超过20个字符:");
28             string like = Console.ReadLine();
29             //给属性赋值
30             dierti dierti = new dierti();
31             dierti.Name = name;
32             dierti.age = age;
33             dierti.Like = like;
34             dierti.Check();
35             Console.WriteLine("请按任意键继续……");
36             Console.ReadKey();
37 
38         }
39         /// <summary>
40         /// 学生信息类
41         /// </summary>
42         public class dierti
43         {
44             #region 属性
45             /// <summary>
46             /// 输入姓名
47             /// </summary>
48             public string Name { get; set; }
49             /// <summary>
50             /// 年龄
51             /// </summary>
52             public int age { get; set; }
53             public string Like { get; set; }
54             #endregion
55             /// <summary>
56             /// 封装方法
57             /// </summary>
58             public void Check()
59             {
60                 Console.WriteLine("\n你刚才输入的姓名:{0}", Name);
61                 if (age < 0 || age == -1)
62                 {
63                     Console.WriteLine("年龄:18");
64                 }
65                 else
66                 {
67                     Console.WriteLine("年龄:{0}", age);
68                 }
69                 if (Like.Length > 20)
70                 {
71                     Console.WriteLine("对不起,爱好不能超过20个字符!");
72                 }
73                 else
74                 {
75                     Console.WriteLine("爱好:{0}", Like);
76                 }
77             }
78         }
79     }
80 
81 }
View Code

显示效果:

 

1.调试的时候要修改启动项,方法步骤如下:

首先:

然后:

三、使用不同的构造函数来创建employ类

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _9._02WeekendHomeWork.题3
 8 {
 9     /*
10     * 创建人:庞
11     * 类型:P57 第1题
12     * 时间:2017.09.03
13     * 
14     * **/
15     public class demo03
16     {
17         /// <summary>
18         /// 建立主入口函数
19         /// </summary>
20         /// <param name="args"></param>
21         static void Main(string[] args)
22         {
23             Employee employee = new Employee();
24             employee.Method01("小李");
25             employee.Method02("小杨",1001,"主管");
26             Console.WriteLine("请按任意键继续……");
27             Console.ReadKey();
28         }
29         /// <summary>
30         /// 建立员工类
31         /// </summary>
32         public class Employee
33         {
34             #region 定义属性
35             public string Name { get; set; }
36             public int Id { get; set; }
37             public string Job { get; set; }
38             #endregion
39             /// <summary>
40             /// 创建方法1
41             /// </summary>
42             public void Method01(string Name)
43             {
44                 Console.WriteLine("我的姓名为{0}",Name);
45             }
46             /// <summary>
47             /// 创建方法2
48             /// </summary>
49             public void Method02(string Name,int Id,string Job)
50             {
51                 Console.WriteLine("我的姓名为{0},员工编号为{1},岗位是{2}。",Name,Id,Job);
52             }
53         }
54     }
55 }
View Code

显示效果:

 

四、使用方法的重载调用带参数的函数

1.重载的特点:一同三不同

  1. 方法名称一样;
  2. 方法的参数个数不一样;
  3. 参数类型不一样;
  4. 当参数的类型相同时,参数的顺序不一样;

2.注意:方法的重载一定要在同一个类中进行;否则不能称之为重载。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _9._02WeekendHomeWork.题3
 8 {
 9     /*
10     * 创建人:庞
11     * 类型:P57 第1题
12     * 时间:2017.09.03
13     * 
14     * **/
15     public class demo03
16     {
17         /// <summary>
18         /// 建立主入口函数
19         /// </summary>
20         /// <param name="args"></param>
21         static void Main(string[] args)
22         {
23             Employee employee = new Employee();
24             employee.Method01("小李");
25             employee.Method02("小杨",1001,"主管");
26             Console.WriteLine("请按任意键继续……");
27             Console.ReadKey();
28         }
29         /// <summary>
30         /// 建立员工类
31         /// </summary>
32         public class Employee
33         {
34             #region 定义属性
35             public string Name { get; set; }
36             public int Id { get; set; }
37             public string Job { get; set; }
38             #endregion
39             /// <summary>
40             /// 创建方法1
41             /// </summary>
42             public void Method01(string Name)
43             {
44                 Console.WriteLine("我的姓名为{0}",Name);
45             }
46             /// <summary>
47             /// 创建方法2
48             /// </summary>
49             public void Method02(string Name,int Id,string Job)
50             {
51                 Console.WriteLine("我的姓名为{0},员工编号为{1},岗位是{2}。",Name,Id,Job);
52             }
53         }
54     }
55 }
View Code

3.入口函数为静态方法:

1 static void Main(string[] args)
2         {
3             //chongzai chongzai = new chongzai();
4             methord("小李");
5             methord(201,"小王");
6             methord(502,"小胡","");
7             Console.ReadKey();   
8         }

所以在构造方法时也要标明为静态方法,即要用到static关键字,否则vs会报错:

1 public static void methord(string name)
2         {
3             Console.WriteLine("我来自T102班,我叫{0},我今年18岁,我的爱好是唱歌。", name);
4         }

显示效果:

 

五、方法的重载

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _9._02WeekendHomeWork.题5
 8 {
 9     /*
10      * 创建人:庞花辉
11      * 时间:2017.9.03
12      * 类型:P57 第3题
13      * **/
14     class demo05
15     {
16         /// <summary>
17         /// 创建入口函数
18         /// </summary>
19         /// <param name="args"></param>
20         static void Main(string[] args)
21         {
22             methord01();
23             Console.WriteLine("该正方形的边长为{0}", methord01(5));
24             methord02();
25             Console.WriteLine("该长方形的面积为{0}", methord02(5,6));
26             methord03();
27             Console.WriteLine("该圆的面积为{0}", methord03(2.2));
28             Console.ReadKey();
29         }
30         /// <summary>
31         /// 方法01
32         /// </summary>
33         public static void methord01(){
34             Console.WriteLine("该正方形的边长为5");
35         }
36         public static int methord01(int a){
37             return a * a;
38         }
39         /// <summary>
40         /// 方法02
41         /// </summary>
42         public static void methord02()
43         {
44             Console.WriteLine("该长方形的长和宽为5,6");
45         }
46         public static int methord02(int b,int c)
47         {
48             return b * c;
49         }
50         /// <summary>
51         /// 方法03
52         /// </summary>
53         public static void methord03()
54         {
55             Console.WriteLine("该圆的半径为2.2");
56         }
57         public static double methord03(double r)
58         {
59             return r * r * Math.PI;
60         }
61     }
62 }
View Code

C#中圆周率的书写:

1 public static double methord03(double r)
2         {
3             return r * r * Math.PI;
4         }

显示效果:

 

 

posted @ 2017-09-03 18:50  青红造了个白  阅读(433)  评论(0编辑  收藏  举报