PDA

View Full Version : Class Clarification



Catalist
May 3rd, 2007, 11:40 PM
Ok, I just had a few things I need clarified.

1. I can have one public class per package?
2. package "name" { ... "name" refers to directory? Because I tried giving my package a name and got an error.
3. When extending a class, say we have class Shape {
and I want to extend class Square {
I would then create a... var myShape:Shape = new Square(12);
or var myShape:Square = new Square(12);

Anyhow, those are some things I'm not understanding right now. Thanks if you get help me, I've been going through the help files and I'd like to get past some of this stuff so I can get on to the better stuff.

B L U E
May 3rd, 2007, 11:54 PM
1. yes
2. yes, but don't use quotes and each subdirectory should be delineated by a period instead of a forward slash (i.e. classes.subfolder instead of classes/subfolder)
3. not quite sure what you mean on this one. say you want a new class "myShape" to extend "Shape"? then you'd have something like

myShape.as
package {

public class myShape extends Shape {

public function myShape() {

}

}

}

FLA
import myShape;

var shp:myShape = new myShape();


and myShape would inherit the properties/functions of Shape

Catalist
May 4th, 2007, 12:09 AM
Ok, I wasn't using quotes, but I understand that part now.
As far as extends goes, I meant this...

ActionScript Code:

// Person.as

class Person {

public function Person():void {}

}

// Man.as

class Man extends Person {

public function Man():void {}

}

// Main.as

var Dustin:Person = new Man();

// or

var Dustin:Person = new Person();

// Also, I haven't been importing Person.as, or Man.as, but it has been working. Anyhow, //I'm prolly way off, but I'll figure it out. I'm determined now.






Thanks

B L U E
May 4th, 2007, 12:17 AM
var Dustin:Person = new Man(); is incorrect. i'm not sure why you're trying to do that instead of var Dustin:Man = new Man(); ?

Catalist
May 4th, 2007, 12:23 AM
Ok, I figured it out.
I had to import, and now that I did that things are working....haha, I guess.
But, it was working before I imported, kinda.
Anyhow. I figured out what I have to do, here's another example, and I don't need help any more now, thanks though.




// Mammal.as

package {

public class Mammal {

public var age:Number;

public function Mammal(a:Number = 0):void {

age = a;

}

}

}

// Bird.as

package {

public class Bird extends Mammal {

public var color:String

public function Bird(a:Number = 0, c:String = "white"):void {

age = a;

color = c;

}

}

}

// animals.fla

import Mammal;

import Bird;

var myBird:Bird = new Bird(10, "Red");

trace(myBird.color);

trace(myBird.age);

// This all works, so my problem is solved. Thanks a ton.

TheCanadian
May 4th, 2007, 12:43 AM
A bird isn't a mammal!

You can have more than one class per package, they just must be defined in different AS files.

You can type a variable as a super class of the value you are assigning or an interface it implements. Note, however, that if the properties of the created instance are not memebers of the super class then you will get an error.

Importing a class that is in the same directory as the FLA or at a global classpath is redundant since they are automatically recognized. For example, import Mammal; is useless.

Catalist
May 4th, 2007, 09:19 AM
A bird isn't a mammal!

You can have more than one class per package, they just must be defined in different AS files.

You can type a variable as a super class of the value you are assigning or an interface it implements. Note, however, that if the properties of the created instance are not memebers of the super class then you will get an error.

Importing a class that is in the same directory as the FLA or at a global classpath is redundant since they are automatically recognized. For example, import Mammal; is useless.

Yea, I thought import Mammal seemed pointless, but the script seemed to work better than before. Sometimes I would get an error talking about undefined classes and methods. I was like, seriously, it's right there in the folder with is, what in the world an be wrong. Anyhow, I have it all sorted now. I'll just stick to one class per file, I don't want to muddy it up with the super class thing.