object

PHP Object Life Time

梦想与她 提交于 2020-01-13 02:15:10
问题 I am using PHP 5.2. If I new an object at one page, when will this object be destructed? Is the object destructed automatic at the time that user go to another .php page or I need to call __destructor explicitly? 回答1: It will be destructed (unloaded from memory) at the end of the page load, or if you unset all references to it earlier. You will not have to destroy it manually since PHP always cleans up all memory at the end of the script. In fact, you should never call __destruct yourself.

Java 中的泛型

北慕城南 提交于 2020-01-13 01:56:02
泛型的概念 泛型:   泛型是一种末知的数据类型,当我们不知道使用什么数据类型的时候,可以使用泛型   泛型也可以看成是一个变量用来接收数据类型   E e:Element元素   T t:Type类型 是否使用泛型的对比 不使用泛型 /** * 创建集合对象,不使用泛型 * 好处: * 集合不使用泛型,默认的类型就是Object类型,可以存储任意类型的数据。 * 弊端: * 不安全,会引发异常 */ 好处(举例): public class Demo01Generic { public static void main(String[] args) { // 集合不使用泛型 ArrayList arrayList = new ArrayList(); // 向集合中添加数据,用于测试,这里添加了String类型的数据,和int类型的数据 arrayList.add("ABC"); arrayList.add(123); // 使用迭代器遍历集合 // 第一步:获取送代器的实现类对象,并使用Iterator接口接收 Iterator ite = arrayList.iterator(); // 第二步:使用hasNext方法和next方法遍历集合,取出的元素的类型默认是Object类型 while (ite.hasNext()) { System.out.println( ite

ArrayList源码 基本功能分析

人走茶凉 提交于 2020-01-13 01:23:26
ArrayList 继承与实现 空数组 EMPTY_ELEMENTDATA DEFAULTCAPACITY_EMPTY_ELEMENTDATA 异常 常规函数实现 contains indexOf lastIndexOf clone toArray add(int index, E element) remove(Object o) batchRemove(Collection<?> c, boolean complement) 内部类 注意事项 总结 继承与实现 函数名 介绍 AbstractList List接口 继承自Collection,祖父Iterable RandomAccess 标志接口,表明实现的这个接口的List集合是支持快速随机访问的。 Cloneable 接口 覆盖clone函数,能被克隆。 java.io.Serializable 接口 支持序列化,能够序列化传输 空数组   首先,我观察到了。为毛定义了两个空数组呢~( ̄▽ ̄)。 private static final Object [ ] EMPTY_ELEMENTDATA = { } ; private static final Object [ ] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = { } ;   既然是list有空的情况和非空的情况

print object/instance name in python

旧街凉风 提交于 2020-01-13 00:58:33
问题 I was wondering if there is a way to print the object name in python as a string. For example I want to be able to say ENEMY1 has 2 hp left or ENEMY2 has 4 hp left. Is there a way of doing that?\ class badguy: def __init__(self): self.hp = 4 def attack(self): print("hit") self.hp -= 1 def still_alive(self): if self.hp <=0: print("enemy destroyed") else : print (str(self.hp) + " hp left") # creating objects enemy1 = badguy() enemy2 = badguy() enemy1.attack() enemy1.attack() enemy1.still_alive(

print object/instance name in python

非 Y 不嫁゛ 提交于 2020-01-13 00:57:10
问题 I was wondering if there is a way to print the object name in python as a string. For example I want to be able to say ENEMY1 has 2 hp left or ENEMY2 has 4 hp left. Is there a way of doing that?\ class badguy: def __init__(self): self.hp = 4 def attack(self): print("hit") self.hp -= 1 def still_alive(self): if self.hp <=0: print("enemy destroyed") else : print (str(self.hp) + " hp left") # creating objects enemy1 = badguy() enemy2 = badguy() enemy1.attack() enemy1.attack() enemy1.still_alive(

StringBuffer 和 StringBuilder的区别

让人想犯罪 __ 提交于 2020-01-12 23:35:14
一个线程安全,一个不安全 不断调用toString方法,buffer不回重复new一个对象,但是builder会。 @Test public void testBuffer() throws Exception { StringBuffer buffer = new StringBuffer("aaa"); String s1 = buffer.toString(); String s2 = buffer.toString(); String s3 = buffer.toString(); Field value = String.class.getDeclaredField("value"); value.setAccessible(true); char[] o1 = (char[]) value.get(s1); char[] o2 = (char[]) value.get(s2); char[] o3 = (char[]) value.get(s3); System.out.println((Object) o1); System.out.println((Object) o2); System.out.println((Object) o3); } // [C@6996db8 // [C@6996db8 // [C@6996db8 @Test public void

How to deserialize byte[] into generic object to be cast at method call

半世苍凉 提交于 2020-01-12 22:27:16
问题 I am working on an object encryption class. I have everything worked out but I want to be able to encrypt/decrypt any object type with one deserialize method. As of now the only thing holding me up is the deserialize method. I have the function returning type(object) in the hopes of returning a weak typed object. It works as long as I cast the type during the return value assignment, but If I deserialize into type 'object' the result is byte[]. This means that I have to write a deserialize

Java 反射 Method的invoke回调调用任意方法

僤鯓⒐⒋嵵緔 提交于 2020-01-12 19:24:19
Java 反射 Method的invoke回调调用任意方法 @author ixenos 关键子:Method、Field、invoke方法指针/函数指针、回调函数 invoke回调流程示例 0. 由Class对象动态构造对应类型对象 1. Class对象的getMethod方法,由方法名和形参构造Method对象 2. Method对象的 invoke 方法来 委托 动态构造的对应类型对象,使其执行对应形参的add方法,这是 回调函数(方法) 的功能 “回调函数就是一个通过函数指针调用的函数。如果你把函数的指针(地址)作为参数传递给另一个函数,当这个指针被用来调用其所指向的函数时,我们就说这是回调函数。 回调函数不是由该函数的实现方直接调用,而是在特定的事件或条件发生时由另外的一方调用的,用于对该事件或条件进行响应 。” //动态构造InvokeTest类的实例Class<?> classType = InvokeTest.class; Object invokeTest = classType.newInstance(); //动态构造InvokeTest类的add(int num1, int num2)方法,标记为addMethod的Method对象 Method addMethod = classType.getMethod("add", new Class[]{int

Java Immutable Objects [closed]

守給你的承諾、 提交于 2020-01-12 19:02:06
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 5 years ago . I am learning the concept of immutability. I understand that immutable objects cannot change their values once the object is created. But I didn't understand the following uses of immutable objects. They are are automatically thread-safe and have no synchronization issues. How ?

Spring data jpa 返回map 结果集

送分小仙女□ 提交于 2020-01-12 18:49:06
在JPA 2.0 中我们可以使用entityManager.createNativeQuery()来执行原生的SQL语句。 但当我们查询结果没有对应实体类时,query.getResultList()返回的是一个List<Object[]>。也就是说每行的数据被作为一个对象数组返回。 常见的用法是这样的: public void testNativeQuery(){ Query query = entityManager.createNativeQuery("select id, name, age from t_user"); List rows = query.getResultList(); for (Object row : rows) { Object[] cells = (Object[]) row; System.out.println("id = " + cells[0]); System.out.println("name = " + cells[1]); System.out.println("age = " + cells[2]); } } 这样用会使代码非常不容易让人理解, 究竟下标为0的元素到底是什么, 不去数查询语句是不知道的,而且一旦查询语句被调整,Java代码也要一起调整。这时候我们想如果返回的是Map的话,用起来会清晰的多。