classes can be instantiated explicitly or implicitly 明确和隐藏建立对象

来源:互联网 发布:ae cc 2017 mac 中文 编辑:程序博客网 时间:2024/06/07 01:02

一、明确建立对象4种方法

      1、 new   :Example4 obj1 = new Example4("Created with new.");

      2、用一个类对象myClass 调用 newInstance()  

          Class myClass = Class.forName("Example4"); 

        Example4 obj2 = (Example4)myClass.newInstance();

     3、  Class myClass1=Example4.class;
  Example4 obj3=(Example4)myClass1.newInstance();

      4、by invoking clone() on any existing object.           

      Example4 obj4 = (Example4) obj2.clone();

           这个克隆的对象和原来的对象一样,有同样的数据和方法。

          1》如果一个类需要克隆对象,必须实现空接口Cloneable ,编译器编译时检查。

              空接口Cloneable,没有什么用(只是标记),表明实现Cloneable 的所有类和接口是一种类型。

         2》 obj2.clone():必须指定返回类型Example4,如果没有指定,哪么会重新建立一个Example4 的新对象

         3》obj2.clone():在父类声明是本地方法,所以不需要方法体,当我们执行方法时,jvm自动去调用操作系统的下的方法。

         
public class Example4 implements Cloneable {
Example4() {
System.out.println("Created by invoking newInstance()");
}
Example4(String msg) {
System.out.println(msg);
}
public static void main(String[] args) throws ClassNotFoundException,
InstantiationException, IllegalAccessException,
CloneNotSupportedException {
  //new
 Example4 obj1 = new Example4("Created with new.");   
 //包的全名
Class myClass = Class.forName("jvm.ex4.Example4");
Example4 obj2 = (Example4)myClass.newInstance();//调用Example4()

 Class myClass1=Example4.class;
  Example4 obj3=(Example4)myClass1.newInstance();

// Make an identical copy of the the second Example4 object
Example4 obj4 = (Example4) obj2.clone();
}
}

二、隐藏建立对象

     1、命令行传入参数:main(String[] arg),

           jvm 会建立 String [] 对象来保存命令行参数,然后把对象的地址给main 里面的方法参数 arg=实际对象的地址。

     2、类装载(被使用后,入内存)时,隐藏建立对象。
   1> First, for every type a Java Virtual Machine loads, it implicitly instantiates a new Class object to represent that type。

     当一个类入内存,就建立一个Class 的对象。

   2> 当一个类定义literal 值时,会建立相应的字符串对象

       如:对象String literal testLiteral,会建立一个String对象

          但是tes保存在常量池中,不需要建立对象。

  public class TestCon {
private String testLiteral="hello";
public final static String tes="iamcon";
 
  public static void main(String[] args) {
  TestCon t=new TestCon();
  }
 }

     3、process of evaluating an expression that involves the string concatenation operator

          求字符串连接表达式的值的过程

         args[0] + args[1]:如果这种表达式不是编译时常量,哪么需要建立String
            1》建立一个StringBuilder 对象

            2》建立一个String对象取出a[0],

            3》初始化StringBuilder  对象,赋值a[0]

            4》建立一个String对象取出a[1],

            3》调用StringBuilder.append(a[1]) .

          因此两个String对象,一个StringBuilder 对象。

     4、当在一个类中调用别类的方法时: System.out.println():PrintStream
    System 的类常量(连接PrintStream类)   public final static PrintStream out = null,当System进内存后,这个类常量是存在常量池中,值是空,
未和PrintStream 的对象相连,当调用PrintStream  下的方法println时,就开始建立一个PrintStream 对象,哪么System 常量池中的public final static PrintStream out =地址(PrintStream  对象)

package jvm.ex5;

public class Example5 {
public static void main(String[] args) {
System.out.println(args[0] + args[1]);
}
} 


阅读全文
0 0