标签: polymorphism

C ++中的子类型多态性

我在 C++ 中有这个程序。

ref2.h :

#ifndef REF2_H
#define REF2_H
#include <iostream>
using std::cout;
using std::endl;

int   add_int_int(int a, int b) {return (a+b);}

class IntClass;

class Number {
    public:

        //return a Number object that's the results of x+this, when x is IntClass
        virtual Number& addInt(IntClass& x) = 0;

        //Print the number stored in the object
        virtual void print_number() = 0;
    }

class IntClass : public Number {

    private:
        int my_number;

    public:
        //Constructor
        IntClass(int n):my_number(n) {}

        //returns the number stored in the …
Run Code Online (Sandbox Code Playgroud)

c++ polymorphism subtype

0
推荐指数
1
解决办法
1013
查看次数

通过继承和接口实现 Python 多态

我正在使用 Python 2.7(谷歌应用引擎)

学习了Python,我已经弄清楚语言是如何使用继承来实现多态的。

INHERITANCE基于多态性在PYTHON

class pet:
  number_of_legs = None
  age = None

  def sleep(self):
    print "zzz"

  def count_legs(self):
    print "I have %s legs." % self.number_of_legs
    return self.number_of_legs


class dog(pet):
  def bark(self):
    print "woof"

doug = dog()

doug.number_of_legs = 5;

print "Doug has %s legs." % doug.count_legs()
doug.bark()
Run Code Online (Sandbox Code Playgroud)

我的问题是:Python 是否具有松散耦合的多态性,即使用interface而不是inheritance. 例如,在 Java 中,没有祖先关系的类可以实现相同的接口。见HouseDogPetRock下面的例子。

INTERFACE基于多态性在JAVA

class Animal{
   int legs;
   double weight;

   public int getLegs(){return this.legs;} …
Run Code Online (Sandbox Code Playgroud)

python java polymorphism interface

0
推荐指数
1
解决办法
1835
查看次数

在java中传递后代而不是接口

我在 Java 中实现了两种类型的列表,一种只是标签列表,另一种在每行上提供两个按钮。

我创建了一个名为 IListView 的接口,它包含以下方法:

public void setDataSource(IListViewDataSource dataSource);
Run Code Online (Sandbox Code Playgroud)

问题在于,在创建第二种类型的列表时,它使用另一种称为IComplexListViewDataSource扩展前一种类型的数据源,并像这样实现前一种方法:

public void setDataSource(IComplexListViewDataSource dataSource);
Run Code Online (Sandbox Code Playgroud)

IntelliJ 告诉我我没有覆盖IListView接口中的方法,即使IComplexListViewDataSourceextends IListViewDataSource.

如果我是对的,如果我处理的是类而不是接口,这将毫无问题地工作。这是怎么回事,我怎样才能让它发挥作用?

java polymorphism interface

0
推荐指数
1
解决办法
181
查看次数

抽象方法中的多态性和检查异常抛出

假设我们有一个Foo包含一个方法的类,该方法可能会抛出IOException.

public abstract class Foo {
    public abstract void foo() throws IOException;
}
Run Code Online (Sandbox Code Playgroud)

但是,当我FooImpl像这样扩展该类时:

public class FooImpl extends Foo {
    public void foo() throws Exception {
        throw new Exception("This cannot throw!");
    }
}
Run Code Online (Sandbox Code Playgroud)

这会导致编译时错误:“覆盖的方法不会抛出 java.lang.Exception”

我理解这是因为覆盖方法“可能只抛出其父方法的已检查异常,以及任何未检查的异常”

为什么会这样呢?为什么我们要禁止自己抛出更广泛的异常?

(我也在寻找解决方法)

谢谢!

java polymorphism

0
推荐指数
1
解决办法
312
查看次数

让成员函数返回类型成为要返回的实际对象的超类的正确方法是什么?

问题

让成员函数返回类型成为您要返回的实际对象的超类的正确方法是什么?

问题

Clang tidy 警告我返回subClassAsubClassB丢弃我对myMethod. 我也没有得到任何输出 - 大概是因为我不小心丢弃了覆盖的myMethod.

编码

#include <iostream>

using namespace std;

class SuperClass {
public :
    SuperClass() {
        cout << "I'm the superclass" << endl;
    };

    virtual std::string myMethod();
};

class SubClassA : public SuperClass {
public:
    SubClassA() : SuperClass() {
        cout << "I'm subclass A" << endl;
    }

    std::string myMethod() override {
        return "A";
    }
};

class SubClassB : public SuperClass {
public:
    SubClassB() : SuperClass() { …
Run Code Online (Sandbox Code Playgroud)

c++ oop polymorphism

0
推荐指数
1
解决办法
63
查看次数

在 Laravel 中更新一对一多态的最佳方法是什么?

我有一对一的多态,我想找出更新现有关系的最佳方式。

class Image extends Model
{
    /**
     * Get the owning imageable model.
     */
    public function imageable()
    {
        return $this->morphTo();
    }
}

class Post extends Model
{
    /**
     * Get the post's image.
     */
    public function image()
    {
        return $this->morphOne('App\Image', 'imageable');
    }
}

class User extends Model
{
    /**
     * Get the user's image.
     */
    public function image()
    {
        return $this->morphOne('App\Image', 'imageable');
    }
}
Run Code Online (Sandbox Code Playgroud)

PostController 中的更新方法

public function update(Request $request, $id)
{
    $post       = Post::findOrFail($id);
    $post->image()->delete();
    $post->image()->save(new Image([
        'url'=> $request->input('image_url), …
Run Code Online (Sandbox Code Playgroud)

polymorphism one-to-one relationship laravel

0
推荐指数
1
解决办法
437
查看次数

通过私有继承覆盖公共别名方法

我有一个类层次结构,其中有一个Base带有实现列表的基类型和另一个基类,AnotherBase几乎相同,Base但略有不同。为了用语言表达这一点,我在第二个基类上使用了私有继承(因此后者与前者的实现之间没有原样的关系)。

假设这是代码(https://wandbox.org/permlink/2e2EG0eKmcLiyvgt

#include <iostream>

using std::cout;
using std::endl;

class Base {
public:
    virtual ~Base() = default;
    virtual void foo() = 0;
};

class Impl : public Base {
public:
    void foo() {
        cout << "Impl::foo()" << endl;
    }
};

class AnotherBase : private Base {
public:
    using Base::foo;

    // other virtual methods
};

class Derived : public AnotherBase {
public:
    explicit Derived(std::unique_ptr<Base> base) : base_{std::move(base)} {}

    void foo() override {
        base_->foo();
    } …
Run Code Online (Sandbox Code Playgroud)

c++ polymorphism inheritance private-inheritance c++17

0
推荐指数
1
解决办法
36
查看次数

Python 类多态性

我正在练习多态,以下代码返回错误

class Animal:
    def talk(self, something):
        print(something)

class Dog(Animal):
    def talk(self):
        super().talk("woof woof")

Bonny = Dog
Bonny.talk()
Run Code Online (Sandbox Code Playgroud)
TypeError: talk() missing 1 required positional argument: 'self'
Run Code Online (Sandbox Code Playgroud)

根据我学到的,邦尼应该是自我论证,所以为什么自我缺失?

python polymorphism class-method

0
推荐指数
1
解决办法
122
查看次数

如何为某些特定的泛型类型做 if-else?

我有MyClass并且我想同时调用mySubMethod(key)mySubMethod2(key)整数,但只调用mySubMethod2(key)非整数。

我试过下面的代码,但不起作用。我也试图改变K == int别的东西,例如K == IntegerK.equals(int)K.equals(Integer)等,但他们都没有工作。我该如何解决?谢谢。

public class MyClass<K,V> {

    public boolean myMethod(K key) {
        if (K == int) {
            mySubMethod(key);
        }else {
            // do nothing
        }
        mySubMethod2(key);
        return false;
    }

    public void mySubMethod(K key) {
        /** something */ 
    }

    public void mySubMethod2(K key) {
        /** something */ 
    }

}
Run Code Online (Sandbox Code Playgroud)

java generics polymorphism casting generic-programming

0
推荐指数
1
解决办法
56
查看次数

如何在 Haskell 中组合多态函数?

我有以下文件:

module SimpleComposition where

class Domain a where
    f :: a -> a
    g :: a -> a

h = f . g
Run Code Online (Sandbox Code Playgroud)

尝试在 ghci 中加载它时,出现错误

src\play.hs:7:5: error:
    * No instance for (Domain c0) arising from a use of `f'
    * In the first argument of `(.)', namely `f'
      In the expression: f . g
      In an equation for `h': h = f . g
Run Code Online (Sandbox Code Playgroud)

我认为问题在于fand的类型gisforall a. Domain a => a ->a而不仅仅是a -> …

polymorphism haskell composition

0
推荐指数
1
解决办法
64
查看次数