简体   繁体   中英

Dart: how to create a Final member Interface for Abstract Class

I want to enforce a Class User to implement a static member from an abstract class Base . Is this possible, or am I using the abstract inheritance paradigm incorrectly?

Without inheritance, the User can simply implement its own static member without overriding the Base , but I would like my linter to throw a warning when a Class does not implement some static member.

What works (without enforcing the interface)

We can implicitly use the same interface in Base and User .

abstract class Base {
  // This member could simply be removed and the snippet would still compile.
  static final String routeName = '/base';
}

class User extends Base {
  static final String routeName = '/base/user';
}

If we were to create a new Class Person that also extends Base , we have no specific implementation requirements. Ideally, I want to enforce Person to have a static member routeName .

class Person extends Base {
  // No warning about a missing static member 'routeName'.
}

Example of the idea (does not compile)

The Base would contain an unimplemented static member ( routeName ).

This snippet does not compile unless we specify the routeName (eg routeName = '/base' ).

abstract class Base {
  // IDE Error: The final variable 'routeName' must be initialized.
  static final String routeName;
}

The User that implements Base would override or implement the static member.

The snippet reports a warning, unless we remove the wrongly placed @override .

class User extends Base {
  // IDE Warning: Field doesn't override an inherited getter or setter.
  @override
  static final String routeName = '/base/user';
}

I want the IDE to throw an error for the Person Class since it does not implement the static member routeName .

class Person extends Base {
  // Expected IDE Error: The static final variable 'routeName' must be initialized.
}

To me, this looks similar to how Java does it , but I am unfamiliar with the Java specifics.

No, it's not possible. Static members are not inherited, they are not part of any interface, and they are not virtual, so you can't "override". An @override annotation doesn't apply to static declarations at all.

You can have static getters with the same name on both your classes, but they are completely unrelated, and don't have to have the same type or even signature (one can be a function, the other a getter, completely unrelated declarations). Every access to one of them has to choose which one directly in the source code.

(Java "inherits" static members only in the sense that you can call them on subclasses too, unless the subclass declares a static with the same name and signature - since Java has overloading - but you don't get any kind of virtual override or late binding. You still pick which one you use when you write the call. You just have more ways to write the same thing.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM