复用有两种方式,组合和继承
在新类中创建现有类的对象。这种方式叫做 “组合”(Com-position),通过这种方式复用代码的功能,而非其形式
class WaterSource {
private String s;
WaterSource() {
System.out.println("WaterSource()");
s = "Constructed";
}
@Override public String toString() { return s; }
}
public class SprinklerSystem {
private String valve1, valve2, valve3, valve4;
private WaterSource source = new WaterSource();
private int i;
private float f;
@Override public String toString() {
return
"valve1 = " + valve1 + " " +
"valve2 = " + valve2 + " " +
"valve3 = " + valve3 + " " +
"valve4 = " + valve4 + "\n" +
"i = " + i + " " + "f = " + f + " " +
"source = " + source; // [1]
}
public static void main(String[] args) {
SprinklerSystem sprinklers = new SprinklerSystem();
System.out.println(sprinklers);
}
}
/* Output:
WaterSource()
valve1 = null valve2 = null valve3 = null valve4 = null
i = 0 f = 0.0 source = Constructed
*/
继承是所有面向对象语言的一个组成部分。事实证明,在创建类时总是要继承,因
为除非显式地继承其他类,否则就隐式地继承 Java 的标准根类对象(Object)
继承的父类子类的构造顺序:
class Art {
Art() {
System.out.println("Art constructor");
}
}
class Drawing extends Art {
Drawing() {
System.out.println("Drawing constructor");
}
}
public class Cartoon extends Drawing {
public Cartoon() {
//super(); 这句调用隐藏了
System.out.println("Cartoon constructor");
}
public static void main(String[] args) {
Cartoon x = new Cartoon();
}
}
/* Output:
Art constructor
Drawing constructor
Cartoon constructor
*/
继承时的各个类的构造顺序为:从最高层的父类依次往下进行构造
注意一个问题,上面的Drawing类的构造方法中其实隐式地调用了父类Carton地无参构造方法,假设给Craton类只定义一个有参构造方法,则编译器不会给Carton类提供一个默认的构造方法,此时必须在Drawing的构造方法中显示地调用Carton类的有参构造方法
class Carton{
public Carton(int i){
System.out.println("Carton 类的有参构造方法");
}
}
public class Drawing extends Carton {
public Drawing(){
super(1);//必须加上这一句 否则编译错误
System.out.println("Drawing 类的无参构造方法");
}
}
方法重写和变量覆盖:
class Carton{
String name="Carton";
public void print(){
System.out.println("Carton");
}
}
public class Drawing extends Carton {
String name="Drawing";
public void print(){
System.out.println("Drawing");
}
public void newMethod(){
System.out.println("newMethod");
}
public static void main(String[] args){
Carton c=new Drawing();
System.out.println(c.name);//Carton
c.print();//Drawing
// c.newMethod();//编译错误 父类中没有newMethod方法
}
}
当引用变量是父类类型,创建的对象的子类类型时,调用的变量时父类的,调用的重载的方法是子类的
final int a=1;//ok
final int a; a=1;//not ok
final int a;
{
a=1;//ok
}
final Apple apple=new Appale();
appple=new Apple();//not ok
apple.price=2.2;//ok
final修饰方法和类:
“类的代码在首次使用时加载 “。这通常是指创建类的第一个对象,或者是访问了类的 static 属性或方法。构造器也是一个 static 方法尽管它的 static 关键字是隐式的
class Insect {
private int i = 9;
protected int j;
Insect() {
System.out.println("i = " + i + ", j = " + j);
j = 39;
}
private static int x1 = printInit("static Insect.x1 initialized");
static int printInit(String s) {
System.out.println(s);
return 47;
}
}
public class Beetle extends Insect {
private int k = printInit("Beetle.k initialized");
public Beetle() {
System.out.println("k = " + k);
System.out.println("j = " + j);
}
private static int x2 =printInit("static Beetle.x2 initialized");
public static void main(String[] args) {
System.out.println("Beetle constructor");
Beetle b = new Beetle();
}
}
/* Output:
static Insect.x1 initialized
static Beetle.x2 initialized
Beetle constructor
i = 9, j = 0
Beetle.k initialized
k = 47
j = 39
*/
总结: