class Person {
String name;
int age;
String _birthday;
bool get isAdult => age > 18;
void run(){
print("Person run...");
}
}
class Student extends Person{
@override
bool get isAdult => age > 15;
@override
void run(){
print("Student run...");
}
@override
String toString(){
return("Student toString");
}
void study(){
print("Student study...");
}
}
class Person {
String name;
int age;
String _birthday;
bool get isAdult => age > 18;
Person(this.name);
Person.withName(this.name);
void run(){
print("Person run...");
}
}
class Student extends Person{
Student(String name) : super(name);
}
class Person {
String name;
int age;
bool get isAdult => age > 18;
Person(this.name);
Person.withName(this.name);
void run(){
print("Person run...");
}
}
class Student extends Person{
final String gender;
Student(String name,String g) : gender = g,super(name);
}
abstract class Person{
void run();
}
class Student extends Person{
@override
void run(){
print("run...");
}
}
class Person {
String name;
int age;
void run(){
print("Person run...");
}
}
class Student implements Person{
@override
String name;
@override
int age;
@override
void run(){
print("run...");
}
}
建议使用抽象类作为接口
abstract class Person{
void run();
}
class Student implements Person{
@override
void run(){
print("run...");
}
}
顺序问题
class A {
String getMessage() => 'A';
}
class B {
String getMessage() => 'B';
}
class P {
String getMessage() => 'P';
}
class AB extends P with A, B {}
class BA extends P with B, A {}
class C extends P with B, A {
String getMessage() => 'C'; //优先级最高的是在具体类中的方法。
}
//关系连接:implements实现、extends继承、with混入
class CC extends P with B implements A {
}
var cc = CC();
print(cc.getMessage());//=>B
使用mixin进行类组合
abstract class Person{
String name;
int age;
void run();
}
class Animal{
void eat(){
print("eat...");
}
}
//mixin on的使用,使用这个mixin的class必须继承自Animal
mixin Boy on Animal implements Person{
@override
String name;
@override
int age;
@override
void run(){
print("Person run...");
}
void playBall(){
print("playBall...");
}
}
mixin Girle implements Person {
@override
String name;
@override
int age;
@override
void run(){
print("Person run...");
}
void dance(){
print("dance...");
}
}
class Cat{
void eat(){
print("eat...");
}
}
此时我们可以使用快捷方式组合class
class A = Cat with Girle;
var a = A();
a.run();
返回类型 operator操作符(参数1、参数2.....){
实现体
return 返回值
}
class Person {
int age;
bool operator > (Person person){
return this.age > person.age;
}
}
var p1= Person();
p1.age = 18;
var p2= Person();
p2.age = 13;
print(p1>p2);//ture