Actionscript 2: A class loaded into a parent class, get parent variables - actionscript

I have a child class that is loaded into the parent class when the swf begins, like so:
var myvar = 'hello';
public function Parent()
{
this.child = new Child();
};
How can I retrieve the variable 'myvar' from within child?

Child has no reference to the parent, so there is no way to retrieve myvar.
You could create child like this:
public class Child
{
private var parent:Parent;
public Child(parent:Parent)
{
this.parent = parent;
}
...
}
This will allow the child to access it's parent through it's parent property and will be able to see all the public members that belong to parent.

Related

Calling consturctor of superclass from constructor's body of subclass in Dart

I am a beginner in Dart. :-)
I wonder why I can't call the parent class constructor inside the child class constructor body.
class Parent {
String? name;
Parent(this.name);
Parent.namedConstructor(this.name);
}
class Child extends Parent {
// Child(String name) : super(name); // It's OK
// Child(String name) : super.namedConstructor(name); // It's OK, too.
Child(String name) {
super(name); // Error
super.namedConstructor(name); // Error
}
}

Access child class constructor from parent class

I would like to get access to a class constructor from it's parent like;
var childClass = Parent().child;
Child child = child.constructor(name, age);
Is that possible? Or is there a way to use a class to return a child class to use with constructor?
What you are looking for is called a "factory constructor":
Use the factory keyword when implementing a constructor that doesn’t always create a new instance of its class. For example, a factory constructor might return an instance from a cache, or it might return an instance of a subtype
https://dart.dev/guides/language/language-tour#constructors
Example:
class Parent {
factory Parent() {
return Child();
}
}

how can i initialize super class variables in dart language?

Simply i have to classes child and parent class i am new in dart language all i need to assign super class properties from child class
this is super class structure
class Trip{
final int id;
final String title;
final double price;
Trip({this.id,this.title,this.price});
}
and this is child class
class FullTrip extends Trip{
final String data;
FullTrip({this.data}) : super(id:id,title:title,price:price);
}
sure this not working at all
the question is : how can i initialize instance from FullTrip and pass variable for FullTrip and Trip(super class)
thanks in advance
You need to repeat the parameters in the subclass.
class FullTrip extends Trip{
final String data;
FullTrip({this.data, int id, String title, double price}) : super(id:id,title:title,price:price);
}
There are discussions about reducing such boilerplate for constructors, but nothing is decided yet as far as I know.
User super-parameters, which were added in Dart 2.17. For example, say this is your class:
class Parent {
Parent({
int? i,
bool b = false,
required String s,
});
}
Old way (boilerplate code)
Earlier you had to do something like this:
class Child extends Parent {
Child({
int? i,
bool b = false,
required String s,
}) : super(
i: i,
b: b,
s: s,
);
}
New way (neat and clean)
But now you can get rid of those boilerplate code.
class Child extends Parent {
Child({
super.i,
super.b = false,
required super.s,
});
}
If you want to re-initialize in a subclass the not private variables of an extended or implemented upper class before compiling, simply use #override. Look at this example
(if you want to try the code, have in mind it supports null safety. If your test doesn't, simply delete the ? signs.)
class Go {
String? name = "No name";
}
class Foo implements Go { //use either implements or extends
#override
String? name = "Foo";
}
class Doo extends Go { //use either implements or extends
#override
String? name = "Doo";
}
Use case example: In the code above, you can see that in our upper class we have this String name variable. Then in the subclasses we can simply override that variable. After that, in main we can now for example do something like iterating through a List<Go>, access what we wanted to and then trigger something, like printing the name:
void main() {
List<Go> foos = [
Go(),
Foo(),
Doo(),
];
for(Go x in foos){
print(x.name);
}
}
Output:
No name
Foo
Doo
This works if you use either the extends or implements keywords.

Abstract domain class and tablePerHierarchy

In my Grails 3.2.6 app I have 2 classes:
abstract class Base {
static mapping = {
tablePerHierarchy false
}
}
and
class Child extends Base {
static mapping = {
collection 'child'
}
}
Upon saving the instances of Child are dumped into "base" collection (with _class = Child field) instead of "child".
How to make it work right?
UPDATE
I defined the Base as a trait under src/main/groovy:
trait Base { }
and
class Child implements Base { }
then it worked properly.
In your Child class mapping method, add this
table "child"

How to use a vaiable declared in base class froma derived class

I have a base class which uses a variable in ones of its methods and also a derived class that needs the same vaiable in its methods.
Below are the details
abstract class BaseClass
{
protected Transition transition;
public event EventHandler ActionComplete;
private string abc;
Public string ABC
{
get{ return abc;}
set { abc = value;}
}
public void TransitionState(BaseClass obj)
{
ActionComplete(this, null);
}
public abstract void RequestSomeAction(Transition obj);
}
internal class DerivedClass : BaseClass
{
//do i need to create transition variable again here
internal new Transition transition;
//this parameter's value (here obj) should be assigned to the base class,
public override void RequestSomeAction(Transition obj)
{
//is below code correct.
stateTransition = obj;
base.transition= transition;
}
}
Why the new transition variable and base.transition? If you make a variabele protected in the 'base class' you can just use it directly in the 'child class' that extends that class!?
First if you don't need to redeclare the variable, don't. You can just use the protected property.
this.transition
If you need 2 copies of the variable with the same name (i can't think of any case when you would need this) then you are technically 'hiding' versus overridding so you could do:
base.transition
or the same
((BaseClass)this).transition
MSDN on base.

Resources