c# 從零到精通-封裝一個類
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test05
{
///
/// 自定義類,封裝加數(shù)和被加數(shù)屬性
///
class MyClass
{
private int x = 0; //定義int型變量,作為加數(shù)
private int y = 0; //定義int型變量,作為被加數(shù)
///
/// 加數(shù)
///
public int X
{
get
{
return x;
}
set
{
x = value;
}
}
///
/// 被加數(shù)
///
public int Y
{
get
{
return y;
}
set
{
y = value;
}
}
///
/// 求和
///
/// 加法運(yùn)算和
public int Add()
{
return X + Y;
}
}
class Program
{
static void Main(string[] args)
{
MyClass myclass = new MyClass(); //實(shí)例化MyClass的對象
myclass.X = 3; //為MyClass類中的屬性賦值
myclass.Y = 5; //為MyClass類中的屬性賦值
Console.WriteLine(myclass.Add()); //調(diào)用MyClass類中的Add方法求和
Console.ReadLine();
}
}文章來源:http://www.zghlxwxcb.cn/news/detail-481191.html
}文章來源地址http://www.zghlxwxcb.cn/news/detail-481191.html
到了這里,關(guān)于c# 從零到精通-封裝一個類的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!