Protected constructor vs abstract class

조회 수: 7 (최근 30일)
behzad abdollahikhameneh
behzad abdollahikhameneh 2018년 3월 16일
편집: per isakson 2018년 3월 17일
Is there a difference between
1. Define constructor as Protected.
2. Define a class "Abstract" to avoid direct instantiation (only subclasses can be instantiated)
Example:
classdef (Abstract) TestInterface < handle
properties
test;
end
methods (Abstract)
testMethod(pObj);
testMethod2(pObj);
end
end
classdef TestImpl < TestInterface %%%%%%%%%%%%%VARIATION 1
methods
function testMethod(pObj)
end
end
methods(Access = 'protected')
function rObj = TestImpl()
rObj.test = 1;
end
end
end
classdef (Abstract) TestImpl < TestInterface %%%%%%%%%%%%VARIATION 2
methods
function rObj = TestImpl()
rObj.test = 1;
end
function testMethod(pObj)
end
end
end
classdef TestImpl2 < TestImpl
methods
function rObj = TestImpl2()
rObj = rObj@TestImpl();
end
function testMethod(pObj)
end
function testMethod2(pObj)
end
end
end
  댓글 수: 1
per isakson
per isakson 2018년 3월 17일
편집: per isakson 2018년 3월 17일
  • "Define constructor as Protected [or private]." doesn't make the class abstract. Combined with static methods it may be used to implement the Singleton Pattern.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 C++ Data and Function Interfaces에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by