2C#面向对象

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _2023_0625_1032面向对象
{
    class Program
    {
        #region 面向对象
        //    static void Main(string[] args)
        //    {
        //        //Man zhangsan = new Man();
        //        //zhangsan.Go();
        //        zhangsan.Go2();//这时错误的
        //        //Man.Go2();//静态方法归该类所有

        //        Woman woman = new Woman();//错误的
        //        //Woman.Go("小丽");
        //        int a = 10;
        //        int b = 20;
        //        //Woman.Swap(ref a, ref b);//ref 按引用传递参数
        //        //Console.WriteLine("a " + a);
        //        //Console.WriteLine("b " + b);
        //        Woman.GetValue(out a);
        //        Console.WriteLine("a " + a);//按输出传递参数
        //    }
        //    #region 静态方法
        //    //方法定义
        //    #endregion
        //}
        //class Man
        //{
        //    public void Go()
        //    {
        //        Console.WriteLine("张三跑");
        //    }
        //    public static void Go2()
        //    {
        //        Console.WriteLine("张三跑22222");
        //    }


        //    int Go3()
        //    {
        //        return 1;
        //    }
        //}
        //static class Woman//静态类无法实例
        //{
        //    public static void GetValue(out int x)
        //    {
        //        int temp=5;
        //        x = temp;
        //    }
        //    public static void Go(string name)
        //    {
        //        Console.WriteLine(name + "在跑步");
        //    }
        //    public static void Swap(ref int x,ref int y)
        //    {
        //        //int temp;
        //        //temp = x;
        //        //x = y;
        //        //y = temp;
        //        //例子2
        //        x = 1;
        //        y = 2;
        //    }
        //}


        //OOP面向对象
        #region ref:传递引用(输入传递)
        //static void Main(string[] args)
        //{
        //    int a = 3;
        //    int b = 4;
        //    Swap(ref a, ref b);
        //    Console.WriteLine("a:" + a);
        //    Console.WriteLine("b:" + b);
        //    Console.ReadLine();
        //}
        //public static void Swap(ref int x, ref int y)
        //{
        //    int temp;
        //    temp = x;
        //    x = y;
        //    y = temp;
        //}
        #endregion
        //static void Main(string[] args)
        //{
        //    int n = 10;
        //    int n2 = 20;
        //    GetValue(out n, out n2);
        //    Console.WriteLine(n + " " + n2);
        //}
        return只能返回一个值,out可以返回多个值
        //public static void GetValue(out int x, out int x2)
        //{
        //    int temp = 5;
        //    x = temp;
        //    x2 = temp;
        //}
        //#endregion
        #region out: 按输出传递参数
        //static void Main(string[] args)
        //{
        //    int n=10;
        //    Program p = new Program();
        //    p.GetValue(out n);
        //    Program p1 = new Program();
        //    Program p2 = new Program();
        //    Console.WriteLine(n);
        //}
        //public void GetValue(out int x)
        //{
        //    int temp = 5;
        //    x = temp;
        //}
        #endregion
        #region 函数参数默认值
        //static void Main(string[] args)
        //{
        //    MySum(15);
        //    MySum(10, 20);
        //}
        //public static void MySum(int a, int b = 10)
        //{
        //    Console.WriteLine("a={0},b={1}", a, b);
        //}
        #endregion
        #region 函数递归调用
        #region 第一题
        // static void Main(string[] args)
        //{
        //    Console.WriteLine(My(8));
        //    Console.WriteLine(My(30));
        //}
        1、1、2、3、5、8、13、21、34...... 求第30位数是多少, 用递归算法实现。
        ///* f(1)=1;
        // * f(2)=1
        // * f(3)=f(2)+f(1);
        // * f(4)=f(3)+f(2);
        // * f(30)=f(29)+f(28);
        // */
        //static int  My(int n)
        //{
        //    if (n == 1)
        //        return 1;
        //    if (n == 2)
        //        return 1;
        //    return My(n - 1) + My(n - 2);
        //}
        #endregion
        //猴子摘了好多好多桃子,一天需要吃掉总数的2/3,
        //觉得不过瘾,还得再多吃一个
        //吃到第9天的时候,发现只有2个桃子了
        //问,当初猴子摘了多少桃子
        #endregion
        #region 求阶乘
        !1=1
        !2=1*2;
        !3=1*2*3;
        !4=1*2*3*4;
        !10
        //static void My(int n)
        //{
        //    int temp = 1;
        //    for (int i = 1; i < n; i++)
        //    {
        //        temp = temp * i;
        //    }
        //    Console.WriteLine(temp);
        //}
        #endregion
        #endregion
        static void Main(string[] args)
        {
            //Man.name = "101010";
            //Man.Money = 99999;
            //Console.WriteLine(Man.Money);
            //Man.Money2 = "9999";
            //Console.WriteLine(Man.Money2);

            Woman woman = new Woman();
            Woman woman2 = new Woman("88888");
        }
    }
    static class Man
    {
        public static string name;//字段
        private static int money;//字段
        //属性
        public static int Money
        {
            set
            {
                money = value;
            }
            get
            {
                return money;
            }
        }
        public static string Money2//简写方式
        {
            get;
            set;
        }
    }
    class Woman
    {
        public string name;
        //构造方法
        public Woman()
        {
            Console.WriteLine("Woman");
        }
        public Woman(string str)
        {
            name = str;
            Console.WriteLine("Woman999999"+ str);
        }
        //析构方法
        ~Woman()
        {
            Console.WriteLine("析构方法调用");
        }

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值