【问题标题】:How to instantiate an object in java?如何在java中实例化一个对象?
【发布时间】:2013-08-01 05:59:27
【问题描述】:

我是编程新手,我想知道我在实例化对象时哪里出错了。下面是代码:

public class Testing{
    private int Sample(int c)
    {
        int a = 1;
        int b = 2;
        c = a + b;
        return c;
    }
    public static void main(String []args)
    {
        Sample myTest = new Sample();
        System.out.println(c);
    }
}

【问题讨论】:

  • 变量c 可以在Sample 方法的范围内访问。你试图在这个范围之外访问它。

标签: java instantiation


【解决方案1】:

我怀疑你真的想创建一个对象。

从您的代码 sn-p 中,我了解到您想要运行一个名为 Sample 的“方法”,它将两个数字相加。而在 JAVA 中,您不必实例化方法。对象是class 的实例。方法只是这个类所具有的一种行为。

根据您的要求,您不需要显式实例化任何内容,因为当您运行编译后的代码时,JAVA 会自动创建您的类的实例并在其中查找 main() 方法来执行。

可能你只想做以下事情:

public class Testing{
    private int sample(int a, int b) {
        return a + b;
    }
    public static void main(String[] args) {
        int c = sample(1, 2);
        System.out.println(c);
    }
}

注意:我将Sample 更改为sample,因为普遍接受的做法是方法名以小写字母开头,类名以大写字母开头,因此Testing 在这方面是正确的。

【讨论】:

    【解决方案2】:

    您的代码中没有 Sample 类。您声明的是私有方法。

    // private method which takes an int as parameter and returns another int
    private int Sample(int c)
    {
      int a = 1;
      int b = 2;
      c = a + b;
      return c;
    }
    

    使用当前的 sn-p ,您需要实例化 Testing 类并使用 Sample 方法。请注意,您的类定义前面有关键字 class ,在本例中为 class Testing

    public class Testing{
      private int Sample(int c)
      {
        int a = 1;
        int b = 2;
        c = a + b;
        return c;
     }
      public static void main(String []args)
     {
        Testing t = new Testing(); // instantiate a Testing class object
        int result = t.Sample(1); // use the instance t to invoke a method on it
        System.out.println(result);
     }
    }
    

    但这并没有什么意义,你的 Sample 方法总是返回 3

    您是否正在尝试做这样的事情:

    class Sample {
     int a;
     int b;
    
     Sample(int a, int b) {
        this.a = a;
        this.b = b;
     }
    
     public int sum() {
        return a + b;
     }
    }
    
    public class Testing {
     public static void main(String[] args) {
        Sample myTest = new Sample(1, 2);
        int sum = myTest.sum();
        System.out.println(sum);
     }
    }
    

    【讨论】:

    • 你知道,你是对的,它没有意义,但是嘿,我正在学习!
    • 其实,是的!这正是我想做的!太棒了!
    【解决方案3】:

    你用new关键字正确实例化,但是你的类名和方法调用是错误的

     Testing myTest = new Testing();
      int result =myTest.Sample(1);  //pass any integer value
      System.out.println(result );
    

    【讨论】:

      【解决方案4】:

      Sample 不是一个类,它只是一个方法。您不能创建它的实例。 你只运行它 -

      int sample = Sample(3);
      

      如果您希望 sample 成为一个类,请将其定义为一个类。

      在您的情况下,该方法不是静态的,因此您不能直接从静态方法 Main 访问它。将其设为静态,以便您可以访问它。或者只是创建一个新的测试实例并使用它 -

      Testing testing = new Testing();
      int sample = testing.Sample(3);
      

      【讨论】:

        【解决方案5】:

        示例方法返回整数,因此获取结果并在任何地方使用。

        public static void main(String []args)
        {
            int myTest = Sample(4555);//input may be any int else
            System.out.println(myTest);
        }
        

        【讨论】:

          【解决方案6】:

          这就是你应该这样做的方式。

          public class Testing{
          public int Sample(int c)
          {
              int a = 1;
              int b = 2;
              c = a + b;
              return c;
          }
          public static void main(String []args)
          {
              // Creating an Instance of Testing Class
              Testing myTest = new Testing();
              int c =0;
              // Invoking the Sample() function of the Testing Class
              System.out.println(myTest.Sample(c));
          }
          

          【讨论】:

            【解决方案7】:

            嗯,这很容易。要在 Java 中实例化一个对象,您应该使用类名并使用它来处理它的类收到了一个 valor。例如:

            ...Car c1 = new Car();

            【讨论】:

            • 您的回答没有回答问题。您已经正确展示了如何实例化对象,但您没有突出显示问题代码中的变量范围
            猜你喜欢
            • 2016-05-20
            • 2016-07-26
            • 1970-01-01
            • 1970-01-01
            • 2014-10-31
            • 2011-06-05
            • 2012-01-13
            • 2018-12-13
            • 2012-07-16
            相关资源
            最近更新 更多