Java:如何将参数传递给构造函数?未定义的方法错误?

Sam*_*Sam 1 java constructor arguments class undefined

好的,所以我需要一些基本的帮助.这是我正在尝试学习的教程(http://docs.oracle.com/javase/tutorial/java/javaOO/classes.html),但我对如何实际传递数据感到困惑.问题是我在尝试学习java时已经掌握了Pascal的大脑......

这是我的代码.我究竟做错了什么?

public class OrigClass{
    public static void main(String[] args){
        StudentData(17, "Jack"); //error here: the method StudentData(int, String) is undefined for the type OrigClass
    }

    public class Student{
        public void StudentData(int age, String name){
            int sAge = age;
            String sName = name;
            System.out.println("Student Name: " + sName + " | Student Age: " + sAge);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在此先感谢您的帮助 :)

das*_*ght 5

构造函数不仅仅是一个方法:您需要为它指定与该类相同的名称,并使用new运算符调用它,如下所示:

public class Student{
    // Declare the fields that you plan to assign in the constructor
    private int sAge;
    private String sName;
    // No return type, same name as the class
    public Student(int age, String name) {
        // Assignments should not re-declare the fields
        sAge = age;
        sName = name;
        System.out.println("Student Name: " + sName + " | Student Age: " + sAge);
    }
}
// This goes in the main()
Student sd = new Student(17, "Jack");
Run Code Online (Sandbox Code Playgroud)