ITKeyword,专注技术干货聚合推荐

注册 | 登录

class - Not enough input arguments Inheritance Matlab

分享于

2020腾讯云双十一活动,全年最低!!!(领取3500元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1073

【阿里云】双十一活动,全年抄底价,限时3天!(老用户也有),
入口地址https://www.aliyun.com/1111/home

up vote 2 down vote favorite 1 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. 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. as Class B is expecting arguments but I want to give arguments as input in Class B like above. What solution is to be suggested or appropriate in my case? My Code: (Base Class 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: classdef B < A %derived Class

properties (Access=protected)

arg2

obj1

end

methods

function obj1 = B(arg2)

obj1.arg2=arg2;

A(obj1);

end

end

end

matlab class oop inheritance
  |
  this question asked Apr 18 '14 at 19:23 user3396151



 |  1 Answers

up vote 3 down vote ---Accepted---Accepted---Accepted---

Call the constructor of superclass A like this: B.m:

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. 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 A.m B.m just for good measure. 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]


  |
  this answer edited Apr 18 '14 at 19:51 answered Apr 18 '14 at 19:45 chappjc 27.4k 6 53 95 Hey, I understand this very well, but still confusion, I dont want to input arg1 in Class B but wants obj = obj@A(1);. Is there any possibility of this, as my Goal is only multiple calls. –  user3396151 Apr 18 '14 at 19:57 1 If you want to input arg1, then just do what you suggest with obj = obj@A(1);. Just have a literal value in the call to the superclass constructor. Or for better readability, arg1 = 1; obj = obj@A(arg1);. If you can't take an input for the arg, then you need to hard code it as a default somehow. –  chappjc Apr 18 '14 at 19:59



 | 

up vote 2 down vote favorite 1 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 cla

相关阅读排行


相关内容推荐

最新文章

×

×

请激活账号

为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。

您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱

如果您没有收到激活邮件,请注意检查垃圾箱。