Is there way to overriding method in Dart like JAVA, for example:
public class A {
public void handleLoad() {
}
}
And when overriding:
A a = new A() {
#Override
public void handleLoad() {
// do some code
}
};
No, Dart does not have anonymous classes. You have to create a class that extends A and instantiate it.
No but it much less useful in Dart because you can just reassign function:
typedef void PrintMsg(msg);
class Printer {
PrintMsg foo = (m) => print(m);
}
main() {
Printer p = new Printer()
..foo('Hello') // Hello
..foo = ((String msg) => print(msg.toUpperCase()))
..foo('Hello'); //HELLO
}
However you will need some extra boilerplate to access instance.
Use type Function:
class A {
final Function h
A(this.h);
void handleLoad(String loadResult) { h(loadResult); }
}
Or
class A {
final Function handleLoad;
A(this.handleLoad);
}
A a = new A((String loadResult){
//do smth.
});
Related
Lets say that I have an abstract class
abstract class OnClickHandler {
void doA();
void doB();
}
I have a class
class MyClass {
OnClickHandler onClickHandler;
MyClass({
this.onClickHandler
})
void someFunction() {
onClickHandler.doA();
}
}
And I have a class
class Main implements onClickHandler {
// This throws me an error
MyClass _myClass = MyClass(onClickHandler = this); // <- Invalid reference to 'this' expression
#override
void doA() {}
#override
void doB() {}
}
How can I say that use the same implementations that the Main class has? or is there an easier/better way to do this?
Your problem is that this does not yet exists since the object are still being created. The construction of Dart objects is done in two phases which can be difficult to understand.
If you change you program to the following it will work:
abstract class OnClickHandler {
void doA();
void doB();
}
class MyClass {
OnClickHandler onClickHandler;
MyClass({this.onClickHandler});
void someFunction() {
onClickHandler.doA();
}
}
class Main implements OnClickHandler {
MyClass _myClass;
Main() {
_myClass = MyClass(onClickHandler: this);
}
#override
void doA() {}
#override
void doB() {}
}
The reason is that code running inside { } in the constructor are executed after the object itself has been created but before the object has been returned from the constructor.
I am missing method from interface
value of annotiations is null.
Already tried hardcoded, it works, but I need to be customizable.
package com.luv2code.springdemo.mvc.validation;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
public class CourseCodeConstraintValidator implements ConstraintValidator {
private String coursePrefix;
public void initalize(CourseCode theCourseCode) {
coursePrefix = theCourseCode.value();
}
#Override
public boolean isValid(String theCode, ConstraintValidatorContext theConstraintValidatorContext) {
/*if (coursePrefix==null)
coursePrefix = "LUV";*/
System.out.println(theConstraintValidatorContext.getDefaultConstraintMessageTemplate());
boolean result;
if (theCode!=null) {
System.out.println(coursePrefix);
result = theCode.startsWith(coursePrefix);
} else {
result = true;
}
return result;
}
}
Expecting possible #Override method initialize
initalize => initialize
just one letter missing
I have a lot of part of library. But all of the same type (extends Part)
part1.dart
part of Parts;
class Part1 extends Part { /* ... */ }
parts.add((varOfSomeClass){ return new Part1(varOfSomeClass + 1); });
part2.dart
part of Parts;
class Part2 extends Part { /* ... */ }
parts.add((varOfSomeClass){ return new Part2(varOfSomeClass - 1); });
parts.dart
library Parts;
part "Part1.dart";
part "Part2.dart";
List<Function> parts = new List<Function>();
class Parts {
getPart(int index) {
if (parts.contains(index)) {
return parts[index](someVarOfThisClass);
}
}
}
OUTPUT: error: unexpected token 'parts'
How to get all included factories without create instance all the Part classes?
For example need to do:
BMW.dart
part of Auto;
class BMW {
String color;
BMW(this.color);
}
list.add((color){
return new BMW(color);
});
Lada.dart
part of Auto;
class Lada {
List<int> color;
}
list.add((color){
var auto = new Lada();
auto.color = hex2rgb(color);
return auto;
});
Auto.dart
library Auto;
class Auto {
getByIndex(int index) {
if (list.contains(index)) {
return list[index](color);
}
return null;
}
}
Looks like your problem is that you have code outside of a class or function definition. If I'm guessing what you want to do correctly, you want BMW.dart to look something like
part of Auto;
class BMW {
String color;
BMW(this.color);
}
and then in your main() method have code like
main() {
List list = [];
list.add((color) => new BMW(color));
}
This will get the code above running. It's probably not the best way to structure your program though. You may want to do some more reading on Dart factory constructors. https://www.dartlang.org/dart-tips/dart-tips-ep-11.html is a good place to start.
I don't know what you need it for but how about using mirrors:
main.dart
library cars;
import 'dart:mirrors';
part 'bmw.dart';
part 'audi.dart';
abstract class Car {
}
void main() {
List<Car> cars = new List<Car>();
Map libraries = currentMirrorSystem().libraries;
LibraryMirror mirror = libraries[libraries.keys.last];
mirror.declarations.forEach((Symbol s, DeclarationMirror mirror) {
if(mirror is ClassMirror) {
if(!mirror.isAbstract && mirror.isAssignableTo(reflectType(Car))) {
// new Symbol(mirror.reflectedType.toString()))
cars.add(mirror.newInstance(#Car, []).reflectee);
}
}
});
print(cars);
}
bmw.dart
part of cars;
class Bmw extends Car {
Bmw.Car() {
}
}
audi.dart
part of cars;
class Audi extends Car {
Audi.Car() {
}
}
have class Klass with static method fn1
class Klass {
static String fn1() => 'hello';
}
> Klass.fn1(); // hello
but when Klass is assigned to a variable, calling the method fn1 fails
var k = Klass;
> k.fn1() // "Unhandled exception: Class '_Type' has no instance method 'fn1'.
don't quite know what's going on here
A simple workaround
class Klass {
static fn1(String name) {
return name;
}
fn1NonStatic(String name) {
return fn1(name);
}
}
Klass().fn1NonStatic("test");
I'm not sure what the intent of the code here is, but you might want to use dart:mirrors to reflectively call fn1(). I don't believe you can invoke it by assigning Klass to a variable. Here is how you can do it:
import 'dart:mirrors';
class Klass {
static String fn1() => 'hello';
}
main() {
final mirror = reflectClass(Klass);
print(mirror.invoke(#fn1, []).reflectee); // Prints 'hello'.
}
I have ImageView.class how to get the program to use it instead of the native javax.swing.text.html.ImageView?
javax.swing.text.html.ImageView map = new javax.swing.text.html.ImageView(); //does not work
I was told that it is necessary to use ClassFileTransformer and ClassLoader, but I can not find a working examples
I think that what you really want to do is to...
Extend HTMLEditorKit and override getViewFactory()
Have it return a class that extends HTMLEditorKit.HTMLFactory
In that class, override create() to return your custom view for <img> and super.create() otherwise
Like this:
class MyImageKit extends HTMLEditorKit {
private static final MyImageFactory myFactory = new MyImageFactory();
public ViewFactory getViewFactory() {
return myFactory;
}
static class MyImageFactory extends HTMLFactory {
public View create(Element elem) {
Object type = elem.getAttributes()
.getAttribute(StyleConstants.NameAttribute);
if(type == HTML.Tag.IMG) {
return new MyImageView(elem);
} else {
return super.create(elem);
}
}
}
}
class MyImageView extends ImageView {
MyImageView(Element elem) {
super(elem);
}
protected void setPropertiesFromAttributes() {
super.setPropertiesFromAttributes();
try {
ImageView.class.getDeclaredField("vAlign").set(this, new Float(0.75f));
} catch(Exception e) {
e.printStackTrace();
}
}
}