Dart的四个作业

1.

定义一个Student类,类中除了设计有stuID、stuName、stuClass、stuGrade、stuDept成员变量外,还设计有Student.newStudent(this.stuID)、factory Student(String stuID) 构造方法以及String toString()成员方法。请实现该类,并在main()中对设计实现的类进行实例化验证,并对验证输出进行简要适当的注释。main()验证代码类似如下:

main() {

  Person student1 = new Student('14194703748');

  print(student1.hashCode);

  Person student2 = new Student('14194703751');

  print(student2.hashCode);

  Person student3 = new Student('14194703748');

  print(student3.hashCode);

  

  assert(student1 == student3);

  print(student1 == student3);

  print(identical(student1, student3)); //判断变量是否指向同一块内存

}

 

reference:

Dart JSON和序列化

https://www.jianshu.com/p/34c45dc28305

class Student {

  String stuID;

  String stuName;

  String stuGrade;

  String stuDept;



  factory Student(String stuID) {

    return Student.newStudent(stuID);

  }

  Student.newStudent(this.stuID) {

    stuName = null;

    stuGrade = null;

    stuDept = null;

  }

  String toString() {

    return "stuID:" +

        this.stuID +

        "\nstuName:" +

        this.stuName +

        "\n" +

        "\nstuGrade:" +

        this.stuGrade +

        "\nstuDept" +

        this.stuDept;

  }

}



main() {

  Student student1 = new Student('14194703748');

  print(student1.stuID.hashCode);

  //在Object类中,每个对象都有一个默认的散列码,其值为对象的存储地址。

  Student student2 = new Student('14194703751');

  print(student2.stuID.hashCode);

  Student student3 = new Student('14194703748');

  print(student3.stuID.hashCode);

  assert(student1 == student3);

  print(student1.stuID == student3.stuID);

  print(identical(student1.stuID, student3.stuID)); //判断变量是否指向同一块内存

}

// 输出:

// 444574562

// 666782041

// 444574562

// true

// true

2.

有student.json文件,内容如下:

{

    "id": "10086",

    "name": "Jack",

    "phone": "13311112222"

}

另设计有Student类,类容如下:

class Student {

  String stuId;

  String stuName;

  String stuTel;

 

  Student({this.stuId, this.stuName, this.stuTel});

 

  factory Student.fromJson(Map<String, dynamic> json) {

    return Student(

        stuId: json["id"], stuName: json["name"], stuTel: json["phone"]);

  }

}

 

现在要求使用异步方式从磁盘上读取person.json 文件,得到一个json字符串,然后使用json.decode(String string)对json字符串进行解析,得到一个Map对象,最后使用Student.fromJson(Map<String, dynamic> json)建立一个与student.json中内容相对应的Student类型的实例对象并予以输出。

import 'dart:io';

import 'dart:convert';



class Student {

  String stuId;

  String stuName;

  String stuTel;

  Student({this.stuId, this.stuName, this.stuTel});

  factory Student.fromJson(Map<String, dynamic> json) {

    return Student(

        stuId: json["id"], stuName: json["name"], stuTel: json["phone"]);

  }

  String toString() {

    return "id:" +

        this.stuId +

        "\nname:" +

        this.stuName +

        "\nTel:" +

        this.stuTel;

  }

}



void main() async {

  File file = new File('student.json');

  try {

    String k = await file.readAsString();

    print("文件读取成功!");

  } catch (error) {

    print(error);

  }

  String k = await file.readAsString();

  Student student1 = Student.fromJson(jsonDecode(k));

  print(student1.toString());

}

//copyright@halfgong

3.

定义一个抽象类Shape,类中有抽象方法getPerimeter()和getArea()。

定义一个Rectangle类,使之继承Shape抽象类,覆盖Shape抽象类中的抽象方法getPerimeter()和getArea()。

定义一个Cuboid长方体类,使其继承Rectangle类,其中包含一个表示高的double类型成员变量height;定义构造方法Cuboid(double length, double width, double height);  再定义求长方体体积的成员方法volume()。

在main()中创建Rectangle类的实例、Cuboid类的实例进行测试。比如求一个长、宽、高分别为10、5、2的长方体的体积。

最后再定义Square类、Cube类。思考Square类、Cube类如何在已有的Shape类、Rectangle类和Cuboid类上继承实现。进一步在main()中测试定义的Square类、Cube类。

 

abstract class Shape {

  double getPerimeter();

  double getArea();

}



class Rectangle extends Shape {

  double length;

  double width;

  Rectangle(this.length, this.width);

  double getPerimeter() {

    return this.length * 2 + this.width * 2;

  }



  double getArea() {

    return this.length * this.width;

  }

}



class Cuboid extends Rectangle {

  double high;

  Cuboid(double length, double width, this.high) : super(length, width) {}

  double volume() {

    return this.high * this.length * this.width;

  }

}



class Square extends Shape {

  double edge;

  Square(this.edge);

  double getPerimeter() {

    return this.edge * 4;

  }



  double getArea() {

    return this.edge * this.edge;

  }

}



class Cube extends Square {

  Cube(double edge) : super(edge) {}

  double volume() {

    return this.edge * this.edge * this.edge;

  }

}



main() {

  Rectangle R = new Rectangle(1, 3);

  print(R.getArea());

  Cuboid C = new Cuboid(1, 3, 5);

  print(C.volume());

  Square S = new Square(2);

  print(S.getArea());

  Cube Cu = new Cube(2);

  print(Cu.volume());

}



// result:

// 3.0

// 15.0

// 4.0

// 8.0

//copyright@龚一半

4.

有如下Rectangle类,请为该类定义两个计算属性:right 和 bottom

class Rectangle {

  num left;

  num top;

  num width;

  num height;

 

  Rectangle(this.left, this.top, this.width, this.height);

 

  // 定义两个计算属性: right and bottom.

  //...

}

然后在main( )中对Rectangle类进行测试。测试代码类似如下:

main() {

  var rect = new Rectangle(3, 4, 20, 15);

  assert(rect.left == 3);

  print('right: ${rect.right}, bottom: ${rect.bottom}');

  rect.right = 12;

  //set right, then calculate left with width: left = right - width

  assert(rect.left == -8);

  print('left: ${rect.left}, top: ${rect.top}');

}

class Rectangle {

  num left;

  num top;

  num width;

  num height;

  Rectangle(this.left, this.top, this.width, this.height);

  // 定义两个计算属性: right and bottom.

  //...

  num get right {

    num right;

    right = left;

    return right;

  }



  num get bottom {

    num bottom;

    bottom = top;

    return bottom;

  }



  num set right(num right) {

    num left;

    left = right - width;

    return left;

  }

}



main() {

  var rect = new Rectangle(3, 4, 20, 15);

  assert(rect.left == 3);

  print('right: ${rect.right}, bottom: ${rect.bottom}');

  rect.right = 12;

  //set right, then calculate left with width: left = right - width

  assert(rect.left == -8);

  print('left: ${rect.left}, top: ${rect.top}');

}

//result:

// right: null, bottom: null

// left: 3, top: 4

//copyright@龚梓橙

 

posted @ 2020-10-03 11:03  龚一半  阅读(303)  评论(2编辑  收藏  举报