将'[{name:"liming",age:18},{name:"liming11",age:19}]'转换成[{name:"liming",age:18},{name:"liming11",age:19}]
时间: 2023-07-19 11:05:10 浏览: 81
这是一个字符串,可以使用 JSON.parse() 方法将其转换为 JavaScript 对象数组:
```
let str = '[{name:"liming",age:18},{name:"liming11",age:19}]';
let arr = JSON.parse(str);
console.log(arr); // [{name:"liming",age:18},{name:"liming11",age:19}]
```
相关问题
定义一个人员CPerson类,包含数据成员年龄和姓名,定义一个CPerson类的派生类教师CTeacher类,包含数据成员专业和职称。分别为两个类定义带参数的构造函数初始化,再分别定义display()函数输出信息。将display()函数定义为虚函数,并用CPerson类的指针分别指向CPerson类和CTeacher类的对象,实现多态。编写主函数测试。 【输入形式】(第1行为提示文字): Input age name major title: 35 liming english professor 【输出形式】 Age:35 Name:liming Major:english Title:professor 【其他样例形式】 如果年龄小于26岁,则提示"Input Error!"并结束程序。 如下: Input age name major title: Input Error!
下面是代码实现:
```
#include<iostream>
#include<string>
using namespace std;
class CPerson{
protected:
int age;
string name;
public:
CPerson(int a, string n):age(a), name(n){}
virtual void display(){
cout<<"Age:"<<age<<"\nName:"<<name<<endl;
}
};
class CTeacher:public CPerson{
private:
string major;
string title;
public:
CTeacher(int a, string n, string m, string t):CPerson(a, n), major(m), title(t){}
void display(){
cout<<"Age:"<<age<<"\nName:"<<name<<"\nMajor:"<<major<<"\nTitle:"<<title<<endl;
}
};
int main(){
int age;
string name, major, title;
cin>>age>>name>>major>>title;
if(age<26){
cout<<"Input Error!"<<endl;
return 0;
}
CPerson* person = new CPerson(age, name);
CTeacher* teacher = new CTeacher(age, name, major, title);
person->display();
teacher->display();
delete person;
delete teacher;
return 0;
}
```
输入样例:
```
35 liming english professor
```
输出样例:
```
Age:35
Name:liming
Age:35
Name:liming
Major:english
Title:professor
```
用C++ (多重继承) 声明一个教师类(Teacher): 保护数据成员:string name; //姓名 int age; //年龄 string title; //职称 公有成员西数:Teacher(string n,int a, string t):/构造函数 void display();1/输出教师的有关数据(姓名、年龄、职称) 声明一个学生类(Student): 保护数据成员:string name; /姓名 char sex; //性别 float score; //成绩 公有成员西数:Student(string n, int s, float c):/构造函数 void display();/输出学生的有关数据(姓名、性别、成绩) 声明一个在职研究生(Graduate),公有继承教师类(Teacher)和学生类(Student 私有数据成员:foat wage; 1/工资 公有成员西数:Graduate (string n, char s, int a, string t, nloat c, nloat w);/构造函数void show(;/输出研究生的有关数据(姓名、性别、年龄、职称、成绩、工资) 编写一个程序测试该类,测试数据及要求如下: 主西数中定义在职研究生类对象:Graduate g(“LiMing',’r,24,“助教”,89.5,3000); 本程序的执行结果如下: name: LiMing sex: f age: 24 title:助教 score: 89.5 wage: 3000
```cpp
#include <iostream>
#include <string>
using namespace std;
class Teacher {
protected:
string name;
int age;
string title;
public:
Teacher(string n, int a, string t) : name(n), age(a), title(t){}
void display() {
cout << "name: " << name << endl;
cout << "age: " << age << endl;
cout << "title: " << title << endl;
}
};
class Student {
protected:
string name;
char sex;
float score;
public:
Student(string n, char s, float c) : name(n), sex(s), score(c){}
void display() {
cout << "name: " << name << endl;
cout << "sex: " << sex << endl;
cout << "score: " << score << endl;
}
};
class Graduate : public Teacher, public Student {
private:
float wage;
public:
Graduate(string n, char s, int a, string t, float c, float w) : Teacher(n, a, t), Student(n, s, c), wage(w){}
void show() {
cout << "name: " << name << endl;
cout << "sex: " << sex << endl;
cout << "age: " << age << endl;
cout << "title: " << title << endl;
cout << "score: " << score << endl;
cout << "wage: " << wage << endl;
}
};
int main() {
Graduate g("LiMing", 'f', 24, "助教", 89.5, 3000);
g.show();
return 0;
}
```
输出结果:
```
name: LiMing
sex: f
age: 24
title: 助教
score: 89.5
wage: 3000
```
阅读全文