#jsl5# 章八 抽象class,接口与继承
8.1.1.1 abstract Classes
- An abstract class is a class that is incomplete, or to be considered incomplete.
- abstract class cannot be instantiated.
例子:抽象class不可以被初始化,正常class才可以。但是正常class可以extend 抽象class。
//Example 8.1.1.1-1. Abstract Class Declaration
abstract class Point {
int x = 1, y = 1;
void move(int dx, int dy) {
x += dx;
y += dy;
alert();
}
abstract void alert();
}
abstract class ColoredPoint extends Point {
int color;
}
class SimplePoint extends Point {
void alert() { }
}
//run
jshell> Point p = new Point()
| Error:
| Point is abstract; cannot be instantiated
| Point p = new Point();
| ^---------^
jshell> Point p = new SimplePoint()
p ==> SimplePoint@4cf777e8
8.1.1.2 final Classes
不可变,只能初始化一次。
8.1.2 Generic Classes and Type Parameters
8.1.2通用类和类型参数
如果一个类声明了一个或多个类型变量(§4.4),则它是通用的。这些类型变量称为类的类型参数。 方式参数部分在类名后面,并由尖括号分隔。
8.1.5超级接口
类声明中的可选implements子句列出了接口的名称这是被声明的类的直接超接口。
注意以下例子,通用class中需要完整声明interface包含的信息。
interface Colorable {
void setColor(int color);
int getColor();
}
enum Finish { MATTE, GLOSSY }
interface Paintable extends Colorable {
void setFinish(Finish finish);
Finish getFinish();
}
class Point { int x, y; }
class ColoredPoint extends Point implements Colorable {
int color;
public void setColor(int color) { this.color = color; }
public int getColor() { return color; }
}
class PaintedPoint extends ColoredPoint implements Paintable {
Finish finish;
public void setFinish(Finish finish) {
this.finish = finish;
}
public Finish getFinish() { return finish; }
}
8.2 Class Members 继承
注意作用范围,如果没有public,package中的class不能被外部class调用,会出现“XX is not public”的错误。class的继承一般用super
package Inheritance
- 如果可以改package,可以在参数前加个public
- 不改package,可以用super 继承
//Example 8.2-2. Inheritance of Class Members with Package Access
package points;
public class Point {
int x,y;
public void move(int dx, int dy){
x += dx; y += dy;
}
}
package points;
public class Point3d extends Point{
int z;
public void move(int dx, int dy,int dz){
x += dx; y += dy; z += dz;
}
}
//wrong one
import points.Point3d;
class Point4d extends Point3d{
int w;
public void move(int dx, int dy, int dz, int dw){
x += dx; y += dy; z += dz; w += dw;
}
}
//
//correct
import points.Point3d;
class Point4d extends Point3d{
int w;
public void move(int dx, int dy, int dz, int dw){
super.move(dx,dy,dz); w += dw;
}
}
Inheritance of public and protected Class Members
private变量不可被子集继承,只能作用于private的类。
the public and protected fieldsx, y, useCount, and totalUseCount are inherited in all subclasses of Point.
//Example 8.2-3. Inheritance of public and protected Class Members
package points;
public class Point {
public int x,y;
protected int useCount = 0;
static protected int totalUseCount = 0;
public void move(int dx, int dy){
x += dx; y += dy; useCount++; totalUseCount++;
}
}
class Point4d extends points.Point{
public void moverBack(int dx, int dy){
x -= dx; y -= dy;useCount++; totalUseCount++;
}
}
Inheritance of private Class Members
the class variable totalMoves can be used only within the class Point; it is not inherited by the subclass Point3d. A compile-time error occurs because method move of class Point3d tries to increment totalMoves.
// Example 8.2-4. Inheritance of private Class Members
package points;
class Point {
int x,y;
void move(int dx, int dy){ x += dx; y += dy; totalMoves++; }
private static int totalMoves ;
void printMoves(){ System.out.println(totalMoves); }
}
package points;
public class Point3d extends Point{
int z;
void move(int dx, int dy,int dz){
super.move(dx,dy); z += dz;
totalMoves++; // error
}
}
8.3 Field Declarations
Example 8.3-1. Multiply Inherited Fields
同名的会混乱,class用super继承,其他用 fields.name 继承。
jshell> interface Frob { float v = 2.0f; }
| 已创建 接口 Frob
jshell> class SuperTest { int v = 3; }
| 已创建 类 SuperTest
jshell> class Test extends SuperTest implements Frob {
...> public static void main(String[] args) {
...> new Test().printV();
...> }
...> void printV() { System.out.println(v); }
...> }
| 错误:
| 对v的引用不明确
| SuperTest 中的变量 v 和 Frob 中的变量 v 都匹配
| void printV() { System.out.println(v); }
| ^
jshell> class Test extends SuperTest implements Frob {
...> public static void main(String[] args) {
...> new Test().printV();
...> }
...> void printV() {
...> System.out.println((super.v + Frob.v)/2);
...> }
...> }
| 已创建 类 Test
8.3.1 Field Modifiers
用于修饰field的有以下。
FieldModifier:
(one of)
Annotation public protected private
static final transient volatile
8.3.1.1 static Fields
If a field is declared static, there exists exactly one incarnation of the field, nomatter how many instances (possibly zero) of the class may eventually be created. A static field, sometimes called a class variable, is incarnated when the class is initialized (§12.4).
//Example 8.3.1.1-2. Hiding of Class Variables
class Point {
static int x = 2;
}
class Test extends Point {
static double x = 4.7;
public static void main(String[] args) {
new Test().printX();
}
void printX() {
System.out.println(x + " " + super.x);
}
}
//output
4.7 2
jshell> Test.x
$7 ==> 4.7
jshell> Point.x
$8 ==> 2
8.4 Method Declarations
Example 8.4.2-1. Override-Equivalent Signatures
抽象method只能用在抽象class中。
abstract class Point {
int x, y;
public abstract String toString();
}
Example 8.4.8.1-2. Overriding
need to do it later
进度 297
2018.7.30