Python 文档说,如果对象的引用计数为零,则认为该对象已删除,并且del和__del__之间的区别在于:
这意味着如果我们一直删除对对象的引用,直到引用计数为零。它会执行__del__来删除对象以释放内存。
然而,让我烦恼的是:
如果我创建一个对象并将该对象绑定到名称ObjectX。它会将引用计数增加 1,对吗?
如果我创建一个名为列表ListY和我追加ListY 与目标x。这意味着它将引用计数增加到 2,对吗?
现在,如果我删除ObjectX和ListY,对象的引用计数是否仍然保持为 1 并且对象仍然坐在硬盘中等待某种灵魂来杀死它?
欢迎任何意见和想法......
顺便说一句,如果发生的事情真的是我对在 Python 中删除对象的理解。关于如何删除附加到列表中的对象并释放内存空间有什么好的建议吗?
class Slave:
def __init__(self):
self.name=""
def __del__(self):
print("SLAVE DEAD")
def sequence():
SlaveX=Slave()
slaveList=list()
slaveList.append(SlaveX)
del SlaveX
print("I AM NOT DEAD YET")
del slaveList
Run Code Online (Sandbox Code Playgroud) 我尝试在 Android 中使用RecyclerView创建一个列表。但是,我不明白为什么 RecyclerView无法正确加载数据。我尝试将我的代码与在线可用的示例进行比较,但我无法分辨示例代码和我的代码之间有什么区别。
任何人都可以帮助指出我的代码缺少什么或有什么问题吗?
活动主文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="daozui.assignment3_task3.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/RecyclerView"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
contact_arrangement.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/contactIcon_ID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/man"
android:layout_margin="10dp"/>
<TextView
android:id="@+id/contactName_ID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="30sp"
android:text="Name"
android:layout_toEndOf="@id/contactIcon_ID"
android:layout_marginTop="25dp"
android:layout_marginStart="25dp"/>
<TextView
android:id="@+id/contactRelationship_ID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFBEB9B9"
android:textSize="20sp"
android:text="Relationship"
android:layout_alignStart="@id/contactName_ID"
android:layout_below="@id/contactName_ID"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
MainActivity.Java
package daozui.assignment3_task3;
import android.app.ListFragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List; …Run Code Online (Sandbox Code Playgroud)