123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace encapsulation
- {
- //自定义类,封装加数和被加数属性
- class MyClass
- {
- private int x = 0;
- private int y = 0;
- //加数
- public int X {
- get {
- return x;
- }
- set {
- x = value;
- }
- }
- //被加数
- public int Y {
- get {
- return y;
- } set {
- y = value;
- }
- }
- //求和
- public int Add()
- {
- return X + Y;
- }
- }
- internal 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();
- }
- }
- }
|