C#语言关于类的继承和访问中public、internal、private、protected的访问级别和实例

本文详细解析了 C# 中的访问修饰符,包括 public、internal、private 和 protected 的使用场景及权限范围,通过具体示例展示了不同修饰符在类继承中的表现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public :全部公开,在本程序集,其他程序集(Assembly)和继承类中均可访问。

//本程序集
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyLib
{
    public class Vehicle
    {
        public String Owner { get; set; }
      

    }
    public class Car:Vehicle
    {
       public void ShowOwner()
        {
            Console.WriteLine(this.Owner);   //继承类、本程序集中使用public 变量
        }

    }
}

//另一程序集

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

namespace HelloOOP
{
    class Program
    {
        static void Main(string[] args)
        {
            
            Vehicle vehicle = new Vehicle();
            vehicle.Owner = "Timothy";       //在另一程序集使用

        }
    }

internal :默认访问级别,internal修饰的变量只可以在本程序集中使用,包括本程序集的继承类

//本程序集
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyLib
{
    public class Vehicle
    {
        internal String Owner { get; set; }
      

    }
    public class Car:Vehicle
    {
       public void ShowOwner()
        {
            Console.WriteLine(this.Owner);   //继承类、本程序集中使用public 变量
        }

    }
}

在另一程序集引用则会报错

 private:只可以在本程序段中使用,即在此大括号中。不能外部访问问,但是会继承在子类中,仅仅是没法通过点操作符直接访问罢了。

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

namespace MyLib
{
    public class Vehicle
    {
        private  String Owner { get; set; }

    }
    public class Car:Vehicle
    {
       public void ShowOwner()
        {
            Console.WriteLine(this.Owner);
        }

    }
}

此时已经会报错:

 protected:保护某一个方法或属性,使其不能直接作为属性暴露,但是创建他的子类时可以使用,范围可以是本程序集也可以是另一程序集,如

//本程序集
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyLib
{
    public class Vehicle
    {
        public  String Owner { get; set; }

        protected int _rpm;    //protected 多用在方法上,可以和internal组合
        private int _fuel;
        public void Refuel()
        {
            _fuel = 100;
        }
        protected void Burn(int fuel)
        {
            _fuel-=fuel;
        }
        public void Accelerate()
        {
            _rpm += 1000;
            Burn(1);
        }

        public int MySpeed
        {
            get { return _rpm / 100; }//只读
           
        }

    }
    public class Car:Vehicle
    {
       public void ShowOwner()
        {
            Console.WriteLine(this.Owner);
        }
        public void TurboAccelerate()
        {
            Burn(2);
            _rpm += 3000;      //protected 修饰的变量在本程序集中的子类使用
        }
    }
}
//另一程序集
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MyLib;

namespace HelloOOP
{
    class Program
    {
        static void Main(string[] args)
        {

            Vehicle vehicle = new Vehicle();
            vehicle.Owner = "Timothy";
            vehicle.Accelerate();
            vehicle.Accelerate();
            Console.WriteLine(vehicle.MySpeed);
            Car car = new Car();
            car.TurboAccelerate();
            Console.WriteLine(car.MySpeed);
            Bus bus = new Bus();
            bus.SlowAccelerate();
            Console.WriteLine(bus.MySpeed);
        }
    }
    class Bus : Vehicle
    {
        public void SlowAccelerate()
        {
            Burn(1);
            _rpm += 500;        //protected 修饰的变量在另一程序集中的子类使用
        }
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值