拓展方法、内部类、分部类、密封类

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

namespace _02_拓展方法
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // 扩展方法用来给一个类型的数据添加一个新的方法
            Person p = new Person() { Name = "张三" };
            p.Work();
            p.Say();
            p.Say("你好1234567754");
            int n = 0;
            Console.WriteLine(n.Add(1));
            // 调用扩展方法
            Console.ReadKey();
        
        }
    }
    class Person
    {
        public string Name { get; set; }
        public void Work()
        {
            Console.WriteLine("人类的工作,文明的延续");
        }
    }
    // 扩展方法写在静态类中
    // 静态类中只包含静态成员
    static class Tools
    { 
        // 扩展方法
        /// <summary>
        /// 扩展方法
        /// </summary>
        /// <param name="person">拓展方法的实例</param>
        public  static void Say(this Person person)
        {
            //person 用来接收拓展方法的实例
            Console.WriteLine($"{person.Name} 说 hello");
        }
        // 扩展方法支持重载 
        // 拓展方法的参数写在 拓展方法实例的后面
        public static void Say(this Person person, string message)
        {
            //person 用来接收拓展方法的实例
            Console.WriteLine($"{person.Name} 说 hello");
        }

        // 扩展方法可以内置的类型添加方法
        public static int Add(this int num,int num1)
        {
            return num + num1;
        }
    }


}
/*
 1. 给int 拓展方法
    求x 的 y 次幂的方法
    拓展求数字二进制的结果
 2. 给 double 拓展向下取整的方法 向上取整的方法 四舍五入的方法


 
 
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;

namespace _03_内部类
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // 内部类 就是写在内部的类就叫做内部类
            // 内部类可以让相关的类看起来更像一个整体。
            //People people = new People();
            //People.Head head = people.head;
            //People.Body body = people.body;

            //Console.WriteLine(people.head.Eyes);
            //Console.WriteLine(people.body.Arm);
            // 要获取眼睛先要找到 头部
            Student student = new Student();
           Student.P1ay play = new Student.P1ay();
            Student.Study study = new Student.Study();
            play.P1ay1();
           study.Study1();
            
           

            
            Console.ReadKey();
        }
    }
    // 创建一个人
    class People
    {
        public Head head { get; set; } 

        public Body body { get; set; } 

        Footer footer { get; set; }
        // 这就是一个内部类
        public   class Head
        {
            public string  Eyes { get; set; } 
             public string  Mouth { get; set; }

        }
        // 身体
        public  class Body
        {
            public string  Arm{ get; set; }
        }
        // 脚
        class Footer
        {
            public string Foots { get; set; }
        }
    }

    class Student
    {
        public string Name { get; set; }
        public P1ay p1ay { get; set; }
        public Study study { get; set; }
         public class P1ay
        {
            public void P1ay1()
            {
                Console.WriteLine("正在打游戏");
            }
        }
       public  class Study
        {
            public void Study1()
            {
                Console.WriteLine("正在学习");
            }
        }
    }

}
// 内部类如果想要在类的外部访问,那么内部类必须是公开的
// 私有的内部类只能在所在类的内部使用


/*
 封装一个student类
在student类的内部有 P1ay(玩)类和 study(学习)类
在p1ay 类中有 p1ay 方法 调用输出 正在打游戏
在Study 类中有 study 方法 调用输出 正在学习
 
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _04_分部类
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // 分部类就是把一个类分为几个部分书写就是分部类
            // 普通的类不支持分布的写法 只有分部类支持分布的写法
            //partial 修饰的类叫分部类
            //Partial 修饰的类 类中的内容 只是类的一部分 并不是类的全部内容
            // 分布类的内容是不能重复的 因为各个分部类是同一个类中的内容
            People p = new People() { Name = "张三"};
            Console.WriteLine(p.Name);
            Console.WriteLine(p.Life);
        }
    }
   partial  class People
   {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
   }
  partial  class People
  {
        public string Life { get; set; } = "我是这个人的生活";
  }

}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _05_密封类
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // 密封类 指不能被继承的类
            
            // 密封类不能被继承 但是可以继承其他类
            // 密封类 用来保护类中的数据
        }
    }
     class Father
    {

    }
    class Son : Father
    {

    }
    //sealed class Grandson : Son
    //{

    //}


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值