c# 从零到精通-封装一个类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test05
{
///
/// 自定义类,封装加数和被加数属性
///
class MyClass
{
private int x = 0; //定义int型变量,作为加数
private int y = 0; //定义int型变量,作为被加数
///
/// 加数
///
public int X
{
get
{
return x;
}
set
{
x = value;
}
}
///
/// 被加数
///
public int Y
{
get
{
return y;
}
set
{
y = value;
}
}
///
/// 求和
///
/// 加法运算和
public int Add()
{
return X + Y;
}
}
class Program
{
static void Main(string[] args)
{
MyClass myclass = new MyClass(); //实例化MyClass的对象
myclass.X = 3; //为MyClass类中的属性赋值
myclass.Y = 5; //为MyClass类中的属性赋值
Console.WriteLine(myclass.Add()); //调用MyClass类中的Add方法求和
Console.ReadLine();
}
}文章来源:https://www.toymoban.com/news/detail-481191.html
}文章来源地址https://www.toymoban.com/news/detail-481191.html
到了这里,关于c# 从零到精通-封装一个类的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!