如何从公共班级召集私人班级

时间:2020-04-29 02:16:18

标签: java android

我有以下private class

private class sendReportePeriodico extends AsyncTask<String, Void, String> {

    protected String doInBackground(String... params) {
        socket = null;
        SocketAddress address = new InetSocketAddress(server_address, server_port);
        socket = new Socket();
        try {
            socket.connect(address, 3000);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            socket.setSoTimeout(3000);
        } catch (SocketException e) {
            e.printStackTrace();
            return "Can't Connect";
        }
        OutputStream out = null;
        try {
            out = socket.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        PrintWriter output = new PrintWriter(out);

        output.print(texto);
        output.flush();

        return null;
    }
}

我需要call这个课程inside public class

 public void onLocationChanged(Location location) {
    Log.d("LogXXX","Visualizando gps");
    texto = "GPS: " + location.getLatitude() + ", " + location.getLongitude();
    tvMensaje.setText(texto);
    mapa(location.getLatitude(), location.getLongitude());
    new sendReportePeriodico();

    HERE I NEED TO CALL THE PRIVATE CLASS;
 }

我该怎么办?

1 个答案:

答案 0 :(得分:0)

您要呼叫的班级是否公开并不重要。 如果“ sendReportePeriodico”类不在另一个类中,则无法调用它。

除非私人类位于另一个类内(在本例中称为“内部类”),否则它们是无用的。

即使反射也不起作用,因为私有类使用反射将outter类的实例作为参数)

解决方案是使“ sendReportePeriodico”类公开或受保护,或者将该类移入要从中调用的类中。