博客
关于我
8.2 复用(protected+向上转型+final关键字+类的初始化)
阅读量:281 次
发布时间:2019-03-01

本文共 8072 字,大约阅读时间需要 26 分钟。

protected

???????????????????????????????????????????? protected ?????????????????????? private ???????????????????????????????protected ???????????

?????? protected ????????????????? private????? protected ????????????????

????

?????????????????????????????????????????????????????????????????????????

class Instrument {    public void play() {    }    static void tune(Instrument i) {        // ...        i.play();    }}// Wind objects are instruments// because they have the same interface:public class Wind extends Instrument {    public static void main(String[] args) {        Wind flute = new Wind();        Instrument.tune(flute); // Upcasting    }}

tune() ??????? Instrument ?????????? Wind ? main() ????tune() ???????? Wind ?????????????????????????????????????????????????????Wind ?????????? Instrument ????? Instrument ? tune ???????? Wind ??? tune() ????? Instrument ??? Instrument ??????????? Wind ????? Instrument ????????????

????????????????????????????Wind.java ?????

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

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

??????????????????????????????????????????????????????

2.1 ?????????????

??????????????????????????????????????????????????????????????????????????????????? OOP ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

final ???

????????Java ???? final ?????????????????????????????????????????????????????????????????? final?

??????????? final ??????????????

3.1 final ????

????????????????????????????????????????

  • ?????????????
  • ?????????????????

????????????????????????????????????????????????????? Java ???????????????????? final ???????????????????

??? static ? final ???????????????????????

?? final ?????????????????????????????????final ????????????????final ???????????????????????????????????????????????????Java ?????????????????????????????????????????????????????????

3.1.1 ?? final

?? final ?????????? final ???????????? final ??????????????????????????????? final ???????????? final ??????????????

3.1.2 final ????

????????????? final ????????????????????????

class Gizmo {    public void spin() {    }}public class FinalArguments {    void with(final Gizmo g) {        //-g = new Gizmo(); // Illegal -- g is final    }    void without(Gizmo g) {        g = new Gizmo(); // OK -- g is not final        g.spin();    }    //void f(final int i) { i++; } // Can't change    // You can only read from a final primitive    int g(final int i) {        return i + 1;    }    public static void main(String[] args) {        FinalArguments bf = new FinalArguments();        bf.without(null);        bf.with(null);    }}

?? f() ? g() ??? final ??????????????????????????????????????????????????????????

3.2 final ????

?? final ?????????????????????????????????????????????????????????????????

?????? final ???????????????? Java ?????????????? final?????????????????????????????? final ??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

???? Java ???????????????????? hotspot ??????????????????????????????????? final ?????????????????? JVM ???????????????????????? final?

3.2.1 final ? private

????? private ????????? final???? private ???? final ????????????????

?????????????? private ?????? final ????????????????????????

class WithFinals {    // Identical to "private" alone:    private final void f() {        System.out.println("WithFinals.f()");    }    // Also automatically "final":    private void g() {        System.out.println("WithFinals.g()");    }}class OverridingPrivate extends WithFinals {    private final void f() {        System.out.println("OverridingPrivate.f()");    }    private void g() {        System.out.println("OverridingPrivate.g()");    }}class OverridingPrivate2 extends OverridingPrivate {    public final void f() {        System.out.println("OverridingPrivate2.f()");    }    public void g() {        System.out.println("OverridingPrivate2.g()");    }}public class FinalOverridingIllusion {    public static void main(String[] args) {        OverridingPrivate2 op2 = new OverridingPrivate2();        op2.f();        op2.g();        // You can upcast:        OverridingPrivate op = op2;        // But you can't call the methods:        //- op.f();        //- op.g();        // Same here:        WithFinals wf = op2;        //- wf.f();        //- wf.g();    }}

????????????????????????????????????????????????????????????????? private ??????????????????????????????????????????????????????????? public?protected ???????????????????????????????????????????????? private ?????????????????????????????????????????

3.3 final ???

?????? final?final ???????????????????????????????????????????????????????????????

class SmallBrain {}final class Dinosaur {    int i = 7;    int j = 1;    SmallBrain x = new SmallBrain();    void f() {    }}// - class Further extends Dinosaur {} // error: Cannot extend final class 'Dinosaur'public class Jurassic {    public static void main(String[] args) {        Dinosaur n = new Dinosaur();        n.f();        n.i = 40;        n.j++;    }}

????????????? final ???????????????????????????????

??????????????????????????????????????????????? final????????????????????????????????????????????????

Java ????????????????? Java 1.0/1.1 ? Vector ????????????????????????????????????????????? final?????? final ??????????[^1]???????????????????????????????????????????????????Stack ??? Vector???? Stack ?? Vector????????????????Java ?????????????????? Stack ?????????? final ???????

???Vector ??????????? addElement() ? elementAt() ??????????????????????????????????????? final ?????????????????????????????????????????????????????????????????????? Java ??? ArrayList ??? Vector?????????????????????????????????????? Vector?

Java 1.0/1.1 ????????????? Hashtable???? HashMap ????????? final ???????????????????????????????Hashtable ?? Vector ????????????????????????????????????????????????????????????????????????????????????

4 ???????

? Java ?????????????????????????????Java ?????????????????????????????????????????????????????????????????????????????????????????? static ????????????? static ????? static ?????????????????????????? static ?????????????

??????? static ?????????? static ??? static ??????????????????????????????static ??????????

4.1 ??????

??????????????????????????????

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();    }}

??? java Beetle???????? Beetle ?? main() ????? static ???????????? Beetle ?????????? Beetle.class ???????????????????????????????????????????????????????????????????????????????

?????????????????????????????????????????????? Insect?? static ????????????????????????????????? static ?????????????????????

???????????????????????????????????????????????????? null?????????????????????????????????????????????????????? super ???????????? Beetle ??????????????????????????????????????????????????????????????????????????????

转载地址:http://bntx.baihongyu.com/

你可能感兴趣的文章
Python3 - 在字符串格式化程序参数中使用变量
查看>>
Python3 A*寻路算法实现
查看>>
python3 grpc安装
查看>>
python3 mysql 封装多表_python3 mysql-多表查询
查看>>
python3 mysql8_Python MySQL8.0 错误:authentication plugin 'calling_sha2_password' is not supported....
查看>>
Python关键代码总览!从Zero到Hero必看的考点
查看>>
python3 robotframework+pycharm框架搭建
查看>>
Python3 shutil模块
查看>>
Python关于strftime函数详细解析 附实战代码
查看>>
Python3 tutorial day3
查看>>
python3 week4
查看>>
Python3 创建数组
查看>>
python3 套接字异常(a byte-like object is required not str)
查看>>
python3 实现用户三次输入错误记录黑名单并对该用户登录锁定-day1
查看>>
python3 文本变图片
查看>>
Python3 查看 com 组件的属性
查看>>
python3 案例分享--发邮件
查看>>
python3 模板库_Python3 标准库概览_Python教程_我爱模板网 - 提供下载各种免费建站资源,免费网站模板,免费网页特效,让你爱上建站!...
查看>>
Python3 求最大/小值及索引值 Numpy
查看>>
python3+django使用celery执行某些任务失败的解决方案
查看>>