设有一个描述坐标点的CPoint类,其私有变量x和y代表一个点的x,y的坐标值。编写程序实现以下功能功能:利用
答案:1 悬赏:0 手机版
解决时间 2021-11-13 05:56
- 提问者网友:轻浮
- 2021-11-12 08:24
设有一个描述坐标点的CPoint类,其私有变量x和y代表一个点的x,y的坐标值。编写程序实现以下功能功能:利用
最佳答案
- 五星知识达人网友:梦中风几里
- 2021-11-12 09:31
⑴用构造方法的重载版本:
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;
}
}
运行结果:
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯