Created
November 14, 2018 12:53
-
-
Save Maxomax/495dab1931d53088ccb313661b906c0b to your computer and use it in GitHub Desktop.
интерфейс Externalizable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.task.task20.task2012; | |
import java.io.Externalizable; | |
import java.io.IOException; | |
import java.io.ObjectInput; | |
import java.io.ObjectOutput; | |
/* | |
OutputToConsole | |
*/ | |
public class Solution { | |
public static String greeting = "Hello world"; | |
/** | |
* OutputToConsole is an inner base class for improving your attentiveness. | |
* An OutputToConsole object encapsulates the information needed | |
* for displaying the [greeting] variable to the console. | |
* @author JavaRush | |
*/ | |
public static class OutputToConsole implements Externalizable { | |
private int counter; | |
/** | |
* @param out A stream for externalization | |
* @throws java.io.IOException | |
*/ | |
@Override | |
public void writeExternal(ObjectOutput out) throws IOException { | |
out.writeInt(counter); | |
} | |
/** | |
* @param in A stream for de-externalization | |
* @throws java.io.IOException | |
* @throws ClassNotFoundException | |
*/ | |
@Override | |
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { | |
counter = in.readInt(); | |
} | |
/** | |
* Class constructor that sets the private counter field. | |
*/ | |
public OutputToConsole(int counter) { | |
this.counter = counter; | |
} | |
/** | |
* Prints greeting message to console counter times. | |
*/ | |
public void printMessage() { | |
for (int i = 0; i < counter; i++) { | |
System.out.println(greeting); | |
} | |
} | |
} | |
public static void main(String[] args) { | |
OutputToConsole outputToConsole = new OutputToConsole(1000); | |
outputToConsole.printMessage(); | |
} | |
} | |
/*Требования: | |
1. Класс Solution.OutputToConsole должен поддерживать интерфейс Externalizable. | |
2. Класс OutputToConsole должен быть создан в классе Solution. | |
3. Класс OutputToConsole должен быть публичным. | |
4. Класс OutputToConsole должен быть статическим.*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment