GetId() BaseCourse
id=90
Course
GetId() BaseCourse
id=0
BaseCourse
GetId() BaseCourse
id=90
objA: id=0
objB: id=90
Course
GetId() Course
id=71
getId() Course, BaseCourse . BaseCourse :
Course objA = new BaseCourse();
BaseCourse objB = new BaseCourse();
BaseCourse() Course. BaseCourse, getId(),
BaseCourse, id, BaseCourse. id , .. .
((BaseCourse)objA).id ((Course)objB).id, id .
final
, final:
// ConstCourse
final class ConstCourse { /**/ }
//
class BaseCourse extends ConstCourse { /**/ }
super this
super . :
super(_);/*
*/
super. id = 71; /* */
super. getId(); //
super id . Java , , , .
this , . , . id this.id, , id this.id .
Point2D |
- |
x: int |
- |
y: int |
+ |
Point2D(int, int) |
Point3D |
- |
z: int |
+ |
Point3D(int, int, int) |
+ |
Point3D() |
Point4D |
- |
ltime: long |
+ |
Point4D(int, int, int, long) |
+ |
Point4D() |
, , this, .
// # 3: this : Point2D.java, Point3D.java, Point4D.java
package chapt04;
public class Point2D {
private int x, y;
public Point2D(int x, int y) {
this. x = x; //this
this. y = y; //x, y, x, y, z
|
|
}
}
package chapt04;
public class Point3D extends Point2D {
private int z;
public Point3D(int x, int y, int z) {
super (x, y);
this. z = z;
}
public Point3D() {
this (-1,-1,-1); // Point3D
}
}
package chapt04;
public class Point4D extends Point3D{
private long time;
public Point4D(int x, int y, int z, long time) {
super (x, y, z);
this. time = time;
}
public Point4D() {
// super();
}
}
Point3D . , .
this , x, y z , , , . this() .
Java , , . , , Object .
, . , . (overloading). , . , , ( ), (overriding) . , . , Java , , , . , . . , , , , .
Course |
- |
id: int |
- |
name: String |
+ |
toString(): void |
BaseCourse |
- |
idTeacher: int |
+ |
toString(): void |
OptionalCourse |
- |
required: int |
+ |
toString(): void |
. 4.1.
/* # 4: : Course.java: BaseCourse.java: OptionalCourse.java: DynDispatcher.java */
package chapt04;
public class Course {
private int id;
private String name;
public Course(int i, String n) {
id = i;
name = n;
}
public String toString() {
return ": " + name + "(" + id + ")";
}
}
|
|
package chapt04;
public class BaseCourse extends Course {
private int idTeacher;
public BaseCourse(int i, String n, int it) {
super (i, n);
idTeacher = it;
}
public String toString() {
/* toString() !!!
,
*/
Return
super. toString() + " .(" + idTeacher + ")";
}
}
package chapt04;
public class OptionalCourse extends BaseCourse {
private boolean required;
public OptionalCourse(int i, String n, int it,
boolean r) {
super (i, n, it);
required = r;
}
public String toString() {
return super. toString() + " required->" + required;
}
}
package chapt04;
public class DynDispatcher{
public void infoCourse(Course c) {
System. out. println(c.toString());
//System.out.println(c);//
}
}
package chapt04;
public class Runner {
public static void main(String[] args) {
DynDispatcher d = new DynDispatcher();
Course c = new Course(7, "");
d.infoCourse(cc);
BaseCourse bc = new BaseCourse(71, "", 2531);
d.infoCourse(bc);
OptionalCourse oc =
new OptionalCourse(35, "", 4128, true);
d.infoCourse(oc);
}
}
:
: (7)
: (71) .(2531)
: (35) .(4128) required->true
, toString() super . super() .
: .
Java ( virtual, C++, ).
, , . .
, , , .
/* # 5: -: CourseHelper.java:
BaseCourseHelper.java: RunnerCourse.java*/
package chapt04;
public class CourseHelper {
public Course getCourse(){
System. out. println("Course");
return new Course();
}
}
package chapt04;
public class BaseCourseHelper extends CourseHelper {
public BaseCourse getCourse(){
System. out. println("BaseCourse");
return new BaseCourse();
}
}
package chapt04;
public class RunnerCourse {
public static void main(String[] args) {
CourseHelper bch = new BaseCourseHelper();
Course course = bch.getCourse();
//BaseCourse course = bch.getCourse();//
System. out. println(bch.getCourse().id);
}
}
BaseCourseHelper . getCourse() , -. , getCourse(), Course.
- , . ( ) .
:
Transport s1 = new Bus();
Transport |
+ |
repair(): void |
Bus |
+ |
repair(): void |
Tram |
+ |
repair(): void |
Transport s2 = new Tram();
|
|
. 4.2.
Transport . Bus Tram .
/* # 5: : Transport.java: Bus.java: Tram.java:
RepairingCenter.java: Runner.java*/
package chapt04;
import java.util.Random;
class Transport {
public void repair() { /* */
}
}
class Bus extends Transport {
public void repair() {
System. out. println(" ");
}
}
class Tram extends Transport {
public void repair() {
System. out. println(" ");
}
}
class RepairingFactory { // Factory
public Transport getClassFromFactory(int numMode) {
switch (new Random().nextInt(numMode)) {
case 0:
return new Bus();
case 1:
return new Tram();
default:
throw new IllegalArgumentException();
// assert false;
// return null;
/*
* if((int)(Math.random() * numMode)==0) return new Bus(); else
* return new Tram();
* . ?
*/
}
}
}
public class Runner {
public static void main(String[] args) {
RepairingFactory rc = new RepairingFactory();
Transport[] box = new Transport[15];
for (int i = 0; i < box.length; i++)
/* */
box[i] = rc.getClassFromFactory(2 );// 2
for (Transport s: box)
s.repair(); //
}
}
.
RepairingFactory getClassFromFactory(int numMode), Transport , . return, Bus Tram. main() Transport, getClassFromFactory(). , ( , ). K , repair() .
, , TrolleyBus, repair() getClassFromFactory(), .
, , . . , , .
/* # 6: : Runner.java */
package chapt04;
class Base {
public static void assign() {
System. out. println(
" assign() Base");
}
}
class Sub extends Base {
public static void assign() {
System. out. println(
" assign() Sub");
}
}
public class Runner {
public static void main(String[] args) {
Base ob1 = new Base();
Base ob2 = new Sub();
Sub ob3 = new Sub();
ob1. assign (); //
ob2. assign (); // Base.assign();
|
|
ob3. assign ();
}
}
:
assign() Base
assign() Base
assign() Sub
ob1 ob2, assign() Base. ob3 assign(), . static , .
, , :
Base. assign ();
Sub. assign ();
.
. , . . , , . , . .
abstract , , . , , . , . , .
( ) . Number Byte, Float . Number ,
floatValue(). , -. Number , . .
/* # 7: : AbstractManager.java */
package chapt04;
public abstract class AbstractManager {
private int id;
public AbstractManager(int id) { //
this. id = id;
}
//
public abstract void assignGroupToCourse(
int groupId, String nameCourse);
}
/* # 8: : CourseManager.java */
package chapt04;
// assignGroupToCourse()
public class CourseManager extends AbstractManager {
public void assignGroupToCourse(
int groupId, String nameCourse) {
//...
System. out. println(" " + groupId
+ " " + nameCourse);
}
}
/* # 9: : Runner.java */
package chapt04;
public class Runner {
public static void main(String[] args) {
AbstractManager mng; //
// mng = new Abstract Manager(); !
mng = new CourseManager();
mng.assignGroupToCourse(10, "");
}
}
: