java编程思想笔记--类型信息部分一

来源:互联网 发布:淘宝微商代理能挣钱吗? 编辑:程序博客网 时间:2024/05/22 09:43

1.数组这种容器,将所有的事物都看做Object来持有,当从中取出元素时,会自动转型为相应类型。

2.在java中,所有的类型转换都是在运行时进行类型转换的正确性检查的。

3.RTTI:run-time type identification.RTTI是由类中的一个叫做Class对象的特殊对象来完成的。它包含了与类有关的信息。事实上,class对象就是用来创建类的所有常规对象的。

4.每个类都有一个Class对象。换言之,每当编写一个新类,这个新类中就会产生一个Class对象(被保存在一个同门的字节码文件中)。为了生成这个类的对象,允许这个程序的java虚拟机将使用称为类加载器的子系统。

5.类加载器是jvm的一部分。所有的类都是在第一次使用时,被加载到类加载器中的。当程序创建第一个对类的静态成员的引用时,就会加载这个类。这个证明构造器也是类的静态方法,即使在构造器之前都没有使用static关键字。因此,使用new操作符来创建类的新对象也被当做是对类的静态成员的引用。

6.java程序在它开始运行前并非完全加载,其各个部分是在必需时才加载的。动态加载使能的行为,在c++这样的静态语言中很难或者根本不可能复制。

7.类加载器首先检查这个类的Class对象是否已经加载。如果尚未加载,默认的类加载器就会根据类名查找.class文件。一旦某个类的Class对象被载入内存,它就被用来创建这个类的所有对象。

8.Class对象只在需要的时候才被加载,static初始化是在类加载时进行的。

9.关于Class.forName("A");这个方法是Class类的一个static成员。Class对象就和其他对象一样,我们可以获取并操作它的引用。forName()是取得Class对象的引用的一种方法。对forName()的调用是为了它产生的副作用:如果类还没有被加载就加载它。在加载的过程中,该类的static字句被执行。

package com.wang.test;import java.util.*;abstract class Shape {  void draw() { System.out.println(this + ".draw()"); }  abstract public String toString();}class Circle extends Shape {  public String toString() { return "Circle"; }}class Square extends Shape {  public String toString() { return "Square"; }}class Triangle extends Shape {  public String toString() { return "Triangle"; }}public class Shapes {  public static void main(String[] args) {    List<Shape> shapeList = Arrays.asList(      new Circle(), new Square(), new Triangle()    );    for(Shape shape : shapeList)      shape.draw();  }} /* Output:Circle.draw()Square.draw()Triangle.draw()*///:~

上面这个类的父类方法中的打印的this,实质上因为子类复写了父类中的方法,因此,调用的是子类中的toString方法。


package com.wang.test;//: typeinfo/SweetShop.java//Examination of the way the class loader works.class Candy {static { System.out.println("Loading Candy"); }}class Gum {static { System.out.println("Loading Gum"); }}class Cookie {static { System.out.println("Loading Cookie"); }}public class SweetShop {public static void main(String[] args) {System.out.println("inside main"); new Candy(); System.out.println("After creating Candy"); try {   Class.forName("Gum"); } catch(ClassNotFoundException e) { System.out.println("Couldn't find Gum"); } System.out.println("After Class.forName(\"Gum\")"); new Cookie(); System.out.println("After creating Cookie");}} /* Output:inside mainLoading CandyAfter creating CandyLoading GumAfter Class.forName("Gum")Loading CookieAfter creating Cookie*///:~


原创粉丝点击