, , :
c ;
Java , static , , . public, private, protected . . final . final , , .
, :
/* # 1: : MyClass.java */
class MyClass {
int x; //
int y = 2; //
final int YEAR = 2003; //
static int bonus; //
static int b = 1; //
void init(int z){//
z = 3;//
int a;//
a = 4;//
}
}
, . , new.
Java , . - Java :
( );
(private);
(protected);
, (public).
( ), public, . , private, . , . . , , , , protected.
- , . , ; , new . , .
Java , . finalize(). Java , , .
|
|
/* # 2: : NewBook.java */
class NewBook {
private String title, publisher;
private float price;
public NewBook() {
title = NoTitle;
}
public NewBook(String t, String pub, float p) {
title = new String(t);
publisher = pub;
price = p;
}
}
NewBook , :
NewBook tips1; //
tips1 = new NewBook();//
NewBook tips2 = new NewBook("Java2", "", 9.f);
new , , .
, Java , . , .
Locate c (), .
/* # 3: : Distance.java */
class Locate {
private double x, y; /* x=0 y=0 */
public Locate(){
x = 1;
y = 1;
}
public void setX(double a){
x = a;
}
void setY(double b){ /* */
y = b;
}
public double getX(){
return x;
}
public double getY(){
return y;
}
}
public class Distance {
public static void main(String[] args) {
//
Locate t1 = new Locate();
Locate t2 = new Locate();
double dx, dy, distance;
t1.setX(5);
t1.setY(10);
t2.setX(2);
t2.setY(6);
dx = t1.getX() - t2.getX();
dy = t1.getY() - t2.getY();
/* */
distance = Math.sqrt(dx*dx + dy*dy);
//distance = Math.hypot(dx, dy);//java 5.0
System.out.print(" : " + distance);
}
}
:
: 5.0
sqrt() ( hypot()) Math, . Math , E PI.
. , .
{ /* */ }
, , . , , , . this, , ++, .
static. .
|
|
/* # 9: : DemoLogic.java */
public class DemoLogic {
{
System.out.println(" (1) x="
+ this. x);
}
private int x = 1;
public DemoLogic() {
System.out.println("");
}
int getX(){
return x;
}
{
x = 2;
System.out.println(" (2) x=" + x);
}
public static void main(String[] args) {
DemoLogic obj = new DemoLogic();
System.out.println(" ="
+ obj.getX());
}
}
:
(1) x=0
(2) x=2
=2
x , , . x 2, , .
(generic) , , . - 10.
- :
class MyTempl < T1, T2 > {
T1 value1;
T2 value2;
}
T1, 2 , . . MyTempl , , :
MyTempl < Integer, Byte > ob =
new MyTempl < Integer, Byte > ();
CurrentType .
/* # 10: : DemoTemplate.java */
class CurrentType < T > {
T value;
CurrentType(){}
CurrentType(T value){
this. value = value;
}
T getValue() {
return this. value;
}
void setValue (T value) {
this. value = value;
}
}
public class DemoTemplate {
public static void main(String [] args){
CurrentType < Integer > ob1;
ob1 = new CurrentType < Integer > ();
ob1.setValue(new Integer(7));
System.out.println(ob1.getValue());
CurrentType < String > ob2 =
new CurrentType <String> ("Java");
System.out.println(ob2.getValue());
}
}
DemoTemplate : ob1
Integer ob2 String .
:
class MyTemplExt < T extends Number > {
T value;
}
, , Number.
.
. set(), get(), show(). . . .
1. Student: id, , , , , , , , .
2. Customer: id, , , , , , .
3. Patient: id, , , , , , , .
4. Abiturient: id, , , , , , .
5. BOOK: id, , (), , , , .
6. House: id, , , , , , .
7. Phone: id, , , , , , , .
8. Car: id, , , , , , .
|
|
9. Product (): id, , UPC, , , , .