zy17
枚举结构体
枚举
enum 类型名 {枚举项}
定义在类外面
enum WeekDays{Sunday,Monday,Tuesday,Wednesday,Thuresday,Friday,Saturday}
Main()函数里面写
WeekDays today=WeekDays.Sunday;
if((today==WeekDays.Saturday)||(today=WeekDays.Sunday)){
Console.WriteLine("休息");
}
else{
Console.WriteLine("工作");
}
WeekDays today;
today=(WeekDays)3;
Console.WriteLine(today);//Wed
WeekDays today;
today=WeekDays.Sunday;
Console.WriteLine((int)today);//0
WeekDays today;
today=WeekDays.Sunday+2;
Console.WriteLine(today);//周二
enum WeekDays:int//byte sbyte short ushort int uint long ulong
{
Sunday=5,
Staurday //6
}
结构体
定义在类外面
struct Student{
//必须加public否则外部不能访问
public int number;
public string name;
public string sex;
public string birthday;
}
Main()
Student wjl;
wjl.number=2015;
wjl.name="吴家亮";
wjl.sex="男";
wjl.birthday="2000-1-1";
Student zy;
zy.number=2016;
zy.name="曾瑛";
zy.sex="男";
zy.birthday="1997-1-1";
Console.WriteLine("学号:"+wjl.number);
Console.WriteLine("学号:"+zy.number);
Console.WriteLine("姓名:"+wjl.name);
Console.WriteLine("姓名:"+zy.name);
zy18
数组
int[] scores= {120,140,112,133};
元素类型 数组名称 初始列表值
int[] scores={120,140,112,133,127};
scores[0]=110;
int sum=scores[0]+scores[1]+scores[2]+scores[3]+scores[4];
int sum=0;
for(int i=0;i<scores.Length;i++){
sum+=scores[i];
}
声明数组而不初始化
string[] names =new string[3];
names[0]="小明";
names[1]="里斯";
names[2]="王五";
string[] names =new string[3]{"1","2","3"};
数组不可以是动态的 c++中可以
foreach(string name in names){
Console.WriteLine(name);
}
zy19
二维数组
int[,] erwei={{1,2,3},{4,5,6},{7,8,9}};
int[,] erwei=new int[3,3]{{1,2,3},{4,5,6},{7,8,9}};
int sum=0;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
Console.WriteLine(erwei[i,j]);
sum+=erwei[i,j];
}
}
Console.WriteLine(sum);
int[,] erwei=new int[3,3]{{1,2,3},{4,5,6},{7,8,9}};
int sum=0;
foreach(int number in erwei){sum+=number;}
Console.WriteLine(sum);
可变数组
int[][] kebian=new int[2][]
行列
kebian [0]=new int[3]{1,2,3};
kebian [1]=new int[4]{1,2,3,4};
foreach(int element in kebian[0]){
Console.WriteLine(element);
}
foreach(int element in kebian[1]){
Console.WriteLine(element);
}
int[][] kebian=new int[2][]{new int[3]{1,2,3},new int[4]{1,2,3,4}};
foreach(int[] line in kebian){
foreach(int element in line){
Console.WriteLine(element);
}
}
zy20
函数
写在类里面
static string/返回类型值/ CaculateWeekDay(int y,int m,int d)/方法/{
if(m==1||m=2){
m+=12;
y--;
}
int week=(d+2m+3(m+1)/5+y+y/4-y/100+y/400+1)%7;
string weekString="";
switch(week){
case 0:weekString="星期日";break;
case 1:weekString="星期一";break;
case 2:weekString="星期二";break;
case 3:weekString="星期三";break;
case 4:weekString="星期四";break;
case 5:weekString="星期五";break;
case 6:weekString="星期六";break;
default:break;
}
//Console.WriteLine(weekString);
return weekString;
}
Main()
string week=CaculateWeekDay(2018,9,9)
Console.WriteLine(week);
static string 函数名(参数列表)
静态 返回类型
函数命名方式:首字母全部大写
return语句可以返回表达式
函数可以没有返回值void
可以有多个return,先执行到那个那个起作用
zy21
函数参数
值传递
类里面定义
static double Bigger(double x,double y)//形式参数
{//形参的变化不会影响实参
double maxValue=(x>y)?x:y;
return maxValue;
}
Main()
double a,b,result;
a=Convert.ToDouble(Console.Read());
b=Convert.ToDouble(Console.Read());
result=Bigger(a,b);
Console.WriteLine(result);
地址传递形参实参一起变化
类里面定义
static void Dou(int[] array)
{
for(int i=0;i<array.Length;i++){
array[i]=2array[i]
}
}
Main()
int[] numbers={1,2,3,4,5};
Console.Write("Before:");
foreach(int number in numbers){
Console.WriteLine(number);
}
Dou(numbers);
Console.Write("After:");
foreach(int number in numbers){
Console.WriteLine(number);
}
数组等复杂类型应用地址传递
static void Swap(int x,int y){
int temp=x;
x=y;
y=temp;
}
Main()
int a=40;
int b=80;
Console.WriteLine("交换前a={0},b={1}",a,b);
Swap(a,b);
Console.WriteLine("交换后a={0},b={1}",a,b);//a=40,b=80
引用型参数
static void Swap(ref int x;ref int y){
int temp=x;
x=y;
y=temp;
}
Main()
int a=40;
int b=80;
Console.WriteLine("交换前a={0},b={1}",a,b);
Swap(ref a,ref b);
Console.WriteLine("交换后a={0},b={1}",a,b);//a=40,b=80
输出型参数
ref传入函数前必须赋值
out传入函数前不需要赋值,赋值了也会被忽略
static double CalculateCircle(double r,out double c){
c=2Math.PIr;
double s=Math.PIrr;
return s;
}
Main()
double r=156;
double zhouchang;
double area=CalculateCircle(r,out zhouchang);
Console.WriteLine("周长为:{0},面积为{2}",zhouchang,area);
参数匹配,可以隐士转换
static double VolumeofSpheroid(double r){
double v=(4/3)Math.PIrrr;
return v;
}
Main()
double redius1=10.86;
double volume1=VolumeofSpheroid(redius1);//匹配
int redius2=80;
double volume2=VolumeofSpheroid(redius2);//经过隐式转换匹配
string radius3="100";
double volume2=VolumeofSpheroid(redius3);//不匹配,需要显式转换
zy22
函数递归
static int Fac(int n){
if(n<=1){
return 1;
}
else{
return nFac(n-1);//递归关系
}
}
斐波那契数列
static int Fibonacci(int n){
if((n==1)||(n==2)){
return 1;
}
else{
return Fibonacci(n-1)+Fibonacci(n-2);
}
}
Main()
for(int i=1;i<=n;i++){
Console.Write("{0}\t",Fibonacci(i));
if(i%5==0){
Console.WriteLine();
}
}
zy23
局部变量
函数内的变量
static void A()
{
int x=10;
Console.WriteLine("The value x in A() is {0}",x);
}
static void B(){
int x=10;
Console.WriteLine("The value x in B() is {0}",x);
}
Main()
int x=30;
A();
B();
Console.WriteLine("The value x in Main() is {0}",x);
zy24
类
封装
接口
对象
zy25
定义类和创建对象
新建cat.cs类
class Cat
{
public string name;//字段
public int age;
private int miceCount=0;
private void Hello(){//方法
Console.WriteLine("嗨,我是"+name);
}
public void Meow()
{
Hello();
Console.WriteLine("喵喵喵~~~");
}
public void ChaseMice()
{
miceCount++;
Hello();
Console.WriteLine("我已经捉住了{0}只老鼠",miceCount);
}
}
实例化
Cat Kitty=new Cat();
Kitty.name="Kitty";
Kitty.age=3;
Console.WriteLine(Kitty.name);
Console.WriteLine(Kitty.age);
Kitty.Meow();
Kitty.ChaseMice();
Cat er=new Cat();
er.name="哆啦A梦";
er.age=10;
Console.WriteLine(er.name);
Console.WriteLine(er.age);
er.Meow();
er.ChaseMice();
zy26
属性
class Cat
{
public string name;//字段
private int age;
/*
public void SetAge(int ageValue)
{
if(int ageValue<0)
{age=0;}
else
{age=ageValue;}
}
public int GetAge()
{
return age;
}
//访问器
*/
public int Age
{
get
{
return age;
}
set{
if(value<=0){
age=0;
}
else
{
age=value;
}
}
}
private int miceCount=0;
private void Hello(){//方法
Console.WriteLine("嗨,我是"+name);
}
public void Meow()
{
Hello();
Console.WriteLine("喵喵喵~~~");
}
public void ChaseMice()
{
miceCount++;
Hello();
Console.WriteLine("我已经捉住了{0}只老鼠",miceCount);
}
}
Cat Kitty=new Cat();//构造函数
Kitty.name="Kitty";
Kitty.Age=3;
//Kitty.SetAge(3);
Console.WriteLine(Kitty.name);
//Console.WriteLine(Kitty.GetAge());
Console.WriteLine(Kitty.Age);
Kitty.Meow();
Kitty.ChaseMice();
带参数构造函数
class Cat
{
public string name;//字段
public int age;
private int miceCount=0;
//有参数的构造函数
public Cat(string nameValue,int ageValue)
{
name=nameValue;
age=ageValue;
}
public Cat()
{
name="";
age=0;
}
private void Hello(){//方法
Console.WriteLine("嗨,我是"+name);
}
public void Meow()
{
Hello();
Console.WriteLine("喵喵喵~~~");
}
public void ChaseMice()
{
miceCount++;
Hello();
Console.WriteLine("我已经捉住了{0}只老鼠",miceCount);
}
}
Cat Kitty=new Cat("Kitty",3);//构造函数
Console.WriteLine(Kitty.name);
Console.WriteLine(Kitty.age);
Kitty.Meow();
Kitty.ChaseMice();
zy27
析构函数垃圾回收以及类的实例
class Cat
{
private string name;
private int age;
public Cat(string nameValue,int ageValue){
name=nameValue;
age=ageValue;
Console.WriteLine("一只猫诞生了");
}
~Cat(){//析构函数
Consle.WriteLine("一只猫去了天堂");
}
public void ZL()
{
Console.WriteLine("我的姓名是"+name);
Console.WriteLine("我的年龄是"+age);
}
}
Main()
Cat kit=new Cat("kitty",4);
kit.ZL();
Console.WriteLine("-------------------------------");
Console.WriteLine("######################");
非托管程序
System.Gc.Collect(),强制执行垃圾回收机制
class Time
{
private int hour;
private int minute;
private int second;
private void SetTime(int h,int m,int s)
{
//属性值默认大写
Hour=h;
Minute=m;
Second=s;
}
public Time()
{
SetTime(0,0,0);
}
public Time(int hourValue)
{
SetTime(hourValue,0,0);
}
public Time(int hourValue,int minuteValue)
{
SetTime(hourValue,minuteValue,0);
}
public Time(int hourValue,int minuteValue,int secondValue)
{
SetTime(hourValue,minuteValue,secondValue);
}
public int Hour
{
get{return hour;}
set{hour=(value>=0&&value<=24)?value:0;}
}
public int Minute
{
get{return minute;}
set{minute=(value>=0&&value<=60)?value:0;}
}
public int Second
{
get{return second;}
set{second=(value>=0&&value<=60)?value:0;}
}
public string ToString24()
{
string output=Hour+":"+Minute+":"+Second;
return output;
}
public string ToString12()
{
int hourTemp=(Hour==12||Hour==0)?12:(Hour%12);
string AMPM=(Hour<12?"AM":"PM");
string output=hourTemp+":"+Minute+":"+Second+AMPM;
return output;
}
}
Main()
Time time1=new Time();
Time time2=new Time(22);
Time time3=new Time(22,36,30);
Console.WriteLine("time1 12小时制:"+time1.ToString12());
Console.WriteLine("time1 24小时制:"+time1.ToString24());
Console.WriteLine("time2 12小时制:"+time2.ToString12());
Console.WriteLine("time2 24小时制:"+time2.ToString24());
Console.WriteLine("time3 12小时制:"+time3.ToString12());
Console.WriteLine("time3 24小时制:"+time3.ToString24());
网友评论