11

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

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);
    }
}
4

7 回答 7

18

您的代码中没有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;
}

使用当前的代码片段,您需要实例化Testing该类并使用该Sample方法。请注意,在这种情况下,您的类定义前面是关键字classclass 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);
 }
}
于 2013-08-01T06:00:43.240 回答
4

I doubt you actually want to create an object.

From your code snippet, I understand that you want to run a 'method' named Sample which adds two numbers. And in JAVA you don't have to instantiate methods. Objects are instances of class. A method is just a behavior which this class has.

For your requirement, you don't need to explicitly instantiate anything as when you run the compiled code JAVA automatically creates an instance of your class and looks for main() method in it to execute.

Probably you want to just do following:

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);
    }
}

Note: I changed Sample to sample as it's generally accepted practice to start a method name with lower-case and class name with an upper-case letter, so Testing is correct on that front.

于 2013-08-01T08:16:45.110 回答
1

new您使用关键字正确实例化,但是您的 calss 名称和方法调用是错误的

 Testing myTest = new Testing();
  int result =myTest.Sample(1);  //pass any integer value
  System.out.println(result );
于 2013-08-01T06:01:28.667 回答
1

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

int sample = Sample(3);

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

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

Testing testing = new Testing();
int sample = testing.Sample(3);
于 2013-08-01T06:02:13.370 回答
1

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

public static void main(String []args)
{
    int myTest = Sample(4555);//input may be any int else
    System.out.println(myTest);
}
于 2013-08-01T06:04:28.393 回答
1

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

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));
}
于 2013-08-01T06:08:02.130 回答
0

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

...Car c1 = new Car();

于 2020-10-29T18:57:00.863 回答