В Python можно ли назвать метод экземпляра класса A, но передать ли в экземпляре класса B?

Создайте один класс pojo, как

class student{
    String id,
    String name,
    String age,
    String titles
}

, теперь замените приведенный ниже код

if(c.moveToFirst()) {
        do {
         titles.add(c.getString(id) + " \t " +  c.getString(name) + " \t " +     c.getString(age));
        } while (c.moveToNext());
        arrayAdapter.notifyDataSetChanged();

     lst1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String tt = titles.get(position).toString();
            Intent i = new Intent(getApplicationContext(),edit.class);
            i.putExtra("age",tt);
            startActivity(i);
        }
    });

на

ArrayList<student> stud = new ArrayList<student>();
if(c.moveToFirst()) {
    do {
     student stu = new student()
     stu.id = c.getString(id);
     stu.name =  c.getString(name);
     stu.age =  c.getString(age);
     //you need to add the Student object stu not the ArrayList Object stud
     stud.add(stu);
     titles.add(c.getString(id) + " \t " +  c.getString(name) + " \t " +     c.getString(age));
    } while (c.moveToNext());
    arrayAdapter.notifyDataSetChanged();

    lst1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String tt = titles.get(position).toString();
            student stu = stud.get(position);
            Intent i = new Intent(getApplicationContext(),edit.class);
            i.putExtra("age",stu.age);
            startActivity(i);
        }
    });
6
задан Jason DeFontes 24 April 2009 в 05:33
поделиться

3 ответа

Выглядит так:

Foo.hello.im_func(bar)

Привет, я Бар.

Полагаю, мне нужно прочитать этот немного сложнее. ..

9
ответ дан 8 December 2019 в 14:47
поделиться

It happens because python wraps class functions as an "unbound method" which performs this type checking. There's some description of the decisions involved in this here.

Note that this type checking has actually been dropped in python 3 (see the note at the end of that article), so your approach will work there.

5
ответ дан 8 December 2019 в 14:47
поделиться

Некоторое время назад я размышлял о той же «функции» в Perl на PerlMonks, и общий консенсус заключался в том, что, пока он работает (как в Python), вы не должны делать то, что способ.

0
ответ дан 8 December 2019 в 14:47
поделиться
Другие вопросы по тегам:

Похожие вопросы: