Answer the question
In order to leave comments, you need to log in
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!
}
}
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));
}
}
public class PrivateFields {
private boolean Key = false;
public void ShowStatus (){
System.out.println("Status: " + Key);
}
}
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();
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question