尝试调用以下课程的toString
时,会调用Object.toString
并将[object Object]
打印到控制台。为什么Bankaccount.toString
没有被调用?
export class Bankaccount {
name: string;
iban: string;
bic: string;
accountNumber: string;
customerId: string;
type: string;
id?: number;
constructor(name: string, iban: string, bic: string, accountNumber?: string, customerId?: string, type?: string, id?: number) {
this.name = name;
this.iban = iban;
this.bic = bic;
this.accountNumber = accountNumber;
this.customerId = customerId;
this.type = type;
this.id = id;
}
toString = (): string => {
return 'Bankaccount[Name: ' + this.name + ', IBAN: ' + this.iban + ', BIC: ' + this.bic + ', Typ: ' + this.type + ']';
}
}
来电者/客户:
import {Bankaccount} from '../../models/model-interfaces';
... further imports ...
@Component({
selector: 'ac-bankaccount-edit',
templateUrl: './bankaccount-edit.component.html',
styleUrls: ['./bankaccount-edit.component.css']
})
export class BankaccountEditComponent {
bankaccount: Bankaccount = new Bankaccount('BankName', 'IBAN1234', 'BIC9876', '', '', 'PRIVATE');
constructor() {}
logBankaccount() {
console.log('BA1: ' + this.bankaccount);
console.log('BA2: ' + this.bankaccount.toString());
console.log(`${this.bankaccount}`);
}
}