公共,私有,保护,与虚无之间的区别是什么? [英] What is the difference between Public, Private, Protected, and Nothing?

查看:341
本文介绍了公共,私有,保护,与虚无之间的区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所有的大学里,我一直在使用公开,并想知道的区别公开私人保护

还有哪些呢静态做的,而不是有什么?

解决方案

访问修饰符

  

公开

     
    

该类型或成员可以通过任何其他code在同一组件或引用它的另一个组件进行访问。

  
     

私人

     
    

该类型或成员只能由code在同一类或结构进行访问。

  
     

保护

     
    

该类型或成员只能由code在同一类或结构访问,或在派生类中。

  
     

内部

     
    

该类型或成员可以通过任何code在同一个组件进行访问,而不是从另一个程序集。

  
     

受保护的内部

     
    

该类型或成员可以通过任何code在同一个组件进行访问,或在另一个程序集的任何派生类。

  

没有访问修饰符设置,默认的访问修饰符。所以总有某种形式的访问修饰符,即使它没有设置。

静态

  

在一个类中的static修饰符意味着该类不能被实例化,所有成员都是静态的。静态成员有一个版本,无论其封闭类型的多少实例被创建。

     

一个静态类是基本相同的非静态类,但有一个区别:静态类不能从外部实例化。换句话说,你不能使用new关键字创建类类型的变量。因为没有实例变量,可以使用类名称本身访问静态类的成员。

     

不过,有一个这样的东西静态构造函数。任何类可以有其中的一个,包括静态类。它们不能直接与安培调用;不能有参数(不是在类本身的任何类型的参数等)。静态构造函数被自动调用初始化类的第一个实例创建或任何静态成员被引用之前。看起来是这样的:

 静态类Foo()
{
    静态的Foo()
    {
        酒吧=FUBAR;
    }

    公共静态字符串吧{获得;组; }
}
 

静态类常被用作服务,您可以使用它们像这样:

  MyStaticClass.ServiceMethod(...);
 

All my college years I have been using public, and would like to know the difference between public, private, and protected?

Also what does static do as opposed to having nothing?

解决方案

Access modifiers

public

The type or member can be accessed by any other code in the same assembly or another assembly that references it.

private

The type or member can only be accessed by code in the same class or struct.

protected

The type or member can only be accessed by code in the same class or struct, or in a derived class.

internal

The type or member can be accessed by any code in the same assembly, but not from another assembly.

protected internal

The type or member can be accessed by any code in the same assembly, or by any derived class in another assembly.

When no access modifier is set, a default access modifier is used. So there is always some form of access modifier even if it's not set.

Static

The static modifier on a class means that the class cannot be instantiated, and that all of its members are static. A static member has one version regardless of how many instances of its enclosing type are created.

A static class is basically the same as a non-static class, but there is one difference: a static class cannot be externally instantiated. In other words, you cannot use the new keyword to create a variable of the class type. Because there is no instance variable, you access the members of a static class by using the class name itself.

However, there is a such thing as a static constructor. Any class can have one of these, including static classes. They cannot be called directly & cannot have parameters (other than any type parameters on the class itself). A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced. Looks like this:

static class Foo()
{
    static Foo()
    {
        Bar = "fubar";
    }

    public static string Bar { get; set; }
}

Static classes are often used as services, you can use them like so:

MyStaticClass.ServiceMethod(...);

这篇关于公共,私有,保护,与虚无之间的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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