ArrayList odczyty statycznej składowej

0

Witam,
stworzyłem klasę MyClass zawierającą parę składowych i w innej klasie tworzę ArrayList obiektów tej stworzonej klasy. W tej MyClass mam statyczną składową i statyczne metody ustawiające i odczytujące tą składową. I mam mały problem ponieważ nie wiem jak się dostać do tych metod odczytujących i zapisujących tą składową statyczną.
Jak próbuję napisać:

arrayListMyClass.getStaticInt();

lub

arrayListMyClass.get(0).getStaticInt();

to pokazuje mi cannot resolve method.

0

Dlaczego chcesz coś takiego zrobić? Pokaż cały kod, ponieważ pewnie da się to zrobić w inny sposób.

0

@bakeraw2
Piszę aplikację do robienia testów i każdy obiekt klasy TestFile reprezentuje jedno pytanie wraz z odpowiedziami. Dodatkowo tworząc taki test chcę dodać możliwość podania czasu odpowiedzi na dane pytanie. Po wybraniu z bazy pytań do testu mają one zostać wrzucone do ArrayList i wrzucone do pliku i chcę by te pojedyncze czasy pytań były sumowane i zapisane jako czas na cały test dlatego chciałem użyć składowej statycznej która jest wspólna dla wszystkich obiektów danej klasy.

public class TestFile implements Serializable {
    private String question;
    private String answer_correct;
    private String[] answer_incorrect = new String[3];
    private int time;
    private static int gTime;

    public TestFile() {
    }

    public String getQuestion() {
        return question;
    }

    public void setQuestion(String question) {
        this.question = question;
    }

    public String getAnswer_correct() {
        return answer_correct;
    }

    public void setAnswer_correct(String answer_correct) {
        this.answer_correct = answer_correct;
    }

    public String[] getAnswer_incorrect() {
        return answer_incorrect;
    }

    public void setAnswer_incorrect(String[] answer_incorrect) {
        this.answer_incorrect = answer_incorrect;
    }

    public int getTime() {
        return time;
    }

    public void setTime(int time) {
        this.time = time;
    }

    public static int getgTime() {
        return gTime;
    }

    public static void setgTime(int gTime) {
        TestFile.gTime = gTime;
    }


0

A gdzie jest metoda getStaticInt()?

0

@bogdans

getStaticInt() nie ma ale jest getgTime() która jest statyczne i zwraca wartość statycznej składowej gTime

0

Nazwy zmiennych oraz klasy nie za bardzo podobają. Ja bym napisał jeszcze klasę QuestionService z metodą która zwraca pytania które mają być w teście oraz z metodą która oblicza łączny czas dostępny na wszystkie pytania.

0

@eri, ale Ty wywołujesz metodę getStaticInt(), której nie ma w klasie.

0

Robiłem kiedyś podobną aplikację i u mnie klasa wyglądała w ten sposób:

public class SpecialistQuestion {
    private int id;
    private int points;
    private String question;
    private String answerA;
    private String answerB;
    private String answerC;
    private ABCAnswer userAnswer;
    private ABCAnswer correctAnswer;
    private String module;
    private String mediaPath;
    private MediaType mediaType;

    public SpecialistQuestion(int id,
                              int points,
                              String question,
                              String answerA,
                              String answerB,
                              String answerC,
                              ABCAnswer userAnswer,
                              ABCAnswer correctAnswer,
                              String module,
                              String mediaPath,
                              MediaType mediaType) {
        this.id = id;
        this.points = points;
        this.question = question;
        this.answerA = answerA;
        this.answerB = answerB;
        this.answerC = answerC;
        this.userAnswer = userAnswer;
        this.correctAnswer = correctAnswer;
        this.module = module;
        this.mediaPath = mediaPath;
        this.mediaType = mediaType;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getPoints() {
        return points;
    }

    public void setPoints(int points) {
        this.points = points;
    }

    public String getQuestion() {
        return question;
    }

    public void setQuestion(String question) {
        this.question = question;
    }

    public String getAnswerA() {
        return answerA;
    }

    public void setAnswerA(String answerA) {
        this.answerA = answerA;
    }

    public String getAnswerB() {
        return answerB;
    }

    public void setAnswerB(String answerB) {
        this.answerB = answerB;
    }

    public String getAnswerC() {
        return answerC;
    }

    public void setAnswerC(String answerC) {
        this.answerC = answerC;
    }

    public ABCAnswer getUserAnswer() {
        return userAnswer;
    }

    public void setUserAnswer(ABCAnswer userAnswer) {
        this.userAnswer = userAnswer;
    }

    public ABCAnswer getCorrectAnswer() {
        return correctAnswer;
    }

    public void setCorrectAnswer(ABCAnswer correctAnswer) {
        this.correctAnswer = correctAnswer;
    }

    public String getModule() {
        return module;
    }

    public void setModule(String module) {
        this.module = module;
    }

    public String getMediaPath() {
        return mediaPath;
    }

    public void setMediaPath(String mediaPath) {
        this.mediaPath = mediaPath;
    }

    public MediaType getMediaType() {
        return mediaType;
    }

    public void setMediaType(MediaType mediaType) {
        this.mediaType = mediaType;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("SpecialistQuestion{");
        sb.append("id=").append(id);
        sb.append(", points=").append(points);
        sb.append(", question='").append(question).append('\'');
        sb.append(", answerA='").append(answerA).append('\'');
        sb.append(", answerB='").append(answerB).append('\'');
        sb.append(", answerC='").append(answerC).append('\'');
        sb.append(", userAnswer=").append(userAnswer);
        sb.append(", correctAnswer=").append(correctAnswer);
        sb.append(", module='").append(module).append('\'');
        sb.append(", mediaPath='").append(mediaPath).append('\'');
        sb.append(", mediaType=").append(mediaType);
        sb.append('}');
        return sb.toString();
    }
}
0

@eri gdzieś przez myśl przeszedł mi wzorzec dekoratora: http://www.tutorialspoint.com/design_pattern/decorator_pattern.htm
Gdzie w dekoratorze miałbyś tę metodę i pobierał nią wartości obiektu NewClass (ktory do dekoratora wrzuciłyś interfejsem jako pole Serializable newClass;
Ale to tylko szybka myśl, może starsi koledzy się wypowiedzą :) jak bede miał chwilę to pomyśle jeszcze nad tym, bo na razie troche zajęty jestem

1 użytkowników online, w tym zalogowanych: 0, gości: 1