Java.lang.Integer 71
Optional: ob1 Integer ob2 String . generic- Object, , ob3. , .
generic- <T>, , . Object. , .
, :
public class OptionalExt <T extends T> {
Private T value;
}
, , () T, (bound) .
, . ?. extends .
/* # 11: : Mark.java, Runner.java */
package chapt03;
public class Mark<T extends Number> {
public T mark;
public Mark(T value) {
mark = value;
}
public T getMark() {
return mark;
}
public int roundMark() {
return Math. round (mark.floatValue());
}
/* */ // public boolean sameAny(Mark<T> ob) {
public boolean sameAny(Mark<?> ob) {
return roundMark() == ob.roundMark();
}
public boolean same(Mark<T> ob) {
return getMark() == ob.getMark();
}
}
package chapt03;
public class Runner {
public static void main(String[] args) {
// Mark<String> ms = new Mark<String>(7); //
Mark<Double> md = new Mark<Double>(71.4D); //71.5d
System. out. println(md.sameAny(md));
Mark<Integer> mi = new Mark<Integer>(71);
System. out. println(md.sameAny(mi));
// md.same(mi); //
System. out. println(md.roundMark());
}
}
:
True
True
sameAny(Mark<?> ob) Mark, , Mark<T> , .
generic- . , generic-:
|
|
class Optional <T> {
T value = new T();
}
, .
generic- , generic- generic-, :
/* # 12: : Failed.java */
package chapt03;
class Failed <T1, T2> {
static T1 value;
T2 id;
static T1 getValue() {
return value;
}
static void use() {
System. out. print(id);
}
}
(generic) , , , , , :
<T extends > returnType methodName(T arg) {}
<T> returnType methodName(T arg) {}
. , , , extends. .
Generic- , . . generic-.
/* # 13: : GenericMethod.java */
public class GenericMethod {
public static <T extends Number> byte asByte(T num) {
long n = num.longValue();
if (n >= -128 && n <= 127) return (byte)n;
else return 0;
}
public static void main(String [] args) {
System. out. println(asByte (7));
System. out. println(asByte (new Float("7.f")));
// System.out.println(asByte(new Character('7'))); //
}
}
Integer (int ) Float Number, . Character , asByte(T num).
.
/* # 14: : DemoVarargs.java */
package chapt03;
public class DemoVarargs {
public static int getArgCount(Integer... args) {
if (args.length == 0)
System. out. print("No arg=");
for (int i: args)
System. out. print("arg:" + i + " ");
return args.length;
}
public static void main(String args[]) {
System. out. println("N=" + getArgCount (7, 71, 555));
Integer[] i = { 1, 2, 3, 4, 5, 6, 7 };
System. out. println("N=" + getArgCount (i));
System. out. println(getArgCount ());
}
}
:
arg:7 arg:71 arg:555 N=3
arg:1 arg:2 arg:3 arg:4 arg:5 arg:6 arg:7 N=7
No arg=0
. getArgCount() . . . .
|
|
, :
void methodName([]... args){}
:
void methodName(Integer...args) {}
void methodName(int x1, int x2) {}
void methodName(String...args) {}
. Object... args , :
/* # 15: : DemoOverload.java */
package chapt03;
public class DemoOverload {
public static void printArgCount(Object... args) { //1
System. out. println("Object args: " + args.length);
}
public static void printArgCount(Integer[]...args){ //2
System. out. println("Integer[] args: " + args.length);
}
public static void printArgCount(int... args) { //3
System. out. print("int args: " + +args.length);
}
public static void main(String[] args) {
Integer[] i = { 1, 2, 3, 4, 5 };
printArgCount (7, "No", true, null);
printArgCount (i, i, i);
printArgCount (i, 4, 71);
printArgCount (i); // 1
printArgCount (5, 7);
// printArgCount();//!
}
}
:
Object args: 4
Integer[] args: 3
Object args: 3
Object args: 5
int args: 2
printArgCount() i Object... args, . Integer[]...args , Object[]...args. Integer[]...args Object...args.
- .
.
,
...args , :
void methodName(1 obj, 2... args) {}
(typesafe enums) Java java.lang.Enum. new. .
:
/* # 16: : SimpleUseEnum.java */
package chapt02;
enum Faculty {
MMF, FPMI, GEO
}
public class SimpleUseEnum {
public static void main(String args[]) {
Faculty current;
current = Faculty. GEO;
switch (current) {
case GEO:
System. out. print(current);
break;
case MMF:
System. out. print(current);
break;
// case LAW: System.out.print(current);// !
default:
System. out. print(" case: " + current);
}
}
}
case , switch.
Enum , , . enum :
static enumType[] values() , ;
static T valueOf(Class<T> enumType, String arg) , ;
static enumType valueOf(String arg) , ;
|
|
int ordinal() .
/* # 17: : Shape.java */
package chapt02;
enum Shape {
RECTANGLE, TRIANGLE, CIRCLE;
public double square(double x, double y) {
switch (this) {
case RECTANGLE:
return x * y;
case TRIANGLE:
return x * y / 2;
case CIRCLE:
return Math. pow (x, 2) * Math. PI;
}
throw new EnumConstantNotPresentException(
this. getDeclaringClass(), this. name());
}
}
/* # 18: : Runner.java */
package chapt02;
public class Runner {
public static void main(String args[]) {
double x = 2, y = 3;
Shape[] arr = Shape. values ();
for (Shape sh: arr)
System. out. printf("%10s = %5.2f%n",
sh, sh.square(x, y));
}
}
:
RECTANGLE = 6,00
TRIANGLE = 3,00
CIRCLE = 12,57
, square(). throw , . . case.
/* # 19: : DeanDemo.java */
package chapt02;
enum Dean {
MMF (""), FPMI (""), GEO ("");
String name;
Dean(String arg) {
name = arg;
}
String getName() {
return name;
}
}
package chapt02;
public class DeanDemo {
public static void main(String[] args) {
Dean dn = Dean. valueOf ("FPMI");
System. out. print(dn.ordinal());
System. out. println(": " + dn + ": " + dn.getName());
}
}
:
1: FPMI:
.
:
;
;
;
, new.
-, , , , , , . , , . , .
.
/* # 20: : RequestForCustomer.java */
package chapt03;
public @interface RequestForCustomer {
int level();
String description();
String date();
}
interface @. . -: int level(), String description(), String date().
, , . , , throws . : , String, Enum, Class .
java.lang.annotation.Annotation. : hashCode(), equals() toString(), Object. annotationType(), lass, .
|
|
. , . , , , , enum. . .
, -. , RequestForCustomer :
@RequestForCustomer (
level = 2,
description = "Enable time",
date = "10/10/2007"
)
public void customerThroughTime() {
//...
}
customerThroughTime(). , @, -. -, . , "Enable time" description(), RequestForCustomer. description . - , . , - .
/* # 21: : Request.java */
package chapt03;
import java.lang.reflect.Method;
public class Request {
@RequestForCustomer(level = 2,
description = "Enable time",
date = "10/10/2007")
public void customerThroughTime() {
try {
Class c = this. getClass();
Method m = c.getMethod("customerThroughTime");
RequestForCustomer ann =
m.getAnnotation(RequestForCustomer. class);
//
System. out. println(ann.level() + " "
+ ann.description() + " "
+ ann.date());
} catch (NoSuchMethodException e) {
System. out. println(" ");
}
}
public static void main(String[] args) {
Request ob = new Request();
ob.customerThroughTime();
}
}
:
2 Enable time 10/10/2007
, RUNTIME
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy. RUNTIME) //
, .
SOURCE .
CLASS .class, JVM .
, RUNTIME, .class JVM . , RUNTIME .
: -, .
- -. . . - -, .
public @interface TigerAnnotation {}
- isAnnotationPresent().
-. -. -, . - . , - value().
-. (_ = ) .
Java , @Retention, @Documented, @Target @Inherited java. lang. annotation. @Override, @Deprecated @Suppresswarnings java. lang.
.
3
A
1. n. , , , , , . m . , . .
|
|
2. n. . , , , , . . , , .
3. R3. , , . m . , .
4. (n x n). . , , . . , . , .
5. (m x n). . . , k- . , i- .
6. , , , . n, x, a[n].
7. (m,n). . , , . k , / . , .
8. . . , , , , . n . , .
9. . . , , /. .
10. (BoolMatrix) (n x m). . (), . .
11. (BoolVector) n. . , , .
12. n. . ; , , . , , (), , . , , , .
13. n. . m , . . : /.
14. . . . .
15. . . , , , , ( ).
16. . . : ( ); -, ( ), : ( ), ( ), , ( ).
17. . . , , , , . .
B
, . set (), get (), toString(). , . .
1. Student: id, , , , , , , , , .
. :
a) ;
b) ;
c) , ;
d) .
2. Customer: id, , , , , , .
. :
a) ;
b) , .
3. Patient: id, , , , , , , .
. :
a) , ;
b) , .
4. Abiturient: id, , , , , , .
. :
a) , ;
b) , ;
c) n , ( , ).
5. Book: id, , (), , , , , .
. :
a) ;
b) , ;
c) , .
6. House: id, , , , , , , .
. :
a) , ;
b) , , ;
c) , , .
7. Phone: id, , , , , , , , .
. :
a) , ;
b) , ;
c) .
8. Car: id, , , , , , .
. :
a) ;
b) , n ;
c) , .
9. Product: id, , UPC, , , , .
. :
a) ;
b) , ;
c) , .
10. Train: , , , (, , , ).
. :
a) , ;
b) , ;
c) , .
11. Bus: , , , , , .
. :
a) ;
b) , 10 ;
c) , 100000 .
12. Airlines: , , , , .
. :
a) ;
b) ;
c) , .
3
3.1.
?
1) private;
2) final;
3) native;
4) abstract;
5) protected.
3.2.
Quest3, "".
public class Quest3 {
Quest3 (int i){ System.out.print(""); }
public static void main(String[] args){
Quest3 s= new Quest3();
//1
}
public Quest3() {
//2
}
{
//3
} }
1) //1 Quest3(1);
2) //2 Quest3(1);
3) //3 new Quest3(1);
4) //3 Quest3(1).
3.3.
?
1) nonstatic- ;
2) static- ;
3) private- ;
4) final- .
3.4.
:
public class Quest5 {
{System.out.print("1");}
static {System.out.print("2");}
Quest5(){System.out.print("3");}
public static void main(String[] args) {
System.out.print("4");
} }
:
1) 1234;
2) 4;
3) 34;
4) 24;
5) 14.
, () () , .
, extends. , implements. , . ,
private ( ) .
( extends ) Object.
, , , , . ,
, .
, . , , .
() , , .
.
.
typeEmployee() Employee Manager. , .
/* # 1: :
Employee.java: Manager.java: Runner.java */
package chapt04;
public class Employee { //
private int id;
public Employee(int idc) {
super (); /* ,
*/
id = idc;
}
public int getId() {
return id;
}
public void typeEmployee() {
//...
System. out. println("");
}
}
package chapt04;
// ,
public class Manager extends Employee {
private int idProject;
public Manager(int idc, int idp) {
super (idc); /*
*/
idProject = idp;
}
public int getIdProject() {
return idProject;
}
public void typeEmployee() {
//...
System. out. println("");
}
}
package chapt04;
public class Runner {
public static void main(String[] args) {
Employee b1 = new Employee(7110);
Employee b2 = new Manager(9251, 31);
b1.typeEmployee(); // Employee
b2.typeEmployee(); // Manager
// b2.getIdProject();// !!!
((Manager) b2).getIdProject();
Manager b3 = new Manager(9711, 35);
System. out. println(b3.getIdProject()); // 35
System. out. println(b3.getId()); // 9711
}
}
b1 Employee, , , typeEmployee() Employee. b2 Employee Manager. , .
(, , ) , .
, , .
this,
super. :
/* # 2: : Course.java: BaseCourse.java: Logic.java */
package chapt04;
public class Course {
public int id = 71;
public Course() {
System. out. println(" Course");
id = getId(); //!!!
System. out. println(" id=" + id);
}
public int getId() {
System. out. println("getId() Course");
return id;
}
}
package chapt04;
public class BaseCourse extends Course {
public int id = 90; // !
public BaseCourse() {
System. out. println(" BaseCourse");
System. out. println(" id=" + getId());
}
public int getId() {
System. out. println("getId() BaseCourse");
return id;
}
}
package chapt04;
public class Logic {
public static void main(String[] args) {
Course objA = new BaseCourse();
BaseCourse objB = new BaseCourse();
System. out. println("objA: id=" + objA.id);
System. out. println("objB: id=" + objB.id);
Course objC = new Course();
}
}
:
Course
GetId() BaseCourse
id=0