using System;
namespace StructAndClass
{
struct SPoint
{
public int x, y;
public SPoint(int x, int y)
{
this.x = x;
this.y = y;
}
}
class CPoint
{
public int x, y;
public CPoint(int x, int y)
{
this.x = x;
this.y = y;
}
}
class Test
{
public static void Main()
{
SPoint sp1 = new SPoint(2, 5);
Console.WriteLine("结构/sp1初始值:");
Console.WriteLine("sp1.x={0}", sp1.x);
SPoint sp2 = sp1;
Console.WriteLine("sp1=sp2后:");
Console.WriteLine("sp1.x={0}");
Console.WriteLine("sp1.x={0}", sp1.x);
Console.WriteLine("sp2.x={0}", sp2.x);
sp1.x = 5;
Console.WriteLine("再次改变sp1的值后:");
Console.WriteLine("sp1.x={0}", sp1.x);
Console.WriteLine("sp2.x={0}", sp2.x);
Console.WriteLine("============================");
CPoint cp1 = new CPoint(2,5);
Console.WriteLine("类/cp1初始值:");
Console.WriteLine("cp1.x={0}", cp1.x);
CPoint cp2 = cp1;
Console.WriteLine("cp1=cp2后:");
Console.WriteLine("cp1.x={0}", cp1.x);
Console.WriteLine("cp2.x={0}", cp2.x);
cp1.x = 5;
Console.WriteLine("再次改变cp1的值后:");
Console.WriteLine("cp1.x={0}", cp1.x);
Console.WriteLine("cp2.x={0}", cp2.x);
Console.ReadKey();
}
}
}
struct DC
{
public int x, y;
public int X
{
set
{
x = value;
}
get
{
return x;
}
}
public int Y
{
set
{
y = value;
}
get
{
return y;
}
}
public DC(int x,int y)
{
this.x = x;
this.y = y;
}
}
struct RDC
{
public int x, y;
public int X
{
set
{
x = value;
}
get
{
return x;
}
}
public int Y
{
set
{
y = value;
}
get
{
return y;
}
}
public RDC(int x, int y)
{
this.x = x;
this.y = y;
}
}
class Test
{
public static void Main()
{
DC dc=new DC();
dc.x = 3;
dc.y = 5;
Console.WriteLine("已经对x,y初始化后:");
Console.WriteLine("此时可以访问和修改dc.X={0}的值",dc.X);
Console.WriteLine("我在这里创建了一个DC的复制结构RDC/n并且不对x,y初始化就会报错");
RDC rdc;
rdc.y = 5;//可以编译通过
rdc.X = 3;//这里就会出错,不能编译通过
Console.WriteLine("=======test over================");
}
}
struct DC
{
public int x, y;
public int X
{
set
{
x = value;
}
get
{
return x;
}
}
public int Y
{
set
{
y = value;
}
get
{
return y;
}
}
public DC(int x,int y)
{
X= x;//这里就是错的
Y = y;//会提示没有给this对象的所有字段赋值
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有