1、new的用法
using System;
using System.Collections.Generic;
using System.Text;
namespace yanz
{
public class student
{
public string strName;
public student(string _strName)
{
this.strName=_strName;
}
}
class Program
{
static void Main(string[] args)
{
student s=new student("张三");
student t=new student("李四");
Console.WriteLine(t.strName);
Console.WriteLine(s.strName);
Console.ReadLine();
}
}
}
Student为类,“student s=new student("张三”);"中“s”代表张三
2、引用参数
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication2
{
class myclass
{
public void Swap(ref int x,ref int y)
{
int k;
k=x;
x=y;
y=k;
}
}
class Program
{
static void Main(string[] args)
{
int a=8;b=10;
Console.WriteLine("a={0},b={1}",a,b);
myclass mycl=new myclass();
mycl.Swap(ref a,ref b);
Console.WriteLine("a={0},b={1}",a,b);
}
}
}
3、方法重载
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class student
{
public string strname;
public int nage;
public void grow()
{
this.nage++;
}
public void grow(int _nagespan)
{
this.nage +=_nagespan;
}
}
class Program
{
static void Main(string[] args)
{
student s=new student();
s.strname="zhangsan";
s.nage=20;
s.grow();
Console.WriteLine(s.nage);
s.grow(3);
Console.WriteLine(s.nage);
Console.ReadLine();
}
}
}
4、方法的调用
using System;
using System.Collections.Generic;
using System.Text;
namespace Example_ConstructOverload
{
class Time
{
public int nHour,nMinute,nSecond;
public Time()
{
nHour=nMinute=nSecond=0;
}
public Time(int Hour)
{
nHour=Hour;
nMinute=nSecond=0;
}
public Time(int Hour,int Minute)
{
nHour=Hour;
nMinute=Minute;
nSecond=0;
}
public Time(int hour,int Minute,int Second)
{
nHour=hour;
nMinute=Minute;
nSecond=Second;
}
}
class Test
{
static void Main()
{
Time t1,t2,t3,t4;
t1=new Time();
t2=new Time(10);
t3=new Time(10,20);
t4=new Time(10,22,30);
Console.WriteLine("t1的时间为:{0}时{1}分{2}秒",t1.nHour,t1.nMinute,t1.nSecond);
Console.WriteLine("t2的时间为:{0}时{1}分{2}秒:",t2.nHour,t2.nMinute,t2.nSecond);
Console.WriteLine("t3的时间为:{0}时{1}分{2}秒:",t3.nHour,t3.nMinute,t3.nSecond);
Console.WriteLine("t4的时间为:{0}时{1}分{2}秒:",t4.nHour,t4.nMinute,t4.nSecond);
Console.ReadLine();
}
}
}