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自动去调用操作系统的下的方法。
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 对象。
package jvm.ex5;
public class Example5 {
public static void main(String[] args) {
System.out.println(args[0] + args[1]);
}
}
- classes can be instantiated explicitly or implicitly 明确和隐藏建立对象
- 【已解决】classes could not be instantiated:
- Android The following classes could not be instantiated
- 【Android studio】Rendering Problems:classes could not be instantiated 解决方法
- 【Android studio】Rendering Problems:classes could not be instantiated 解决方法
- 解决方案-Rendering Problems-The following classes could not be instantiated:
- android Rendering Problems :The following classes could not be instantiated
- implicitly shared classes
- Android Studio常见错误之:Rendering Problems/The following classes could not be instantiated
- The following classes could not be instantiated: android.support.v7.widget.ActionBarOverLayLayout
- Android Studio 编译布局文件错误之:Rendering Problems The following classes could not be instantiated
- The following classes could not be instantiated: - android.support.v7.widget.Toolbar 解决方法
- The following classes could not be instantiated: - android.support.v7.internal.widget.ActionBarOverl
- classes could not be instantiated: - android.support.v7.internal.widget.ActionBarOverlayLayout
- Android Studio中新建项目提示classes couldn't be instantiated解决方法
- The following classes could not be instantiated: - android.support.v7.widget.Toolbar 解决办法
- Android Studio布局预览出现rendering problems,the following classes could not be instantiated情况解决办法
- [Android小知识]Rendering Problems The following classes could not be instantiated解决一览
- php如何正则提取指定[]中的值
- 浏览器跨域问题解决办法
- Elasticsearch5.0 beta版本安装错误
- 【cmd】查看物理地址(查看mac地址)
- String 类
- classes can be instantiated explicitly or implicitly 明确和隐藏建立对象
- vue的ajax库 ----- vue-resource
- UICollectionView 基础全面解析
- 为什么还要读马克思?后续马克思博客转战博客园更新,周知
- React Native 的图片点击放大效果的组件使用 react-native-zoom-image
- 链表中倒数第k个结点
- 自定义UISearchBar
- c++ .h和.c文件理解
- Java web开发中的各种bean对应的术语(VO,PO,BO,QO, DAO,POJO,DTO)