Up to Python 2.1, old-style classes were the only flavour available to the user.
The concept of (old-style) class is unrelated to the concept of type:
if x
is an instance of an old-style class, then x.__class__
designates the class of x
, but type(x)
is always <type
'instance'>
.
This reflects the fact that all old-style instances, independently of
their class, are implemented with a single built-in type, called
instance.
New-style classes were introduced in Python 2.2 to unify the concepts of class and type.
A new-style class is simply a user-defined type, no more, no less.
If x is an instance of a new-style class, then type(x)
is typically
the same as x.__class__
(although this is not guaranteed – a
new-style class instance is permitted to override the value returned
for x.__class__
).
The major motivation for introducing new-style classes is to provide a unified object model with a full meta-model.
It also has a number of immediate benefits, like the ability to
subclass most built-in types, or the introduction of "descriptors",
which enable computed properties.
For compatibility reasons, classes are still old-style by default.
New-style classes are created by specifying another new-style class
(i.e. a type) as a parent class, or the "top-level type" object if no
other parent is needed.
The behaviour of new-style classes differs from that of old-style
classes in a number of important details in addition to what type
returns.
Some of these changes are fundamental to the new object model, like
the way special methods are invoked. Others are "fixes" that could not
be implemented before for compatibility concerns, like the method
resolution order in case of multiple inheritance.
Python 3 only has new-style classes.
No matter if you subclass from object
or not, classes are new-style
in Python 3.
Best Answer
ActionScript 3 Classes
The
package
statement.Okay, so firstly like you mentioned, a class must be wrapped by a
package
1. This gives us the first block, where you need to define the class.The
package
statement reflects the location of the class relative to the.fla
2. For example, if you have a folder "classes" within the same directory as the project .fla, then classes within that folder will need a package statement that reflects that:Defining the class.
Within a package statement, you may insert one class. Do not confuse this with the package itself, which can contain many classes - each class just needs to have its own file with the same package statement.
A class definition is made up of up to 5 parts:
internal
orpublic
. Aninternal
class can only be seen by classes within the same package, whereaspublic
classes can be seen from anywhere in the project.If you wanted to create a class called "Person" within the package
classes
, then we would end up with:Properties.
Classes can contain properties. Properties are defined using the
var
keyword. They may belong to one of a number of namespaces (including your own) and are used to hold values that belong to your class. Properties are most commonly found clustered together at the top of your class.Our
Person
class may enjoy the propertiesheight
andweight
:These properties can be accessed via any instance of
Person
that you create. Each instance will have its own set of these properties.Class constructors (I believe this is what you're asking about).
Constructors are used to hold logic that should be run as soon as an instance of your class is created. The class constructor has the same name as the class itself. It must be
public
and it does not return anything. Constructors can accept arguments, which are typically used to pass in references to dependencies for that class or required values.Methods.
Methods are used to hold logic that can be run when calling that method. Methods often return values and can accept arguments. Methods can belong to any namespace that you would expect properties to be able to belong to.
We may want to be able to easily determine the BMI of each instance of
Person
that we create, so we should create a method for that:Instances.
Now that we've defined our new class, we can create instances of this class using the
new
keyword. This can be done from anywhere that can access thePerson
class, which in this case is anywhere in the project because we've made the classpublic
.Though the class is
public
, accessing it from anywhere outside of the package it belongs in will require the use of animport
statement. This statement will need to be used within any class that belongs to a different package. Theimport
statement follows the same name used for thepackage
and includes the name of the class you want to include on the end:Once you've imported
Person
, you can create instances of it and assign them to a variable with differentheight
andweight
values:We can then obtain the BMI for each person using their
getBMI()
method:1 You can place classes outside of a package which can be referred to in the same .as file.
2 You can add more source paths, and packages can be relative to that.