简体   繁体   English

输入参数不足继承Matlab

[英]Not enough input arguments Inheritance Matlab

I'm working on classes and their multiple inheritance, I have a problem which I couldn't solve after so many help, I have a class A which is base class and class B , which is its derive class. 我正在研究类及其多重继承,我遇到了一个问题,经过这么多帮助我无法解决,我有一个类A ,它是基类,而类B是它的派生类。 What I want is, the Constructor of class A with input arguments is to be calling in derived class B ,supposed to be called in class B with its input arguments But unfortunately i have error Not enough input arguments. 我想要的是,带有输入参数的类A的构造方法是在派生类B调用,应该在类B使用其输入参数来调用,但是不幸的是,我有错误输入参数不足。 as Class B is expecting arguments but I want to give arguments as input in Class B like above. 因为B类需要参数,但我想像上面那样在B类中提供参数作为输入。

What solution is to be suggested or appropriate in my case? 在我的情况下,建议或合适的解决方案是什么?

My Code: (Base Class A) 我的代码:(基本类A)

classdef A %base class
        properties 
            arg1
        end
        properties 
           out
        end
        methods 
        function  obj = A(arg1)
            obj.arg1=arg1;
            obj.out=[1 2 3;];
        end

end

end

Derived Class B: 衍生的B类:

classdef B < A %derived Class
        properties (Access=protected)
            arg2
            obj1
        end

        methods 
        function obj1 = B(arg2)
        obj1.arg2=arg2;
 A(obj1);
        end

        end
end

Call the constructor of superclass A like this: 像这样调用超类 A构造函数

Bm : Bm

classdef B < A %derived Class
    properties (Access=protected)
        arg2
    end

    methods
        function obj = B(arg1,arg2)
            obj = obj@A(arg1);
            obj.arg2 = arg2;
        end
    end
end

From the documentation : 文档中

By default, MATLAB calls the superclass constructor without arguments. 默认情况下,MATLAB调用不带参数的超类构造函数。 If you want the superclass constructor called with specific arguments, explicitly call the superclass constructor from the subclass constructor. 如果要使用特定参数调用超类构造函数,请从子类构造函数中显式调用超类构造函数。 The call to the superclass constructor must come before any other references to the object. 对超类构造函数的调用必须先于对该对象的任何其他引用。

在此处输入图片说明

Don't forget to clear all instances of both classes before trying new code, and clear Am Bm just for good measure. 在尝试新代码之前,请不要忘记清除两个类的所有实例,并clear Am Bm只是为了很好。

Usage : 用法

>> myA = A(1)
myA = 
  A with properties:

    arg1: 1
     out: [1 2 3]
>> myB = B(1,2)
myB = 
  B with properties:

    arg1: 1
     out: [1 2 3]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM