静态类的真实生活意义 [英] static class real life significance

查看:134
本文介绍了静态类的真实生活意义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了很多关于static关键字,我只找到静态变量,静态函数,但没有讨论静态类,可以请解释一下这个。

I read a lot on static keyword, I only found static variable, static function, but there is no discussion of static class, can you please explain me about this.


  • 为什么我们在c ++中使用静态类?

  • 为什么要引入这种类型的类?

  • 静态类的意义?

  • 给出静态类的现实生活示例

  • 如果有任何限制, >
  • Why we use static class in c++?
  • Why we introduce this type of class?
  • Give the physical significance of static class?
  • Give the real life example of static class?
  • If there is any limitation, then tell me what?

我在等待您的回复。提前感谢。

I am waiting for your reply. Thanks in advance.

推荐答案

C ++中没有静态类。 static 是指存储类,即它适用于对象或函数,而不适用于数据类型。

There are no static classes in C++. static refers to a storage class, i.e. it applies to objects or functions, not to data types.

static class ,当应用于嵌套在其他类中的类时,意味着嵌套类可以独立于封装类的任何实例来实例化。在C ++中,总是大小写。 C ++中的嵌套类总是独立的数据类型。

In Java, static class, when applied to classes that are nested in other classes, means that the nested class can be instantiated independently of any instance of the enclosing class. In C++ that is always the case. Nested classes in C++ are always independent data types.

这里是我的意思:首先让我们来看看这个Java代码:

Here is what I mean: First let's take a look at this Java code:

public class A {
  public class B {
  }

  public static void main(String[] args)
  {
    A.B b1 = new A.B();  // <-- This is ill-formed, because A.B is not
                         //     an independent data type

    A a = new A();
    A.B b2 = a.new B();  // <-- This is correct. Use an instance of A to
                         //     create an object of type A.B.
  }
}

定义一个类 A 和嵌套类(即成员类或子类) AB 。主程式的第二行显示您无法实例化 A.B 类型的物件。你不能这样做,因为 B A 的成员类,因此需要类型 A 。主程序的第三行显示了如何做。

It defines a class A, and a nested class (i.e. a member class, or sub-class) A.B. The second line of the main program shows how you cannot instantiate a object of type A.B. You cannot do this because B is a member class of A and therefore requires an existing object of type A to be instantiated. The third line of the main program shows how this is done.

为了能够实例化 AB (与 A 的任何实例无关)您必须使 B A 静态成员类别:

In order to be able to instantiate an object of type A.B directly (independently of any instance of type A) you must make B a static member class of A:

public class A {
  public static class B {   // <---- I inserted 'static'
  }

  public static void main(String[] args)
  {
    A.B b1 = new A.B();  // <-- This is now well-formed

    A a = new A();
    A.B b2 = a.new B();  // <-- This is now ill-formed.
  }
}

另一方面,在C ++ 这不是必需的,因为在C ++中,成员类总是一个独立的数据类型(在这种意义上,不需要包含类的实例来创建嵌套类的实例):

On the other hand, in C++ this is not required, because in C++ a member class is always an independent data type (in the sense that no instance of the enclosing class is required to be able to create instances of the nested class):

class A
{
public:
  class B
  {
  };
};

int main()
{
  A::B b;   // <--- Perfectly well-formed instantiation of A::B
  return 0;
}

这篇关于静态类的真实生活意义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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