我有以下代码,它有以下两个问题:
Traceback (most recent call last):
File "C:\Users\v\workspace\first\src\tests.py", line 1, in <module>
class Animal:
File "C:\Users\v\workspace\first\src\tests.py", line 39, in Animal
File "C:\Users\v\workspace\first\src\tests.py", line 31, in main
dog = Animal()
NameError: global name 'Animal' is not defined
Run Code Online (Sandbox Code Playgroud)
这段代码来自一个教程,在教程中它可以正常工作.我有Python 2.7并使用Eclipse的PyDev插件.
class Animal:
__hungry = "yes"
__name = "no name"
__owner = "no owner"
def __init__(self):
pass
def set_owner(self,newOwner):
self.__owner= newOwner
return
def get_owner(self):
return self.__owner
def set_name(self,newName):
self.__name= newName
return
def get_name(self):
return self.__name
def noise(self):
print('errr')
return
def __hiddenmethod(self):
print("hard to …Run Code Online (Sandbox Code Playgroud) 大家好我想为主页编写Django视图,我知道首先我需要在我的项目中创建一个Django应用程序.我知道我应该编写以下内容:$ python manage.py startapp name,在一个coomand行,但我的问题是我不知道我怎么能进入pydev(eclipse).有人知道吗?
$ python manage.py startapp name
filename contains-> __init__
->forms
->models
->tests
->views
Run Code Online (Sandbox Code Playgroud) 我尝试创建一个集合.只是一个有3个名字的小集合,我想按字母顺序排序.并且总是会遇到这个错误:线程"main"中的异常java.lang.AbstractMethodError:collection.compareTo(Ljava/lang/Object;)I.在行中:Collections.sort(名称); 我该怎么做才能克服我的问题?
public class collection implements Comparable<collection> {
private String name;
public collection(String name){
this.name= name;
}
public String getName(){
return name;
}
public int compare??(collection c){
return this.getName().compareTo(c.getName());
}
}
public class collectionList {
private ArrayList <collection> names;
public collectionList(){
names = new ArrayList <collection>();
}
public void populate() {
collection c1 = new collection("Monica Rows");
names.add(c1);
collection c2 = new collection("Peter Walker");
names.add(c2);
collection c3 = new collection("Jack Miller");
names.add(c3);
}
public void sortBy(){
Collections.sort(names);
}
public String …Run Code Online (Sandbox Code Playgroud) 我在教程中观察,我可以用这种方式遍历一个对象数组:
Animals[] an = new Animals[2];
for (Animals a:an){
.
.
}
Run Code Online (Sandbox Code Playgroud)
但现在我想遍历一个二维数组,当我使用这个代码时,我有一个问题(说:需要不兼容的类型:appl1.Animals foundLappl1.Animals []).当我使用这个代码
Animals[][] an = new Animals[2][2];
for (Animals a:an){
.
.
}
Run Code Online (Sandbox Code Playgroud)
有谁知道我怎么能克服这个问题.预先感谢您的帮助.
我有一个数组,我设置这个数组随机值.我希望确保这个数组已满(每个位置都有一个数字).我如何确保这个?我写了下面的代码,但我不确定.
public boolean check(){
boolean checks=false;
int [] array =new int [10];
if(array.length==10){
checks = true;
}
return checks;
}
Run Code Online (Sandbox Code Playgroud) 我有一个2D ArrayList,我想从2D ArrayList中的那些中获取一个特定的ArrayList.
这可能吗?如果是,我该怎么做?2D arrayList究竟是如何工作的,我已经阅读了很多关于它的内容,但我仍然无法理解.我的2D ArrayList有这样的形式:
ArrayList<ArrayList<Items>> arrayList = new ArrayList<ArrayList<Items>>();
for (int i = 0; i < 10; i++) {
ArrayList<Items> row = new ArrayList<Items>();
for (int j = 0; j < 500; j++)
{
// create the items...
}
}
Run Code Online (Sandbox Code Playgroud)