7.1 方法的声明
在面向对象的程序设计中,正是通过构建不同的方法来完成对类中数据的访问,同时其他的类和对象也是通过方法实现对本类数据的访问。
方法是类中用于计算或进行其他操作的成员。类的方法主要用来操作类的数据,提供一种访问数据的途径。
7.2 方法的参数
任何方法都包含数量不一的参数。方法的参数包含在参数列表中,该列表中的参数称为形式参数,调用这个方法时提供的参数叫实(值)参数。
除参数个数外,按照传递方式的不同,参数还分为不同的类型,C#支持四种类型的参数,分别为:
- 值类型:不含任何修饰符;
- 引用类型:使用ref修饰符声明;
- 输出参数:使用out修饰符声明;
- 参数数组:使用params修饰符声明。
7.2.1 值类型参数传递
public void Swap(int x, int y)
{
int temp = x;
x = y;
y = temp;
}
Swap(6, 8);
采用值传递方式进行传递时,编译器首先将实参的值做一份拷贝,并且将此拷贝传递给被调用方法的形参。可以看出这种传递方式传递的仅仅是变量值的一份拷贝,或是为形参赋予一个值,而对实参并没有做任何的改变,同时在方法内对形参值的改变所影响的仅仅是形参,并不会对定义在方法外部的实参起任何作用。
using System;
using System.Collections.Generic;
namespace swap
{
class MainClass
{
static void Swap(int x, int y)
{
int temp = x;
x = y;
y = temp;
}
public static void Main(string[] args)
{
int i = 10;
int j = 20;
Console.WriteLine("交换前i={0}, j={1}", i, j);
Swap(i, j);
Console.WriteLine("交换后i={0}, j={1}", i, j);
Console.ReadLine();
}
}
}
7.2.2 引用类型参数传递
public void Swap(ref int x,ref int y)
{
int temp = x;
x = y;
y = temp;
}
Swap(ref 6,ref 8);
引用类型传递方式下,方法的参数以ref修饰符声明。引用型参数不开辟新的内存区域,即形参和实参共用同一内存块。当利用引用型参数向方法传递形参时,编译程序将把实际值在内存中的地址传递给该方法。
为了传递引用类型参数,必须在方法声明和方法调用中都明确地在参数前指定ref关键字,并且实参变量在传递给方法前必须进行初始化。
using System;
using System.Collections.Generic;
namespace swap
{
class MainClass
{
static void Swap(ref int x, ref int y)
{
int temp = x;
x = y;
y = temp;
}
public static void Main(string[] args)
{
int i = 10;
int j = 20;
Console.WriteLine("交换前i={0}, j={1}", i, j);
Swap(ref i, ref j);
Console.WriteLine("交换后i={0}, j={1}", i, j);
Console.ReadLine();
}
}
}
7.2.3 输出类型参数传递
public void Swap(out int x,out int y)
{
int x=10,y=20;
int temp = x;
x = y;
y = temp;
}
Swap(out i,out j);
输出参数以out修饰符声明。和ref类似,它也是直接对实参进行操作。在方法声明和方法调用时都必须明确地指定out关键字。out参数声明方式不需要变量传递给方法前进行初始化,因为它的含义只是用作输出目的。但是,在方法返回前,必须对out参数进行赋值。该类型参数通常用在需要多个返回值的方法中。
using System;
using System.Collections.Generic;
namespace swap
{
class MainClass
{
static void Swap(out int x, out int y)
{
x = 10;
y = 20;
int temp = x;
x = y;
y = temp;
}
public static void Main(string[] args)
{
int i;
int j;
Swap(out i, out j);
Console.WriteLine("交换后i={0}, j={1}", i, j);
Console.ReadLine();
}
}
}
class Program
{ /// <summary>
/// 利息计算
/// </summary>
/// <param name="x">本金</param>
/// <param name="n">年限</param>
/// <param name="interest">最后一年的年息</param>
/// <returns>本息合计金额</returns>
public static double Gain(double x, int n, out double interest)
{
int i = 0;
do{
interest=x*0.08;
x+=interest;
i++;
}while(i<n);
return x;
}
static void Main(string[] args)
{
double interest1; //保存最后一年利息的变量
Console.WriteLine("10000元在2年后的本息合计金额是{0}", Gain(10000, 2, out interest1));
Console.WriteLine("最后一年的年息是:{0}", interest1);
Console.ReadLine();
}
}
7.2.4 数组类型参数传递
方法的参数中可以包含数组,但如果参数列表中包含有数组,那么数组必须在参数表中位列最后(只能有一个params数组参数),并且只允许是一维数组。数组型参数不能再有ref或out修饰符。数组类型的参数传递应以关键字params来标识。
数组类型参数传递一般用在数组元素个数不定的情况。
using System;
using System.Collections.Generic;
namespace param
{
class Test
{
static void F(params int[] args)
{
Console.WriteLine(“\n参数个数: {0}", args.Length);
for (int i = 0; i < args.Length; i++)
Console.WriteLine("\targs[{0}] = {1}", i, args[i]);
}
static void Main()
{
F();
F(1);
F(1, 2);
F(1, 2, 3);
F(new int[] { 1, 2, 3, 4 });
Console.ReadLine();
}
}
}
7.3 方法的重载
方法的重载既是函数的重载,重载允许一组具有相似功能的函数具有相同的函数名,只不过它们的参数类型或参数个数略有差异。
类的方法的重载也是类似的,类的两个或两个以上的方法,具有相同的方法名,只要他们使用的参数个数或是参数类型不同,编译器变能够根据实参的不同确定在哪种情况下调用哪个方法,这就构成了方法的重载。
using System;
using System.Collections.Generic;
namespace congzai
{
class MainClass
{
public static int max(int x, int y)
{
if (x > y)
return x;
else
return y;
}
public static int max(int x, int y, int z)
{
if (x > y)
{
if (x > z)
return x;
else
return z;
}
else
{
if (y > z)
return y;
else
return z;
}
}
public static float max(float x, float y)
{
if (x > y)
return x;
else
return y;
}
public static void Main(string[] args)
{
Console.WriteLine("2,3,4三个正
数的最大值为:{0}", max(2, 3, 4));
Console.WriteLine("2,3两个整数
的最大值为:{0}", max(2, 3));
Console.WriteLine("2.5,4.5两个小
数的最大值为:{0}", max(2.5f, 4.5f));
Console.ReadLine();
}
}
}
构造函数重载
using System;
using System.Collections.Generic;
namespace Acc
{
class Account
{
private string Password;
private double Balance;
public Account()
{
this.Password = "888888";
this.Balance = 0.00d;
}
public Account(string password, double balance)
{
this.Password = password;
this.Balance = balance;
}
public void OutPassBal()
{
Console.WriteLine("--------------------");
Console.WriteLine("账户密码:{0}",
this.Password);
Console.WriteLine("账户余额:{0}",
this.Balance);
}
}
class test
{
public static void Main(string[] args)
{
Account account = new Account();
account.OutPassBal();
Account account2 = new
Account("asdf", 63.5);
account2.OutPassBal();
Console.ReadLine();
}
}
}
作业:读程序,写运行结果。
注意:程序和答案都要写在作业纸上,不要上机调试。
第1题:
public static void Main()
{ int x=1,a=0,b=0;
switch(x)
{
case 0: b++ ; break;
case 1: a++ ; break;
case 2: a++ ; b++ ; break;
}
Console.WriteLine(“a={0},b={1}”,a,b);
}
第2题:
static void Main(string[] args)
{
int Sum1 = 0, Sum2 = 0;
int i = 0;
while (++i < 10)
{
Sum1 += 1;
}
i = 0;
do
{
Sum2 += 1;
} while (++i < 10);
Console.WriteLine("Sum1 = {0}",Sum1);
Console.WriteLine("Sum2 = {0}",Sum2);
}
第3题:
using system;
class Example1
{
static long fib(int n)
{
if(n>1) return(fib(n-1)+fib(n-2));
else return (2);
}
public static void Main()
{
Console.WriteLine(“{0}”,fib(3));
}
}
第4题:
class Program
{
public static void F(string s)
{
for (int i=0;i<s.Length;i+=2)
Console.Write(s[i]);
}
public static void Main( )
{
string str="syd168编写及收集整理" ;
F(str);
Console.ReadLine();
}
}
第5题:
class Program
{
public static void Fun()
{ int oddsum = 0;
int evensum = 0;
int[] arr ={ 1, 5, 2, 3, 6, 7, 12, 15 };
foreach (int k in arr)
{
if (k % 2 == 0) evensum += k;
else oddsum += k;
}
Console.WriteLine("evensum={0}", evensum);
Console.WriteLine("oddsum={0}", oddsum);
}
static void Main()
{
Fun();
Console.ReadLine();
}
}