• 基于类的封装[基础]



    基于类的封装
    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace Con_1
    {
        
    class Program
        {
            
    static void Main(string[] args)
            {
                Person per1 
    = new Person();
                
    //per1.Name = "刘德华";
                string n1 = per1.Name;   // F11调试get和set
                
    //per1.Name = "张学友";  // 姓名应该只读

                
    //Person per2 = new Person("刘德华");  //通过构造方法传值
                
    //try
                
    //{
                
    //    string n2 = per2.Name;
                
    //    Console.WriteLine(n2);
                
    //    per2.Age = 500;         //验证姓名是否合法
                
    //}
                
    //catch (Exception ex)
                
    //{
                
    //    Console.WriteLine(ex.Message);
                
    //}
                Person per3 = new Person("汪涵");
                per3.Birthday 
    = DateTime.Parse("1972/2/12");
                
    int i_age = per3.Age;  //根据生日得到年龄
                Console.WriteLine(i_age);
                
    // 调用类的静态方法
                Person.Eating();
            }
        }
        
    class Person
        {
            
    // 所以的字段都必须私有化 private
            
    // 私有字段首字母不必大些 name 或 _name
            private string name;
            
    // 属性  public成员必须首字母大写 Name
            public string Name
            {
                
    // 属性作用一:读写设置
                
    //只有get访问器,则该属性只读
                get // 获得,读取
                {
                    
    return name;
                }
                
    //只有set访问器,则该属性只写
                
    //set // 设置,更改
                
    //{
                
    //    name = value;
                
    //}
            }
            
    //private int age;
            
    // 属性作用二:验证输入数据的合法性和正确性
            
    //public int Age
            
    //{
            
    //    get { return age; }
            
    //    set 
            
    //    { 
            
    //        //age = value; 
            
    //        if (value >= 0 && value <= 150)
            
    //        {
            
    //            age = value;
            
    //        }
            
    //        else throw new Exception("年龄输入不正确");
            
    //    }
            
    //}
            private DateTime age;

            
    public DateTime Birthday
            {
                
    //get { return age; }
                set { age = value; }
            }
            
    // 属性作用三:隐藏细节(通过输入生日的日期 Birthday 获得年龄 Age)
            public int Age
            {
                
    get 
                { 
                    
    //return age; 
                    TimeSpan ts = DateTime.Now - age;
                    
    return (int)(ts.TotalDays) / 365;
                }
                
    //set
                
    //{
                
    //    //age = value; 
                
    //    if (value >= 0 && value <= 150)
                
    //    {
                
    //        age = DateTime.Now.AddYears(-value);
                
    //    }
                
    //    else throw new Exception("年龄输入不正确");
                
    //}
            }
            
    // 构造方法
            public Person()
            { }
            
    // 构造方法重载
            public Person(string n)
            {
                
    this.name = n;
            }
            
    //public Person(string n, int a)
            
    //{
            
    //    this.name = n;
            
    //    //this.age = a;
            
    //}
            
    // 普通方法
            public string Hi()
            {
                Console.WriteLine(
    "你好");
                
    return name;
            }
            
    // 重载:方法名相同,参数列表不同。(与返回类型无关)
            
    // 参数列表不同点: 1 个数不同
            
    //                 2 类型不同
            
    //                 3 顺序不同
            public string Hi(int i,string n)
            {
                
    return name;
            }
            
    public string Hi(string n,int i)
            {
                
    return name;
            }
            
    public string Hi(string n, int i,bool b)
            {
                
    return name;
            }
            
    // 错误:已定义了一个名为“Hi”的具有相同参数类型的成员
            
    // 方法的重载与返回类型无关!
            
    //public int Hi(string n, int i, bool b)
            
    //{
            
    //    return Age;
            
    //}

            
    // 静态方法
            public static void Eating()
            {
                Console.WriteLine(
    "吃饭了。。。");
            }
        }
    }


  • 相关阅读:
    Quick Union
    Quick Find (QF)
    ubuntu kylin18 安装NVIDIA驱动
    vim 快捷键(update)
    Qt中的ui指针和this指针
    两种状态机扫描按键,第二种只要三行!!!
    RPi:QT+wiringPi demo程序
    esp-12e折腾
    vfd电子时钟制作
    vfd with stm8
  • 原文地址:https://www.cnblogs.com/xugang/p/1684070.html
Copyright © 2020-2023  润新知