1. Python / Говнокод #25986

    +1

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    class Container:
        def __init__(self, liquid):
            self.liquid = liquid
    
        def look_inside(self):
            return f"{self.liquid} in container"
    
        @classmethod
        def create_with(cls, liquid):
            return cls(liquid)
    
    
    class Bottle(Container):
        def look_inside(self):
            return f"bottle full of {self.liquid}"
    
    
    class Glass(Container):
        def look_inside(self):
            return f"A glass of {self.liquid}"
    
    
    for c in (c.create_with("beer") for c in [Glass, Bottle]):
        print(c.look_inside())

    ми маємо class polymorphism

    Запостил: DypHuu_niBEHb, 23 Октября 2019

    Комментарии (8) RSS

    • SEO: class polymorphism
      Ответить
    • <?php
      
      class Container {
          public $liquid;
      
          function __construct($liquid) {
              $this->liquid = $liquid;
          }
      
          function look_inside() {
              return "{$this->liquid} in container";
          }
      
          static function create_with($cls, $liquid) {
              return new $cls($liquid);
          }
      }
      
      class Bottle extends Container {
          function look_inside() {
              return "bottle full of {$this->liquid}";
          }
      }
      
      class Glass extends Container {
          function look_inside() {
              return "A glass of {$this->liquid}";
          }
      }
      
      foreach(['Glass', 'Bottle'] as $c) {
          echo Container::create_with($c, 'beer')->look_inside() . PHP_EOL;
      }


      https://ideone.com/kvNwig

      Вместо метода create_with можно вообще определить __toString, тогда в цикле достаточно будет echo Container::create_with($c, 'beer').
      Ответить
      • https://wandbox.org/permlink/WLtO0L7QL6rhtvIs
        #include <string>
        #include <iostream>
        #include <tuple>
        
        struct Container {
          std::string look_inside() const {
            return liquid + " in container";
          }
          
          template <typename Cont>
          static Cont create_with(std::string liquid) {
            return Cont{liquid};
          }
        
          std::string liquid;
        };
        
        struct Bottle : Container {
          std::string look_inside() const {
            return "bottle full of " + liquid;
          }
        };
        
        struct Glass : Container {
          std::string look_inside() const {
            return "A glass of " + liquid; 
          }
        };
        
        template <typename T>
        struct Type {
          using type = T;
        };
        
        int main() {
          std::apply([](auto... metatypes) {
            auto print = [](auto metatype) {
              using type = typename decltype(metatype)::type;
              std::cout << type::template create_with<type>("beer").look_inside() << '\n';
            };
            (print(metatypes), ...);
          }, std::tuple{Type<Glass>{}, Type<Bottle>{}});
        }
        Ответить
        • Нужно сделать максимально constexpr.
          Ответить
          • boost::beast::static_string не умеет в constexpr, но clang порешал: https://godbolt.org/z/OLiDt7
            main:                                   # @main
                    sub     rsp, 40
                    mov     qword ptr [rsp], 15
                    lea     rdi, [rsp + 8]
                    movabs  rax, 2338339493803139137
                    mov     qword ptr [rsp + 8], rax
                    movabs  rax, 8243106165096345376
                    mov     qword ptr [rsp + 15], rax
                    mov     byte ptr [rsp + 23], 0
                    call    puts
                    mov     qword ptr [rsp], 19
                    lea     rdi, [rsp + 8]
                    movabs  rax, 7358993307608051554
                    mov     qword ptr [rsp + 8], rax
                    movabs  rax, 2334675641886864742
                    mov     qword ptr [rsp + 15], rax
                    mov     dword ptr [rsp + 23], 1919247714
                    mov     byte ptr [rsp + 27], 0
                    call    puts
                    xor     eax, eax
                    add     rsp, 40
                    ret


            ---
            #include <string_view>
            #include <tuple>
            #include <cstdio>
            
            #include <boost/beast/core/static_string.hpp>
            
            using boost::beast::static_string;
            
            struct Container {
              auto look_inside() const {
                return static_string<30>(liquid) += " in container";
              }
              
              template <typename Cont>
              constexpr static Cont create_with(std::string_view liquid) {
                return Cont{static_string<30>{liquid.data(), liquid.size()}};
              }
            
              static_string<10> liquid;
            };
            
            struct Bottle : Container {
              auto look_inside() const {
                return static_string<30>("bottle full of ") += liquid;
              }
            };
            
            struct Glass : Container {
              auto look_inside() const {
                return static_string<30>("A glass of ") += liquid; 
              }
            };
            
            template <typename T>
            struct Type {
              using type = T;
            };
            
            int main() {
              std::apply([](auto... metatypes) {
                auto print = [](auto metatype) {
                  using type = typename decltype(metatype)::type;
                  printf("%s\n", type::template create_with<type>("beer").look_inside().c_str());
                };
                (print(metatypes), ...);
              }, std::tuple{Type<Glass>{}, Type<Bottle>{}});
            }
            Ответить
    • А де, власне, лайно?
      Ответить

    Добавить комментарий