【问题标题】:A static method belongs to the class rather than object of a class静态方法属于类而不是类的对象
【发布时间】:2015-03-05 14:54:06
【问题描述】:

静态方法属于类而不是类的对象。 无需创建类的实例即可调用静态方法。什么意思?

【问题讨论】:

标签: java oop


【解决方案1】:

这意味着,不需要从类中创建一个新实例,然后调用函数,如:

Foo f = new Foo();
f.bar();

您可以直接从类中访问它,例如:

Foo.bar();

这显然意味着您当然不能从静态方法中使用对象的任何非静态字段或方法。

【讨论】:

    【解决方案2】:
    ClassObject classObj = new ClassObject();
    classObj.doSomething();
    

    对比

    ExampleClass.staticMethod();
    

    第一个需要ClassObject 的实例来调用doSomething()。第二个没有。

    【讨论】:

      【解决方案3】:

      这是一个具有静态方法和标准方法的类的示例。

      public class MyClass {
          public static void staticPrintMe() {
              System.out.println("I can be printed without any instantiation of MyClass class");
          }
      
          public void nonStaticPrintMe() {
              System.out.println("I can be printed only from an object of type MyClass");
          }
      }
      

      这是调用这两种方法的代码:

      MyClass.staticPrintMe();                  // Called without instantiating MyClass
      
      MyClass myClassInstance = new MyClass();  // MyClass instantiation 
      myClass.nonStaticPrintMe();               // Called on object of type MyClass
      

      如您所见,调用静态方法时没有任何 MyClass 类型的对象。

      【讨论】:

        【解决方案4】:

        java.lang.Math 类为例。在这行代码中:

        double pi = 2 * Math.asin(1);
        

        我提到了Math 类,但asin 方法是static。没有创建该类的实例,该类仅充当此实用函数的占位符。

        由此推论,静态方法可能无法访问任何实例数据 - 它只能访问同样声明为 static 的类变量。

        【讨论】:

          【解决方案5】:

          看看这个例子。用静态方法和实例方法定义一个类:

          public class MyClass {
              public MyClass() {
                  // do something
              }
          
              public static staticMethod() {
                  // do something
              }
          
              public void instanceMethod() {
                  // do something
              }
          }
          

          用法:

          MyClass anInstance = new MyClass();
          
          // static method:
          MyClass.staticMethod();
          
          // instance method:
          anInstance.instanceMethod();
          
          // this is possible thought discoraged:
          anInstance.staticMethod();
          
          // this is forbidden:
          MyClass.instanceMethod();
          

          【讨论】:

            【解决方案6】:

            要调用静态方法,您不需要构建类实例(即对象)。

            例如:

            class Multiplier {
              public static double multiply(double arg1, double arg2) {
                      return arg1 * arg2;
              }
            }
            

            静态方法不使用类实例信息,你可以将方法用作:

            Multiplier.multiply(x, y);
            

            非静态方法使用类实例信息并依赖它。

            例如:

            class Pony {
              private Color color;
              private String name;
            
              public Pony(Color color, String Name) {
                this.color = color;
                this.name = name;
              }
            
              public void printPonyInfo() {
                System.out.println("Name: " + this.name);
                System.out.println("Color: " + this.color);
              }
            }
            
            
               Pony pony1 = new Pony(Color.PINK, "Sugar");
               Pony pony2 = new Pony(Color.BLACK, "Jupiter");
            

            当你打电话时:

            pony1.printPonyInfo();
            pony2.printPonyInfo();
            

            您可以获得每个小马对象的名称和颜色。你不能打电话给Pony.printPonyInfo(),因为 printPonyInfo() 不知道要打印什么。它不是静态的。只有创建了 Pony 类实例后,才能调用该方法,具体取决于类实例信息。

            【讨论】:

              【解决方案7】:

              当我们调用一个方法时,我们需要一个这样的类的实例。

              class SomeClass {
                  public void test() {}
              }
              
              SomeClass obj = new SomeClass();
              obj.test();
              

              'obj' 是 SomeClass 的一个实例。 如果没有像 'obj' 这样的实例,我们就无法调用测试方法。

              但静态方法不同。

              class SomeClass2 {
                  public static void testStatic() {}
              }
              

              我们不会像上面的 SomeClass 那样调用 testStatic 方法。

              SomeClass2 obj = new SomeClass2();
              obj.testStatic(); // wrong
              

              我们只用类类型调用。

              SomeClass2.testStatic();
              

              【讨论】:

                猜你喜欢
                • 2017-03-19
                • 1970-01-01
                • 2018-06-28
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2015-06-19
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多