Part1:A Brief Study of Classes and Object-oriented Programming.
来源:互联网 发布:淘宝店哪里有货源 编辑:程序博客网 时间:2024/05/21 08:41
Part1:A Brief Study of Classes and Object-oriented Programming.
For using papervision3d(PV3d),we will use PV3d classes a lot. In PV3d,we just use only the basics of classes.
What are classes? In fact, they are nothing more than a set of functions(methods) and variables(properties) grouped together in a single file, which is known as the class definition. A class forms the blueprint for new objects that you create.
First,have a look at how you can use Sprites class to create a new Sprite object:
var mySprite:Sprite = new Sprite();
By doing this, we create a new copy of the Sprite class as an object called mySprite. This is called instantiation of an object. PV3d is a set of custom classes.
Here we will study how to use classes.
Creating a custom class
An ActionScript class is basically a text-based file with an .as extension stored somewhere on our computer, containing ActionScript code. For example:
package{
public class ExampleClass
{
public var myName:String = “Paul”;
public function ExampleClass()
{
}
public function returnMyName():String
{
return“My name is” + myName;
}
}
}
On the first line, you'll find the package statement. Packages are a way to group classes together and represent the folder in which you saved the file. Imagine you have created a folder called myPackage inside the same folder where you've saved an FLA or inside a defined source folder. In order to have access to the folder and it's classes, you will need to define the package using the folder's name as shown next:
package myPackage{
…
}
Most time we default classes, so just write as the example.
After the package definition, you'll find the class definition, which looks as follows.
public class ExampleClass
{
…
}
The name of the class must be the same name as the class file. In this example, the file needs to be saved as ExampleClass.as.
At the top of the class definition you'll see the word public, which is a keyword defining that the class is accessible to all other code in the project.
The defined name of the class will be used to instantiate new copied ofthe class. Instantiating this class could be done like this:
var classExample:ExampleClass = new ExampleClass();
Inside the class, a string variable is defined in pretty much the same way as you would when working with timeline scripting.
The definition of a variable inside a class is called as a class property. Each definition starts off with an access modifier. The keyword public means that it is both readable and writeable by code located outside the class:
var classExample:ExampleClass = new ExampleClass();
classExample.myName= “Jeff”;
This creates an instance of ExampleClass and we can changed the myName property from Paul to Jeff.
In the next lines we see the creation of a new function calledExampleClass in the class. Functions that have the same name as the name of the class are known as constructors.
public function ExampleClass()
{
}
Constructors always have a public access modifier and are called automatically each time the class instantiated. This means that the code inside this function will be executed automatically.
All other function definitions inside the class are called methods.
public function returnMyName():String
{
return “My name is” + myName;
}
If you're not familiar with function. You can read some other tutorials about ActionScript.
We can use keywords public private protected to make the property can be read and written ,can be read but can't be written, can't be read or written.
Inheritance
Inheritance is the technique of extending an existing class, called the supe rclass or base class, with another class, called the sub class, which adopts the entire class definition from the super class. The new class has access to all non-private properties and methods that are defined inside the super class.
Take a look at how this can be achieved by two simple classes:
package{
public class BasicClass
{
public function doSomething():void
{
trace(“I'mdoing something”);
}
}
}
package{
public class SubClass extends BasicClass
{
public function saySomething():void
{
trace(“I'm saying something”);
}
}
}
The first class will be the super class once it's extended and has only one method. Instantiating a BasicClass and calling a method is pretty straightforward.
var basicClass:BasicClass = new BasicClass();
basicClass.doSomething();
Nothing special so far. According to the class definition of SubClass we see that it extends BasicClass. By doing so, it is possible to use properties and methods from the BasicClass. Let's see what this looks like:
var subClass : SubClass = new SubClass();
subClass.doSomething();
subClass.saySomething();
SubClass inherits doSomething method from BasicClass.
Extending is a good way to create new classes, based on other existing classes.It keeps your code very clean and abstract.
We can change the method by overriding it.
package{
public class SubClass extends BasicClass
{
override public function doSomething():void
{
trace(“Hi,you need to do something “);
}
public function saySomething():void
{
trace(“I'msaying something”);
}
}
}
And if you do this:
override public function doSomething();void
{
trace(“Hi,you need to do something “);
super.doSomething();
}
It will trace:
Hi,you need to do something
I'm doing something
If you want to know more, there are numerous books and online tutorials on the subject of OOP(object-oriented programming).
- Part1:A Brief Study of Classes and Object-oriented Programming.
- Programming with Objects: A Comparative Presentation of Object Oriented Programming with C++ and Jav
- A Static C++ Object-Oriented Programming (SCOOP) Paradigm Mixing Benefits of Traditional OOP and GP
- The Essence of Object-Oriented Programming with Java and UML
- Object-Oriented Programming and Java
- Bob Tarr: Some Object-Oriented Design Principles(1): Minimize The Accessibility of Classes and Members
- Understanding the concepts of Object Oriented Programming
- The characters of OOP(object-oriented programming)
- ANSI C and object-oriented programming
- FCC-Object Oriented and Functional Programming
- FreeCodeCamp <Object Oriented and Functional Programming>
- A Guide to MATLAB Object-Oriented Programming
- C++ for Artists: The Art, Philosophy, and Science of Object-Oriented Programming
- Java For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
- “What's wrong with Object-Oriented Programming and Functional Programming”
- A Brief Look at Nested and Inner Classes
- Histogram of Oriented Gradients and Object Detection
- Understanding Object Oriented Programming
- Android 中的ListView选中项的背景颜色怎么设置?
- window.showModalDialog模式对话框 和 window.open的区别
- linux内核入门集
- 研一第一个学期的结束
- Linux 用户(user)和用户组(group)管理概述
- Part1:A Brief Study of Classes and Object-oriented Programming.
- Eclipse快捷键整理
- Transforms CTM, 转换矩阵
- WEB服务器应用_服务器安全设置之_系统服务(设置完毕需要重新启动)
- WEB服务器应用_服务器安全设置之_组件安全设置篇
- C# String Format
- Hibernate 入门错误解决办法。
- Windows线程、界面、消息剖析
- WEB服务器应用_服务器安全设置之_IIS用户设置方法