一起读Java编程思想(3)---继承、final关键字、复用类

复用类

继承语法
import static net.mindview.util.Print.*;
class Cleanser {
    private  String s = "Cleanser";
    public void append(String a) {
        s += a;
    }
    public void dilute() {
        append(" dilute()");
    }
    public void apply() {
        append(" apply()");
    }
    public void scrub() {
        append(" scrub()");
    }
    public String toString() { return s; }
    public static void main(String[] args) {
        Cleanser x = new Cleanser();
        x.dilute(); x.apply();  x.scrub();
        print(x);
    }
}

public class Detergent extends Cleanser {
    public void scrub() {
        append(" Detergent.scrub()");
        super.scrub();
    }
    public void foam() {
        append(" foam()");
    }
    public static void main(String[] args) {
        Detergent x = new Detergent();
        x.dilute();
        x.apply();
        x.scrub();
        x.foam();
        print(x);
        print("Testing base class");
        Cleanser.main(args);
    }
}

super.用于调用父类的内容。

初始化基类
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() {
        System.out.println("Cartoon constructor");
    }
    public static void main(String[] args) {
        Cartoon x = new Cartoon();
    }
}

输出:

Art constructor
Drawing constructor
Cartoon constructor

构建类的时候,是从基类向外扩散,基类在导出类构造器可以访问它之前就完成了初始化。

super() 与 this()例子
class Game {
    Game(int i) {
        System.out.println("Game constructor");
    }
}
class BoardGame extends  Game {
    BoardGame(int i) {
        super(i);   //调用父类Game中的构造器Game(int i)
        System.out.println("BoardGame constructor");
    }
}
public class Chess extends BoardGame{
    Chess() {
        super(11); //调用父类BoardGame中的构造器BoardGame(int i)
        System.out.println("Chess() constructor");
    }
    Chess(int i) {
        this();
        System.out.println("Chess(int) constructor");
    }
    public static void main(String[] args) {
        Chess x = new Chess();
        System.out.println("Chess y");
        Chess y = new Chess(11);
    }
}

class Shape {
    Shape(int i) {
        System.out.println("Shape constructor");
    }
    void dispose() {
        System.out.println("Shape dispose");
    }
}
class Circle extends Shape {
    Circle(int i) {
        super(i);
        System.out.println("Drawing Circle");
    }
    void dispose() {
        System.out.println("Erasing Circle");
        super.dispose();
    }
}
class Triangle extends  Shape {
    Triangle(int i) {
        super(i);
        System.out.println("Drawing Triangle");
    }
    void dispose() {
        System.out.println("Erasing Triangle");
        super.dispose();
    }
}
class Line extends Shape {
    private int start, end;
    Line(int start, int end) {
        super(start);
        this.end = end;
        System.out.println("Drawing Line: " + start + ", " + end);
    }
    void dispose() {
        System.out.println("Erasing Line: " + start + ", " + end);
        super.dispose();
    }
}
public class CADSystem extends Shape{
    private Circle c;
    private Triangle t;
    private Line[] lines = new Line[3];
    public CADSystem(int i) {
        super(i+1);
        for(int j = 0; j < lines.length; j++) {
            lines[j] = new Line(j, j*j);
        }
        c = new Circle(1);
        t = new Triangle((1));
        System.out.println("Combind constructor");
    }
    public void dispose() {
        System.out.println("CADSystem.dispose()");
        t.dispose();;
        c.dispose();
        for(int i = lines.length - 1; i >= 0; i--) {
            lines[i].dispose();
        }
        super.dispose();
    }
    public static void main(String[] args) {
        CADSystem x = new CADSystem(47);
        try {

        } finally {
            x.dispose();
        }
    }
}

Shape constructor
Shape constructor
Drawing Line: 0, 0
Shape constructor
Drawing Line: 1, 1
Shape constructor
Drawing Line: 2, 4
Shape constructor
Drawing Circle
Shape constructor
Drawing Triangle
Combind constructor
CADSystem.dispose()
Erasing Triangle
Shape dispose
Erasing Circle
Shape dispose
Erasing Line: 0, 4
Shape dispose
Erasing Line: 0, 1
Shape dispose
Erasing Line: 0, 0
Shape dispose
Shape dispose
protected关键字

对于类用户,protected对象是Private的,但是其他基础

class Villain {
    private String name;
    protected void set(String nm) {
        name = nm;
    }
    public Villain(String name) {
        this.name = name;
    }
    public String toString() {
        return "I'm a Villain and my name is " + name;
    }
}

public class Orc extends Villain {
    private int orcNumber;
    public Orc(String name, int orcNumber) {
        super(name);
        this.orcNumber = orcNumber;
    }
    public void change(String name, int orcNumber) {
        set(name);
        this.orcNumber = orcNumber;
    }
    public String toString() {
        return "Orc " + orcNumber + ": " + super.toString();
    }
    public static void main(String[] args) {
        Orc orc = new Orc("Limburger", 12);
        System.out.println(orc);
        orc.change("Bob", 19);
        System.out.println(orc);
    }
}
Orc 12: I'm a Villain and my name is Limburger
Orc 19: I'm a Villain and my name is Bob
向上转型

栗子:

class Instrument {
    public void play() {};
    static void tune(Instrument i) {
        i.play();
    }
}
public class Wind extends Instrument {
    public static void main(String[] args) {
        Wind flute = new Wind();
        Instrument.tune(flute);    //向上转型
    }
}

tune()方法可以接受Instrument引用,但是在Wind.main()中传递给tune()方法的是一个Wind引用。可见Wind对象同样也是一种Instrument对象,并且不存在任何tune()方法可以通过Instrument来调用,同样不存在Wind中。tune()中程序代码可以对Instrument和他的所有导出类起作用。Wind引用转换为Instrument引用的动作,称为向上转型。

final关键字
final数据

基本类型final使得数据恒定不变,对于引用对象,final使引用恒定不变,一旦引用被初始化指向一个对象就无法再把它改为指向另外一个对象。

import java.util.*;
import static net.mindView.util.Print.*;

class Value {
	int i;
	public Value(int i) {
		this.i = i;
	}
}
public class FinalData {
	private static Random rand = new Random[47];
	private String id;
	public finalData(String id) {
		this.id = id;
	}
	private final int valueOne = 9;
	private static final VALUE_TWO = 99;
	public static final int VALUE_THREE = 39;
	private final int l4 = rand.nextInt(20);
	private Value v1 = new Value(11);
	private final Value v2 = new Value(22);
	private static final Value VAL_3 = new Value(33);
	private final int[] a = {1, 2, 3, 4, 5, 6};
	public String toString() {
		return id + ": " + "i4 = " + i4 + ", INT_5=" + INT_5;
	}
	public static void main(String[] args) {
		FinalData fd1 = new FinalData("fd1");
		fd1.v2.i++;
		fd1.v1 = new Value(9);
		for(int i = 0; i < fd1.a.length; i++) {
			fd1.a[i]++;
		}
		print(fd1);
		print(fd2);
		FinalData fd2 = new FinalData("fd2");
		print(fd2);
	}
}
继承与初始化
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 ex tends 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();
    }
}

输出

static Insect.x1 initialized
static Beetle.x2 initialized
Beetle constructor
i = 9, j = 0
Beetle.k initialized
k = 47
j = 39

运行时第一步找Beetle.main()启动Beetle编译,故先编译Insect类

欢迎加微信公众号进一步交流!
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值