Main Content

Method Attributes

Purpose of Method Attributes

Specifying attributes in the class definition enables you to customize the behavior of methods for specific purposes. Control characteristics like access, visibility, and implementation by setting method attributes. Subclasses do not inherit superclass member attributes.

Specifying Method Attributes

Assign method attributes on the same line as themethodskeyword:

methods(Attribute1=value1,Attribute2=value2,...) ...end

Table of Method Attributes

Attributes enable you to modify the behavior of methods. All methods support the attributes listed in the following table.

Attribute values apply to all methods defined within themethods...endcode block that specifies the nondefault values.

Method Attributes

Attribute Name

Class

Description

Abstract

logicalDefault =false

Iftrue, the method has no implementation. The method has a syntax line that can include arguments that subclasses use when implementing the method:

  • Subclasses are not required to define the same number of input and output arguments. However, subclasses generally use the same signature when implementing their version of the method.

  • The method does not containfunctionorendkeywords, only the function syntax (for example,[a,b] = myMethod(x,y)).

  • The method can include comments after the signature line.

Access

  • enumeration, default =public

  • meta.classobject

  • cell array ofmeta.class对象

Determines what code can call this method:

  • public— Unrestricted access

  • protected— Access from methods in class or subclasses

  • private— Access by class methods only (not from subclasses)

  • List classes that have access to this method. Specify classes asmeta.class对象in the form:

    • A singlemeta.classobject

    • A cell array ofmeta.class对象. An empty cell array,{}, is the same asprivateaccess.

    SeeClass Members Access

Hidden

logicalDefault =false

Whenfalse, the method name shows in the list of methods displayed using themethodsormethodsviewcommands. If set totrue, the method name is not included in these listings andismethoddoes not returntruefor this method name.

Sealed

logicalDefault =false

Iftrue, the method cannot be redefined in a subclass. Attempting to define a method with the same name in a subclass causes an error.

Static

logicalDefault =false

Specify astrueto define a method that does not depend on an object of the class and does not require an object argument. Use the class name to call the method:classname.methodnameor an instance of the class:obj.methodname

Static Methodsprovides more information.

Framework attributes

Classes that use certain framework base classes have framework-specific attributes. See the documentation for the specific base class you are using for information on these attributes.

See Also

|

Related Topics