Java и C # -подобные свойства

Вот мое предложение в C ++

Я попытался наложить как минимальное ограничение на тип итератора, так как я мог так это решение предполагал просто итератор вперед, и он может быть const_iterator. Это должно работать с любым стандартным контейнером. В тех случаях, когда аргументы не имеют смысла, это вызывает std :: invalid_argumnent

#include <vector>
#include <stdexcept>

template <typename Fci> // Fci - forward const iterator
std::vector<std::vector<Fci> >
enumerate_combinations(Fci begin, Fci end, unsigned int combination_size)
{
    if(begin == end && combination_size > 0u)
        throw std::invalid_argument("empty set and positive combination size!");
    std::vector<std::vector<Fci> > result; // empty set of combinations
    if(combination_size == 0u) return result; // there is exactly one combination of
                                              // size 0 - emty set
    std::vector<Fci> current_combination;
    current_combination.reserve(combination_size + 1u); // I reserve one aditional slot
                                                        // in my vector to store
                                                        // the end sentinel there.
                                                        // The code is cleaner thanks to that
    for(unsigned int i = 0u; i < combination_size && begin != end; ++i, ++begin)
    {
        current_combination.push_back(begin); // Construction of the first combination
    }
    // Since I assume the itarators support only incrementing, I have to iterate over
    // the set to get its size, which is expensive. Here I had to itrate anyway to  
    // produce the first cobination, so I use the loop to also check the size.
    if(current_combination.size() < combination_size)
        throw std::invalid_argument("combination size > set size!");
    result.push_back(current_combination); // Store the first combination in the results set
    current_combination.push_back(end); // Here I add mentioned earlier sentinel to
                                        // simplyfy rest of the code. If I did it 
                                        // earlier, previous statement would get ugly.
    while(true)
    {
        unsigned int i = combination_size;
        Fci tmp;                            // Thanks to the sentinel I can find first
        do                                  // iterator to change, simply by scaning
        {                                   // from right to left and looking for the
            tmp = current_combination[--i]; // first "bubble". The fact, that it's 
            ++tmp;                          // a forward iterator makes it ugly but I
        }                                   // can't help it.
        while(i > 0u && tmp == current_combination[i + 1u]);

        // Here is probably my most obfuscated expression.
        // Loop above looks for a "bubble". If there is no "bubble", that means, that
        // current_combination is the last combination, Expression in the if statement
        // below evaluates to true and the function exits returning result.
        // If the "bubble" is found however, the ststement below has a sideeffect of 
        // incrementing the first iterator to the left of the "bubble".
        if(++current_combination[i] == current_combination[i + 1u])
            return result;
        // Rest of the code sets posiotons of the rest of the iterstors
        // (if there are any), that are to the right of the incremented one,
        // to form next combination

        while(++i < combination_size)
        {
            current_combination[i] = current_combination[i - 1u];
            ++current_combination[i];
        }
        // Below is the ugly side of using the sentinel. Well it had to haave some 
        // disadvantage. Try without it.
        result.push_back(std::vector<Fci>(current_combination.begin(),
                                          current_combination.end() - 1));
    }
}
16
задан Yves M. 8 October 2014 в 09:47
поделиться

5 ответов

Нет

У вас нет понятия «Свойства» в языке Java. Вам нужно использовать геттеры и сеттеры ..

18
ответ дан stiank81 8 October 2014 в 09:47
поделиться

Как говорили другие, ни одна Java не обладает свойствами как таковыми. Принятым методом является использование геттеров и сеттеров. Кроме того, Delphi поддерживает свойства, очень похожие на C #.

Определенно ничего подобного AutoProperties не поддерживается в Java. Кажется, я не могу найти упоминаний о какой-либо поддержке в будущем.

4
ответ дан Bryce Fischer 8 October 2014 в 09:47
поделиться

В последний раз, когда я проверял, Java не поддерживала свойства.

2
ответ дан 30 November 2019 в 21:10
поделиться

Свойства C # похожи на свойства Delphi по той простой причине, что они были вдохновлены свойствами Delphi. Переезд одного из ключевых сотрудников команды Delphi Borland в Microsoft и (помощь) в разработке C #, конечно же, ничего не дал ничего общего с этим :)

В прошлом году было предложение (фактически несколько) о добавлении свойств стиля C # в Java, но они были фактически отвергнуты сообществом как простой синтаксический сахар без реальной добавленной стоимости.

0
ответ дан 30 November 2019 в 21:10
поделиться

Java не поддерживает свойства, как C#. Вам нужны геттеры и сеттеры, но будьте осторожны, чтобы случайно не дать пользователю доступ к инкапсулированным данным, возвращая ссылку на объект вместо ссылки на его копию.

4
ответ дан 30 November 2019 в 21:10
поделиться