12.30 课堂笔记

C#中的访问修饰符

标识符:代指了 字段 方法名

类的字段封装

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

namespace 类的字段封装
{
    // 人的类
    class People
    {
        // 声明一个字段
       public String name;
       // 声明一个年龄
       public int age = 3;

       // 设置年龄
       public void setAge(int newAge) {

           // 做一个验证
           // 判断newAge参数,如果它是0-150之间,就原样赋值
           // 如果它不在这个范围,那就给一个默认值 18
           if (newAge > 0 && newAge < 150)
           {
               age = newAge;
           }
           else {
               age = 18;
           }
       }

        // 获取年龄
       public int getAge() {

           return age;

       }
    }
}

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

namespace 类的字段封装
{
    class Program
    {
        static void Main(string[] args)
        {
            // 实例化一个具体的人的对象
            People p = new People();
            
            int a = p.getAge();

            Console.WriteLine("获取的年龄是" + a);

            // 设置私有的age的值
            p.setAge(999);

            // 获取私有的age的值
            int b = p.getAge();

            Console.WriteLine("修改后 获取到的年龄是" + b);

            Console.ReadKey();    
        }
    }
}

关于this关键词

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

namespace 关于this关键词
{
    class hero
    {
        public String name="heroname";

        // 起名字的方法
        // 方法的形参名称与字段一模一样
        public void setName(String name) {
            // 输出name的值
            Console.WriteLine(name);
            // this关键词,这个的姓名,this就是对象本身,理解为我的.
            Console.WriteLine(this.name);
            // 理解为 对象.标识符/对象,字段


        }
    }
}

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

namespace 关于this关键词
{
    class Program
    {
        static void Main(string[] args)
        {
            hero h = new hero();
            h.setName("金刚狼");

            // 卡顿
            Console.ReadKey();
        }
    }
}

读写属性

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

namespace 读写属性
{
    class People
    {

        // 定义了一个字段
        private int age = 3;

        // 定义了一个属性
        // 如果属性同时具有get与set方法,代表该属性具有读写权限
        public int Age
        {
            // get访问器
            // 对象.Age 会触发代码
            // 取值时触发
            // 负责读权限
            get {
                Console.WriteLine("get访问器生效了");
                return age; 
            }

            // set访问器
            // 对象,age = 值 会触发代码
            // p.Age = 1000;
            // 赋值时触发
            // 负责写权限
            set {
                Console.WriteLine("set访问器生效了");

                // 对年龄进行验证
                if (value > 0 && value < 150)
                {
                    age = value;
                }
                else {
                    age = 18;
                }
            
            }
        }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 读写属性
{
    class Program
    {
        static void Main(string[] args)
        {
            People p = new People();

            int res = p.Age;

            Console.WriteLine(res);

            p.Age = 180;
            Console.WriteLine(p.Age);

            Console.ReadKey();
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值