繁体   English   中英

如何在JAVA中使用另一个类对象从主类调用主类的方法

[英]how to call a method of main class from main class with another class object in JAVA

如何使用另一个类对象从主类调用主类的方法。 请帮我

学生记录.java

package com.test;
public class  StudentRecord {
    public String name;  
    public int rollNumber;  
    public String departement; 
    public float    totalMark;
    boolean hasReservation;

    public  StudentRecord(){
        name           = new String("");
        rollNumber     = 0;
        departement    = new String("");
        totalMark      = 0;
        hasReservation = false;
    }
    public String toString() {   
        return "[" + departement+ ","+ name + "," + rollNumber +"]";   
    }
}

测试.java

package com.test;
public class Test { 
    public native static StudentRecord[] getStudentDetails();   
    public static void main(String[] args) {
        System.loadLibrary("Sample");
        int a= 10;
        StudentRecord[] records = getStudentDetails();      
        for(StudentRecord record:records){
            System.out.println("Name:"+record.name);
            System.out.println("Roll Number:"+record.rollNumber);
            System.out.println("Departement:"+record.departement);
            System.out.println("Total Marks:"+record.totalMark);
            System.out.println("Has Reservation:"+record.hasReservation);
        }

    }   
}
 error: cannot find symbol
         StudentRecord[] records = getStudentDetails();  
                     ^
  symbol:   class getStudentDetails
  location: class eventJava
eventJava.java:22: error: cannot find symbol
       for(Record record:records){
                         ^
  symbol:   variable records
  location: class test
2 errors

你可以通过 new Test().getStudentDetails() 而不是 getStudentDetails() 来试试这个,猜这对你有用

学生记录.java

package com.test;
public class  StudentRecord {
    public String name;  
    public int rollNumber;  
    public String departement; 
    public float    totalMark;
    boolean hasReservation;

    public  StudentRecord(){
        name           = new String("");
        rollNumber     = 0;
        departement    = new String("");
        totalMark      = 0;
        hasReservation = false;
    }
    public String toString() {   
        return "[" + departement+ ","+ name + "," + rollNumber +"]";   
    }
}

测试.java

package com.test;
public class Test { 
    public native static StudentRecord[] getStudentDetails();   
    public static void main(String[] args) {
        System.loadLibrary("Sample");
        int a= 10;
        StudentRecord[] records = getStudentDetails();      
        for(StudentRecord record:records){
            System.out.println("Name:"+record.name);
            System.out.println("Roll Number:"+record.rollNumber);
            System.out.println("Departement:"+record.departement);
            System.out.println("Total Marks:"+record.totalMark);
            System.out.println("Has Reservation:"+record.hasReservation);
        }

    }   
}

在同一个包中强制使用两个 java 代码

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM