结构体
来源:互联网 发布:淘宝买家被骗案例 编辑:程序博客网 时间:2024/04/30 15:55
using System;
using System.Collections.Generic;
using System.Text;
namespace struct02
...{
public struct CoOrds
...{
public int x, y;
public CoOrds(int p1, int p2)
...{
x = p1;
y = p2;
}
}
class Program
...{
static void Main(string[] args)
...{
CoOrds coords1;
// 赋值
coords1.x = 10;
coords1.y = 20;
Console.WriteLine(coords1.ToString() + " ");
//显示:
System.Console.Write("CoOrds 1: ");
System.Console.WriteLine("x = {0}, y = {1}", coords1.x, coords1.y);
}
}
}
using System.Collections.Generic;
using System.Text;
namespace struct02
...{
public struct CoOrds
...{
public int x, y;
public CoOrds(int p1, int p2)
...{
x = p1;
y = p2;
}
}
class Program
...{
static void Main(string[] args)
...{
CoOrds coords1;
// 赋值
coords1.x = 10;
coords1.y = 20;
Console.WriteLine(coords1.ToString() + " ");
//显示:
System.Console.Write("CoOrds 1: ");
System.Console.WriteLine("x = {0}, y = {1}", coords1.x, coords1.y);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace strctcopy
...{
//定义一个坐标系
struct Point
...{
public int X;
public int Y;
public Point(int x, int y)
...{
X = x;
Y = y;
}
}
//定义一个长方形
struct Rect
...{
public int top, bottom, left, right;
public Rect(Point topLeft, Point bottomRight)
...{
top = topLeft.Y;
left = topLeft.X;
bottom = bottomRight.Y;
right = bottomRight.X;
}
public int Area()
...{
return(bottom-top)*(right-left);
}
}
class Program
...{
static void Main(string[] args)
...{
//演示复制结构变量操作
Console.WriteLine(" 复制结构变量的实例: ");
//使用new关键字声明结构变量
Point pt = new Point(1,2);
Point xy = new Point(3,4);
//复制结构变量
Console.WriteLine("当前pt结构变量的值是:" + "(" + "{0}" + "," + "{1}" + ")", pt.X, pt.Y);
pt = xy;
Console.WriteLine("复制变量后,pt的值是:" + "(" + "{0}" + "," +"{1}" + ")", pt.X,pt.Y);
Console.WriteLine(" ===================================================================== ");
//不使用new声明结构变量
Rect rect01, rect02;
//对rect01结构变量赋值,构建一个长为3,宽为2的长方形
rect01.top = 5;
rect01.left = 2;
rect01.bottom = 3;
rect01.right = 5;
/**/////对rect02结构变量赋值,构建一个长为6,宽为4的长方形
rect02.top = 8;
rect02.left = 2;
rect02.bottom = 4;
rect02.right = 8;
//复制结构变量
rect01 = rect02;
Console.WriteLine("rect01现在的值是:");
Console.WriteLine("top = {0}",rect01.top);
Console.WriteLine("left = {0}",rect01.left);
Console.WriteLine("bottom = {0}",rect01.bottom);
Console.WriteLine("right = {0}",rect01.right);
}
}
}
using System.Collections.Generic;
using System.Text;
namespace strctcopy
...{
//定义一个坐标系
struct Point
...{
public int X;
public int Y;
public Point(int x, int y)
...{
X = x;
Y = y;
}
}
//定义一个长方形
struct Rect
...{
public int top, bottom, left, right;
public Rect(Point topLeft, Point bottomRight)
...{
top = topLeft.Y;
left = topLeft.X;
bottom = bottomRight.Y;
right = bottomRight.X;
}
public int Area()
...{
return(bottom-top)*(right-left);
}
}
class Program
...{
static void Main(string[] args)
...{
//演示复制结构变量操作
Console.WriteLine(" 复制结构变量的实例: ");
//使用new关键字声明结构变量
Point pt = new Point(1,2);
Point xy = new Point(3,4);
//复制结构变量
Console.WriteLine("当前pt结构变量的值是:" + "(" + "{0}" + "," + "{1}" + ")", pt.X, pt.Y);
pt = xy;
Console.WriteLine("复制变量后,pt的值是:" + "(" + "{0}" + "," +"{1}" + ")", pt.X,pt.Y);
Console.WriteLine(" ===================================================================== ");
//不使用new声明结构变量
Rect rect01, rect02;
//对rect01结构变量赋值,构建一个长为3,宽为2的长方形
rect01.top = 5;
rect01.left = 2;
rect01.bottom = 3;
rect01.right = 5;
/**/////对rect02结构变量赋值,构建一个长为6,宽为4的长方形
rect02.top = 8;
rect02.left = 2;
rect02.bottom = 4;
rect02.right = 8;
//复制结构变量
rect01 = rect02;
Console.WriteLine("rect01现在的值是:");
Console.WriteLine("top = {0}",rect01.top);
Console.WriteLine("left = {0}",rect01.left);
Console.WriteLine("bottom = {0}",rect01.bottom);
Console.WriteLine("right = {0}",rect01.right);
}
}
}
复制结构变量的实例:
当前pt结构变量的值是:(1,2)
复制变量后,pt的值是:(3,4)
=====================================================================
rect01现在的值是:
top = 8
left = 2
bottom = 4
right = 8
请按任意键继续. . .