复用代码是Java众多引人注目的功能之一,但要成为极具革命性的语言,仅仅可以复制代码并对之加以改变是不够的,它还必须可以作更多的事情。ide
就是在当前类中产生现有类的对象。函数
每个非基本类型的对象都有该方法,当编译器须要一个String但你只有一个对象时,该方法会自动调用。代理
编译器并非简单的为每个引用都建立对象,由于这回增长没必要要的负担code
extends
关键字实现继承Object
继承super.func()
调用父类中的方法。public class Test { TestDemo demo = new TestDemo(); // 代理 public void func() { demo.func(); } } class TestDemo { void func() {} }
finally
代码块中。@Override
来表示要重写父类方法。组合和继承都容许在新的类中放置子对象,组合是显式地这样作,而继承是隐式地作。对象
继承很重要可是并不意味着咱们须要经常用他,如何判断是否应该使用继承请参照两个标准:排序
protected
域成员,但最好的方式仍是private
,只有在真正须要的时候才使用protected
关键字。final
基本数据类型:表示数据是不可变的常量final
对象引用:引用与对象的关联关系不可变,但能够对对象进行操做。final static
约定用大写字母+下划线命名规范final
但又未给定初值的域,但能够在构造方法static
代码块或构造器中对final
进行赋值。final
private
方法都隐式的指定是final
的,在private
方法前添加final
是没有额外意义的。private
修饰的方法,不属于基类接口一部分,他仅仅是隐藏在类中的一部分代码。所以若是你在导出类中“覆盖”了基类的private
方法,其实并无覆盖private
方法,而产生了一个新方法。final
放在class
前作修饰,代表该类没法进行继承final
类中的域和普通类的域并没有差异final
类隐式的将该类中全部的方法指定为final
,因此在final
类中给方法添加final
关键词没有意义。要考虑清楚!!!继承
每一个类的编译代码.class都存在于独立的文件中,该文件只在须要的使用程序代码时才会被加载。接口
static
变量public class Test extends TestParents { // (a) static属性 static String staticProperty; // (b) 构造方法 public Test() { System.out.println("Test constructor"); } // (c) static代码块 { staticProperty = print("Test static property"); System.out.println("Test static"); } // (d) 非static代码属性 String property = print("Test property"); public static void main(String[] args) { // (1):System.out.println(Test.staticProperty); // TestParents static property // null(由于static代码块中的代码要建立对象才执行) // (2):Test test = new Test(); // TestParents static property // TestParents property // TestParents static // TestParents constructor // Test static property // Test static // Test property // Test constructor } } class TestParents { static String staticProperty = print("TestParents static property"); String property = print("TestParents property"); { System.out.println("TestParents static"); } public TestParents() { System.out.println("TestParents constructor"); } static String print(String str) { System.out.println(str); return str; } }
static
属性static
属性 >> 构造方法;即代码块和非static
属性按照代码中顺序排序,构造函数在最后面public Test() { staticProperty = print("Test static property"); System.out.println("Test static"); property = print("Test property"); System.out.println("Test constructor"); }
优先选择组合和代理,必要时才使用继承。内存