我在外部文件中有一个类,我将其导入。该类被实例化为导入下方的 3 个对象。
我运行函数调用初始,我调用方法将 mystudent0 的属性更改为不存在。然后返回所有属性,一切都按预期运行。
问题是当我运行其他函数来调用其他类方法时,对象会被初始化。到初始值。
我不知道如何停止重新初始化,所以我可以调用其他方法。
代码:
import { storage, context, PersistentVector } from "near-sdk-as";
import { Student } from "./student";
let students = new PersistentVector<string>('student');
let currstudents = new PersistentVector<Student>('studentlist');
let mystudent0 = new Student(1,"Pete Gillan", true);
let mystudent1 = new Student(2,"John Reager", true);
let mystudent2 = new Student(3,"Mary Smith", false);
//
export function initial(): string{
//
let strStud1: string;
let strStud2: string;
let strStud3: string;
let studentCounter: i32 = 0;
let len: i32;
currstudents.push(mystudent0);
currstudents.push(mystudent1);
currstudents.push(mystudent2);
mystudent0.set_absent();
strStud1 = " Student num " + mystudent0.get_studnum().toString() + " Student name: " + mystudent0.get_sname() + " present: " + mystudent0.get_present().toString() + '\n';
strStud2 = "Student num " + mystudent1.get_studnum().toString() + " Student name: " + mystudent1.get_sname() + " present: " + mystudent1.get_present().toString() + '\n';
strStud3 = "Student num " + mystudent2.get_studnum().toString() + " Student name: " + mystudent2.get_sname() + " present: " + mystudent2.get_present().toString() + '\n';
len = currstudents.length;
return strStud1 + strStud2 + strStud3 + " " + len.toString();
}
export function absent(): string{
mystudent0.set_absent();
return "absent";
}
//
export function welcomeUser(): string {
return "Greetings " + context.sender + " .... :)"
}
export function get_name(): string{
//let mystudent0 = new Student(1,"Pete Gillan", true);
let strStud1: string;
let strStud2: string;
let strStud3: string;
let len: i32;
strStud1 = " Student num " + mystudent0.get_studnum().toString() + " Student name: " + mystudent0.get_sname() + " present: " + mystudent0.get_present().toString() + '\n';
strStud2 = "Student num " + mystudent1.get_studnum().toString() + " Student name: " + mystudent1.get_sname() + " present: " + mystudent1.get_present().toString() + '\n';
strStud3 = "Student num " + mystudent2.get_studnum().toString() + " Student name: " + mystudent2.get_sname() + " present: " + mystudent2.get_present().toString() + '\n';
len = currstudents.length;
return strStud1 + strStud2 + strStud3 + " " + len.toString();
}
结果:
:~/roster-project/src/simple/assembly$ near call $CONTRACT initial --account_id $OWNER
Scheduling a call: dev-1640024016985-13984676030210.initial()
Doing account.functionCall()
Transaction Id CFhBKtxJZFt97kFNfRAE3EoXc9tvPqb6rgPMdv1atfjN
To see the transaction in the transaction explorer, please open this url in your browser
https://explorer.testnet.near.org/transactions/CFhBKtxJZFt97kFNfRAE3EoXc9tvPqb6rgPMdv1atfjN
' Student num 1 Student name: Pete Gillan present: **false**\n' +
'Student num 2 Student name: John Reager present: true\n' +
'Student num 3 Student name: Mary Smith present: false\n' +
' 6'
:~/roster-project/src/simple/assembly$ near call $CONTRACT get_name --account_id $OWNER
Scheduling a call: dev-1640024016985-13984676030210.get_name()
Doing account.functionCall()
Transaction Id 8485qqoHSp9WrjiNQKVZtzwMZsh1tE29WFwRkzfJwYLL
To see the transaction in the transaction explorer, please open this url in your browser
https://explorer.testnet.near.org/transactions/8485qqoHSp9WrjiNQKVZtzwMZsh1tE29WFwRkzfJwYLL
' Student num 1 Student name: Pete Gillan present: **true**\n' +
'Student num 2 Student name: John Reager present: true\n' +
'Student num 3 Student name: Mary Smith present: false\n' +
' 6'
:~/roster-project/src/simple/assembly$
班级:
import { logging, RNG } from "near-sdk-as";
//@nearBindgen
export class Student {
private studnum: i32
private sname: string;
private present: boolean;
constructor (studnum: i32, sname: string, present: boolean) {
//studnum = this.studnum;
//sname = this.sname;
//present = this.present;
this.sname = sname;
this.studnum = studnum;
this.present = present;
}
set_studnum(): void {
this.studnum = studnum;
}
set_name(): void {
this.sname = same;
}
set_present(): void {
this.present = true;
}
set_absent(): void {
this.present = false;
}
get_studnum(): i32 {
return this.studnum;
}
get_sname(): string {
return this.sname;
}
get_present(): boolean {
return this.present;
}
}