设有一个描述坐标点的CPoint类,其私有变量x和y代表一个点的x,y的坐标值。编写程序实现以下功能功能:利用 20
1个回答
展开全部
⑴用构造方法的重载版本:
using System;
using System.Collections.Generic;
public class MyClass
{
public static void Main()
{
CPoint cp=new CPoint();
cp.Display();
cp.SetPoint(80,150);
cp.Display();
Console.ReadLine();
}
}
public class CPoint
{
private int x;
private int y;
public CPoint():this(60,75)
{
}
public CPoint(int x,int y)
{
this.x=x;
this.y=y;
}
public void Display()
{
Console.WriteLine("x={0},y={1}",x,y);
}
public void SetPoint(int x,int y)
{
this.x=x;
this.y=y;
}
}
⑵直接设置默认值的版本:
using System;
using System.Collections.Generic;
public class MyClass
{
public static void Main()
{
CPoint cp=new CPoint();
cp.Display();
cp.SetPoint(80,150);
cp.Display();
Console.ReadLine();
}
}
public class CPoint
{
private int x=60;
private int y=75;
public CPoint()//这回不需要了:this(60,75)
{
}
public CPoint(int x,int y)
{
this.x=x;
this.y=y;
}
public void Display()
{
Console.WriteLine("x={0},y={1}",x,y);
}
public void SetPoint(int x,int y)
{
this.x=x;
this.y=y;
}
}
运行结果:
using System;
using System.Collections.Generic;
public class MyClass
{
public static void Main()
{
CPoint cp=new CPoint();
cp.Display();
cp.SetPoint(80,150);
cp.Display();
Console.ReadLine();
}
}
public class CPoint
{
private int x;
private int y;
public CPoint():this(60,75)
{
}
public CPoint(int x,int y)
{
this.x=x;
this.y=y;
}
public void Display()
{
Console.WriteLine("x={0},y={1}",x,y);
}
public void SetPoint(int x,int y)
{
this.x=x;
this.y=y;
}
}
⑵直接设置默认值的版本:
using System;
using System.Collections.Generic;
public class MyClass
{
public static void Main()
{
CPoint cp=new CPoint();
cp.Display();
cp.SetPoint(80,150);
cp.Display();
Console.ReadLine();
}
}
public class CPoint
{
private int x=60;
private int y=75;
public CPoint()//这回不需要了:this(60,75)
{
}
public CPoint(int x,int y)
{
this.x=x;
this.y=y;
}
public void Display()
{
Console.WriteLine("x={0},y={1}",x,y);
}
public void SetPoint(int x,int y)
{
this.x=x;
this.y=y;
}
}
运行结果:
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询