为什么我的课程在 Visual Studio 中默认是私有的? [英] Why are my classes private by default in Visual Studio?

查看:25
本文介绍了为什么我的课程在 Visual Studio 中默认是私有的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WIen 我创建了一个新的类文件,Visual Studio 默认不公开它.我可以改变这个吗?

WIen I create a new class file, Visual Studio does not make it public by default. Can I change this?

推荐答案

默认情况下,没有访问说明符的类是内部类,成员默认为私有.这样可以尽可能限制可见性,从而增加封装性.

By default a class without an access specifier is internal and a member defaults to private. This it keeps the visibility as restricted as possible and thereby increases encapsulation.

不加思索地公开一个新类打破了封装的整个概念.

Making a new class public without even thinking about breaks the entire idea of encapsulation.

VS 用来创建新类的类模板可以在这个 zip 文件中找到(在 CSharp 的情况下):

The class template that VS uses to create a new class can be found in this zip file (in case of CSharp):

C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\ItemTemplates\CSharp\1033\Class.zip 或C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033 使用 VS Orcas 时

C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\ItemTemplates\CSharp\1033\Class.zip or C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033 when using VS Orcas

打开 zip 文件,添加 public 关键字,保存它就大功告成了.每次添加类时,它都会是公开的.希望这会有所帮助.

Open the zip file, add the public keyword, save it and you're done. Every time you add a class it will be public. Hope this helps.

这篇关于为什么我的课程在 Visual Studio 中默认是私有的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆