Класове в java

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

"...

. . ,
,
." [Evans]
, ,
. ,
, ,
,
. , , (..
)
.
, - . - ()
, ,
. ,
, ,
. (class).
, , Java
.
: ?
(class) .
,
(), ().
(object) ()
. ,
, , " ".
, Dog,
, , ,
Dog. , , "some
string" , String. , Dog
, Java,
.
?


, . (
) . .
, .
,
. ,
, .

, -
.
:
- (class declaration) ,
. :
public class Dog {
-

, ,
, "{" "}",
. .
, .

public class Dog {


// ... Here the class body comes ...
}
-

(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

System.out.print("Write first dog's

Dog firstDog = new Dog(firstDogName);


System.out.print("Write second dog's name: ");
Dog secondDog = new Dog();
// Assign dog name with a property
secondDog.setName(input.nextLine());
// Create a dog with a default name
Dog thirdDog = new Dog();
Dog[] dogs = new Dog[] { firstDog, secondDog, thirdDog };
for (Dog dog : dogs) {
dog.bark();
}
}
:
Write first dog's name: Bobcho
Write second dog's name: Walcho
Dog Bobcho said: Wow-wow!
Dog Walcho said: Wow-wow!
Dog Sharo said: Wow-wow!
, Scanner,
, .
firstDogName.
Dog
firstDog, .
Dog,
. , Scanner,
setter setName().
setName() ,
, Dog
secondDog.setName().
Dog,
,
"Sharo".
Dog, ,
.

, , 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);
}

} catch (InterruptedException intEx) {


}
}
}
public class ThreadTest
{
public static void main(String[] args) {
MyThread thread1 = new MyThread("thread 1", 1000);
MyThread thread2 = new MyThread("thread 2", 2000);
MyThread thread3 = new MyThread("thread 3", 1500);
thread1.start();
thread2.start();
thread3.start();
}
}
3
MyThread.
1 2 .
, :
thread 1
thread 2
thread 3
thread 1
thread 3
thread 2
thread 1
thread 3
thread 1
...
, thread
stop(), thread-
interrupt(). thread
, isInterrupted(),

Thread setPriority(int), sleep() setDaemon()


. thread-

java.lang.Object wait() notify(),


.

You might also like