T
T
thatmaniscool2017-05-31 20:14:08
Java
thatmaniscool, 2017-05-31 20:14:08

How to change the value of an interface field in Java?

First of all, I create an interface that will log the operation. After that, I create an interface that will process the logs.
public interface Logs { public void Log (); }

import java.util.*;

public interface DataLogs {
  public String NameOfUser ();
  public String NameOfSurname ();
  public List <Logs> logs ();
  
  public static class MyLogs {
    private static boolean TurnKey = false; // К этому полю мне нужно получить доступ.
    
    public static void ShowLogs (DataLogs data){
      System.out.println("User's name: " + data.NameOfUser());
      System.out.println("User's surname: " + data.NameOfSurname());
      
      // Если ключ в положении истина  - показать проведенные операции.
      if (TurnKey == true)
        for (Logs ShowLog : data.logs()){
          ShowLog.Log();
        }
      else
        System.err.println("Acces to Logs is denied!");
    } // The end of ShowLogs!
  }
}

Then I create a class that will, for example, collect log information.
import java.util.*;

public class Person implements DataLogs{
  private String name;
  private String Surname;
  private List <Logs> AllMyLogs = new ArrayList <Logs> ();
  
  public Person (String name, String Surname, int size){
    this.name = name; this.Surname = Surname;
    String str = "Some operations!";
    
    // Предположим этот метод получает данные из MySQL
    for (int i=0; i!=size; i++){ 
      AllMyLogs.add(
          new Logs (){
            public void Log() {
              System.out.println(str);
            }});
    }
   }

  public String NameOfUser() {
    return name;
  }
  public String NameOfSurname() {
    return Surname;
  }
  public List<Logs> logs() {
    return AllMyLogs; // Отправляем логи в интерфейс.
  }
}

Ну и собственно запускаем программу<code lang="java">
</code>

public class MainClass {

  public static void main(String[] args) {
    DataLogs.MyLogs.ShowLogs(new Person ("Nilokay", "Smirnow", 10));
  }
}

Now, I need to access the boolean value TurnKey, which is in a class in MyLogs, of the MyLogs interface.
How to do it?
PS,
For example, it is not difficult for me to get and change access in this way.
I'm making a class with private fields.
public class PrivateFields {
  private boolean Key = false;
  
  public void ShowStatus (){
    System.out.println("Status: " + Key);
  }
}

And actually the main class where these fields are edited.
import java.lang.reflect.*;
public class MainClass {

  public static void main(String[] args) {
    PrivateFields PF = new PrivateFields ();
    PF.ShowStatus();

    try {
      Field field = PF.getClass().getDeclaredField("Key");
      field.setAccessible(true);
      field.setBoolean(PF, true);
    
    } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException   e) {
      e.printStackTrace();
    }

    PF.ShowStatus();
  }
}

How to do the same but with an interface that contains a class?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Shockoway, 2017-06-01
@Shockoway

Code from the category "and obfuscation is not needed."
Just move the MyLogs class outside of the interface and add a normal human setter (reflection? seriously!?) to it for the variable you want to change.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question