Что является эквивалентом C# PHP's “сам::”?

Я думаю, что это то, что вы хотите

cookie_ids=Cookie.objects.filter(board__pk=self.kwargs['pk']).values_list('id',flat=True)

sugar_ids=Sugar.objects.filter(board__pk=self.kwargs['pk']).values_list('id',Ôflat=True)

ids_list = list(cookie_ids) + list(sugar_ids)
context['total_actions'] = Action.objects.filter(target_id__in=ids_list)
9
задан Edward Tanguay 13 May 2009 в 12:03
поделиться

5 ответов

There's no real equivalent - you have to either specify the class name, i.e.

Customer.DatabaseConnectionExists()

or miss out the qualifier altogether, i.e.

DatabaseConnectionExists()

The latter style of calling is advisable since it's simpler and doesn't lose any meaning. Also, it's more inline with method calling in instances (i.e. calling by InstanceMethod() and not this.InstanceMethod(), which is overly verbose).

15
ответ дан 4 December 2019 в 06:49
поделиться

If you're calling the method from inside the class, you don't need to specify anything like ::Self, just the method name will do.

class Customer
{
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public static Customer GetCurrentCustomer()
    {
        if (DatabaseConnectionExists())
        {
            return new Customer { FirstName = "Jim", LastName = "Smith" };
        }
        else
        {
            throw new Exception("Database connection does not exist.");
        }
    }

    public static bool DatabaseConnectionExists()
    {
        return true;
    }
}
13
ответ дан 4 December 2019 в 06:49
поделиться

Just leave it out. DatabaseConnectionExists is defined inside the class.

4
ответ дан 4 December 2019 в 06:49
поделиться

Just call it without any prefix.

2
ответ дан 4 December 2019 в 06:49
поделиться

No, there isn´t. But with the refactor tools, changing a name of a class should not worry you too much.

0
ответ дан 4 December 2019 в 06:49
поделиться
Другие вопросы по тегам:

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