Professional Documents
Culture Documents
Класове в java
Класове в java
Класове в java
. . ,
,
." [Evans]
, ,
. ,
, ,
,
. , , (..
)
.
, - . - ()
, ,
. ,
, ,
. (class).
, , Java
.
: ?
(class) .
,
(), ().
(object) ()
. ,
, , " ".
, Dog,
, , ,
Dog. , , "some
string" , String. , Dog
, Java,
.
?
, . (
) . .
, .
,
. ,
, .
, -
.
:
- (class declaration) ,
. :
public class Dog {
-
, ,
, "{" "}",
. .
, .
(constructor) , . :
public Dog() {
// ... Some code ...
}
-
(fields) (
-), . ,
. , ,
, ,
, .
// Field/Property-storage definition
private String name;
(properties) .
.
, ,
.
// Field/Property-storage definition
private String name;
(methods) "", ,
, .
.
,
-
, -:
Dog.java
// Class declaration
class Dog { // Openening brace of the class body
// Property-field definition
private String name;
// Constructor definition
public Dog() {
this.name = "Sharo";
}
// Constructor definition
public Dog(String name) {
this.name = name;
}
// Property getter-method definition
public String getName() {
return this.name;
}
// Property setter-method definition
public void setName(String name) {
this.name = name;
}
// Method definition
public void bark() {
System.out.printf("Dog %s said: Wow-wow!%n", name);
}
} // Closing brace of the class body
,
.
" ",
.
.
?
,
. new
. .
,
.
() .
, , ,
getter setter , () .
,
, Dog main() .
-:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
name: ");
String firstDogName = input.nextLine();
// Assign dog name with a constructor
, , Dog.
,
bark() dog.bark().
/ Java
-
Java.
JDK 1.3.
Java -
. ,
. , -
java.io.
.
,
.
java.io.Reader,
java.io.Writer. -
java.io.BufferedReader,
readLine().
-
java.io.PrintWriter, println().
,
:
import java.io.*;
import java.lang.*;
public class TextFileLineNumberInserter {
public static void main(String[] args) throws Exception {
FileReader inFile = new FileReader("input.txt");
BufferedReader in = new BufferedReader(inFile);
FileWriter outFile = new FileWriter("output.txt");
PrintWriter out = new PrintWriter(outFile);
int counter = 0;
String line;
while ( (line=in.readLine()) != null ) {
counter++;
out.println(counter + ' ' + line);
}
in.close();
out.close();
}
}
, Java Unicode ,
Unicode,
8- .
Unicode - ,
. ,
.
java.io.InputStream,
java.io.OutputStream.
InputStream read(byte[] b),
, OutputStream write(byte[] b, int off,
int len), flush(),
.
, , ,
.
-
, flush(), ,
, . ,
:
FileInputStream inFile = new FileInputStream("input.bin");
FileOutputStream outFile = new FileOutputStream("output.bin");
byte buf[] = new byte[1024];
while (true) {
int bytesRead = inFile.read(buf);
if (bytesRead <= 0) break;
outFile.write(buf, 0, bytesRead);
}
outFile.close();
inFile.close();
Java
Java, - .
(multithreaded) ,
.
thread ().
. Microsoft Windows
, .
(),
.
, .
multithreading (). ,
Web- Mail- ,
3 Web
( HTTP), ( SMTP)
( POP3).
,
,
.
Java .
java.lang.Thread run(),
.
start()
. :
class MyThread extends Thread {
private String name;
private long timeInterval;
public MyThread(String name, long timeInterval) {
this.name = name;
this.timeInterval = timeInterval;
}
public void run() {
try {
while (!isInterrupted()) {
System.out.println(name);
sleep(timeInterval);
}