- Java . , - . , Java main. Java, . , , , class, .
class Point {
}
Java . , .
2, . , . Java- . . , . .
class _ extends _ { type 1_:
type 2_:
type N_:
type 1(_) { ;
}
type 2(_) { ;
}
type (_) { ;
}
}
extends , _ _. Java Object. , extends . Point, .
(instance variables)
, . , . , , main. , Point int.
class Point { int , ;
}
, 4, . main, .
new
new . Point.
Point = new Point();
. Point . .
class TwoPoints {
public static void main(String args[]) {
Point p1 = new Point();
Point p2 = new Point();
p1.x = 10;
p1.y = 20;
2. = 42;
2. = 99;
System.out.println("x = " + p1.x + " = " + p1.y);
System.out.println("x = " + 2. + " = " + 2.);
} }
Point, , . , . , .
:\> Java TwoPoints
= 10 = 20
= 42 = 99
Point, TwoPoints, main Point . Point main , , .
class Point { int , ;
public static void main(String args[]) {
Point p = new Point();
. = 10;
p. = 20;
System.out.println("x = " + . + " = " + p.y);
} }
- , . , . . :
_ ( ) {
:
}
, , void - , . - -, . , .
class Point { int , ;
void init(int a, int b) {
= ;
= b;
} }
Java . Java , , , . , , , . 12 , ( -).
Java . , , . init, , this.
class Point { int , ;
void init(int , int ) {
this.x = ;
this. = } }
class TwoPointsInit {
public static void main(String args[]) {
Point p1 = new Point();
Point p2 = new Point();
p1.init(10,20);
p2.init(42,99);
System.out.println("x = " + p1.x + " = + p-l.y);
System.out.printlnC'x = " + p2.x + " = + p2.y);
} }
, , , init. Java , . , . , (, C++). - , void. init .
class Point { int , ;
Point(int , int ) {
this.x = ;
this. = ;
} }
class PointCreate {
public static void main(String args[]) {
Point p = new Point(10,20);
System.out.println("x = " + p.x + " = " + p.);
} }
Pascal (Delphi) constructor.
Java , . (method overloading). Point, , (-1).
class Point { int , ;
Point(int , int ) {
this.x = ;
this. = ;
}
Point() {
= -1;
= -1;
} }
class PointCreateAlt {
public static void main(String args[]) {
Point p = new Point();
System.out.println("x = " + p.x + " = " + p.y);
} }
Point , , . :
:\> java PointCreateAlt
= -1 = -1
, , , new. . .
this
Point , , this , .
class Point { int , ;
Point(int , int ) {
this.x = ;
this. = ;
}
Point() {
this(-1, -1);
} }
.
, , . Point distance. distance . , - Point.
class Point { int , ;
Point(int , int ) {
this.x = ;
this. y = y;
}
double distance(int , int ) {
int dx = this.x - ;
int dy = this. - ;
return Math.sqrt(dx*dx + dy*dy);
}
double distance(Point p) {
return distance(p.x, p.y);
} }
class PointDist {
public static void main(String args[]) {
Point p1 = new Point(0, 0);
Point p2 = new Point(30, 40);
System.out.println("p1 = " + pi.x + ", " + p1.y);
System.out.println("p2 = " + p2.x + ", " + p2.y);
System.out.println("p1.distance(p2) = " + p1.distance(p2));
System.out.println("p1.distance(60, 80) = " + p1.distance(60, 80));
} }
distance . :
:\> java PointDist
1 = 0, 0
2 = 30, 40
1.distance(p2) = 50.0
p1.distance(60, 80) = 100.0
- ( ). - , -. - . . , Point , z.
class Point3D extends Point { int z;
Point3D(int x, int y, int z) {
this.x = x;
this. = ;
this.z = z; }
Point3D() {
this(-1,-1,-1);
} }
extends , Point. , , Point3D Point.
, , C++, , . , . Java . , , Java , .
super
Point3D , . , this . super ( Delphi / ++ inherited).
class Point3D extends Point { int z;
Point3D(int x, int , int z) {
super(x, y); // this.z=z;
public static void main(String args[]) {
Point3D p = new Point3D(10, 20, 30);
System.out.println( " x = " + p.x + " y = " + p.y +
" z = " + p.z);
} }
:
:\> java Point3D
x = 10 = 20 z = 30
Point3D Point distance ( PointDist.java). , Point distance(mt , int ), . (override) , . (overloading), (overriding) distance.
class Point { int , ;
Point(int , int ) {
this.x = ;
this. = ;
}
double distance(int , int ) {
int dx = this.x - ;
int dy = this. - :
return Math,sqrt(dx*dx + dy*dy);
}
double distance(Point p) {
return distance(p., p.y);
}
}
class Point3D extends Point { int z;
Point3D(int , int y, int z) {
super(x, y);
this.z = z;
(
double distance(int , int y, int z) {
int dx = this.x - ;
int dy = this.y - y;
int dz = this.z - z;
return Math.sqrt(dx*dx + dy*dy + dz*dz);
}
double distance(Point3D other) {
return distance(other., other.y, other.z);
}
double distance(int , int y) {
double dx = (this.x / z) - ;
double dy = (this. / z) - y;
return Math.sqrt(dx*dx + dy*dy);
}
}
class Point3DDist {
public static void main(String args[]) {
Point3D p1 = new Point3D(30, 40, 10);
Point3D p2 = new Point3D(0, 0, 0);
Point p = new Point(4, 6);
System.out.println("p1 = " + p1.x + ", " + p1.y + ", " + p1.z);
System.out.println("p2 = " + p2.x + ", " + p2.y + ", " + p2.z);
System.out.println("p = " + p.x + ", " + p.y);
System.out.println("p1.distance(p2) = " + p1.distance(p2));
System.out.println("p1.distance(4, 6) = " + p1.distance(4, 6));
System.out.println("p1.distance(p) = " + p1.distance(p));
} }
:
:\> Java Point3DDist
p1 = 30, 40, 10
2 = 0, 0, 0
= 4, 6
p1.distance(p2) = 50.9902
p1.distance(4, 6) = 2.23607
p1.distance(p) = 2.23607
. , (dynamic method dispatch).
, / , .
class A { void callme() {
System.out.println("Inside A's callrne method");
class extends A { void callme() {
System.out.println("Inside B's callme method");
} }
class Dispatch {
public static void main(String args[]) {
A a = new B();
a.callme();
} }
main , . callme. callme , , , , , callme . :
:\> Java Dispatch
Delphi / C++ , Java ( virtual).
- , , .
final
. , - , final ( Delphi / C++ virtual).
final int FILE_NEW = 1;
final (.. C++). final- , (in-line) (- ).
finalize
Java finalize. finalize C++ ( ~) Delphi ( destructor). Java , .
static
, - . , main, , static. , this super. static, , . . , , .
class Static {
static int a = 3;
static int b;
static void method(int x) {
System.out.println("x = " + x);
System.out.println("a = " + a);
System.out.println("b = " + b);
}
static {
System.out.println("static block initialized");
b = a * 4;
}
public static void main(String args[]) {
method(42);
} }
.
:\> java Static static block initialized
= 42
= 3
B = 12
. .
class StaticClass {
static int a = 42;
static int b = 99;
static void callme() {
System.out.println("a = " + a);
} }
class StaticByName {
public static void main(String args[]) {
StaticClass.callme();
System.out.println("b = " + StaticClass.b);
} }
:
:\> Java StaticByName
= 42 b = 99
abstract
, , - , . abstract , . , abstract, , abstract. , new. , . , .
abstract class A {
abstract void callme();
void metoo() {
System.out.println("Inside A's metoo method");
} }
class B extends A {
void callme() {
System.out.println("Inside B's callme method");
} }
class Abstract {
public static void main(String args[]) {
A a = new B():
a.callme():
a.metoo():
} }
callme metoo , .
:\> Java Abstract
, . (overloading) (overriding) . this super . Java , - . - , .
10.11.2021 - 12:37: - Personalias -> WHO IS WHO - - _. 10.11.2021 - 12:36: - Conscience -> . ? - _. 10.11.2021 - 12:36: , , - Upbringing, Inlightening, Education -> ... - _. 10.11.2021 - 12:35: - Ecology -> - _. 10.11.2021 - 12:34: , - War, Politics and Science -> - _. 10.11.2021 - 12:34: , - War, Politics and Science -> . - _. 10.11.2021 - 12:34: , , - Upbringing, Inlightening, Education -> , - _. 10.11.2021 - 09:18: - New Technologies -> , 5G- - _. 10.11.2021 - 09:18: - Ecology -> - _. 10.11.2021 - 09:16: - Ecology -> - _. 10.11.2021 - 09:15: , , - Upbringing, Inlightening, Education -> - _. 10.11.2021 - 09:13: , , - Upbringing, Inlightening, Education -> - _. |