string { get { return name; } }
时间: 2023-11-01 14:44:23 浏览: 63
你的问题是关于一个字符串属性的访问器。在C#中,你可以使用以下代码来定义一个具有只读访问器的字符串属性:
```csharp
public string Name { get { return name; } }
```
在这个例子中,`Name`是一个字符串属性,它具有一个只读的访问器。当你访问这个属性时,它会返回`name`变量的值。请注意,`name`变量应该在代码的其他地方定义和赋值。
相关问题
class warrior { private: int strength; int num; string name; public: warrior(int i,string name_temp,int n) { strength = i; bnum = 0; rnum = 0; num = n; name = name_temp; } ~warrior(){} string get_name() { return name; } int get_strength() { return strength; } int get_num() { return num; } };分别存放到头文件和源文件
将类定义放入头文件 `warrior.h` 中:
```c++
#ifndef WARRIOR_H
#define WARRIOR_H
#include <string>
class warrior {
private:
int strength;
int num;
std::string name;
public:
warrior(int i, std::string name_temp, int n);
~warrior();
std::string get_name();
int get_strength();
int get_num();
};
#endif // WARRIOR_H
```
将类的实现放入源文件 `warrior.cpp` 中:
```c++
#include "warrior.h"
warrior::warrior(int i, std::string name_temp, int n) {
strength = i;
num = n;
name = name_temp;
}
warrior::~warrior() {}
std::string warrior::get_name() {
return name;
}
int warrior::get_strength() {
return strength;
}
int warrior::get_num() {
return num;
}
```
在使用该类的代码中,只需要包含头文件 `warrior.h` 即可,如:
```c++
#include "warrior.h"
int main() {
warrior w(10, "Tom", 1);
std::string name = w.get_name();
int strength = w.get_strength();
int num = w.get_num();
return 0;
}
```
用赋值兼容规则改写#include <iostream> #include <string> using namespace std; // 定义教师抽象类 class Teacher { public: Teacher(string name, int course_num) : name_(name), course_num_(course_num) {} virtual ~Teacher() {} virtual int get_salary() = 0; // 计算月工资的虚函数 virtual string get_name() = 0;//获取老师名字 protected: string name_; // 姓名 int course_num_; // 课时数 }; // 定义教授类 class Professor : public Teacher { public: Professor(string name, int course_num) : Teacher(name, course_num) {} // 计算月工资的具体实现 virtual int get_salary() { return 5000 + course_num_ * 50; } virtual string get_name() { return name_; } }; // 定义副教授类 class AssociateProfessor : public Teacher { public: AssociateProfessor(string name, int course_num) : Teacher(name, course_num) {} // 计算月工资的具体实现 virtual int get_salary() { return 3000 + course_num_ * 30; } virtual string get_name() { return name_; } }; // 定义讲师类 class Lecturer : public Teacher { public: Lecturer(string name, int course_num) : Teacher(name, course_num) {} // 计算月工资的具体实现 virtual int get_salary() { return 2000 + course_num_ * 20; } virtual string get_name() { return name_; } }; int main() { // 创建教授对象 Professor p("好小子", 10); cout << "教授 " << p.get_name() << " 的工资是 " << p.get_salary() << " 元." << endl; // 创建副教授对象 AssociateProfessor ap("好小伙", 20); cout << "副教授 " << ap.get_name() << " 的工资是" << ap.get_salary() << " 元." << endl; // 创建讲师对象 Lecturer l("好家伙", 30); cout << "讲师 " << l.get_name() << " 的工资是 " << l.get_salary() << " 元." << endl; system("pause"); return 0; }
将#include <iostream>和#include <string>改为赋值兼容规则,则可以改为以下代码:
```
#include<iostream.h>
#include<string.h>
using namespace std;
class Teacher {
public:
Teacher(char* name, int course_num) : name_(name), course_num_(course_num) {}
virtual ~Teacher() {}
virtual int get_salary() = 0;
virtual char* get_name() = 0;
protected:
char* name_;
int course_num_;
};
class Professor : public Teacher {
public:
Professor(char* name, int course_num) : Teacher(name, course_num) {}
virtual int get_salary()
{
return 5000 + course_num_ * 50;
}
virtual char* get_name()
{
return name_;
}
};
class AssociateProfessor : public Teacher {
public:
AssociateProfessor(char* name, int course_num) : Teacher(name, course_num) {}
virtual int get_salary()
{
return 3000 + course_num_ * 30;
}
virtual char* get_name()
{
return name_;
}
};
class Lecturer : public Teacher {
public:
Lecturer(char* name, int course_num) : Teacher(name, course_num) {}
virtual int get_salary()
{
return 2000 + course_num_ * 20;
}
virtual char* get_name()
{
return name_;
}
};
void main() {
Professor p("好小子", 10);
cout << "教授 " << p.get_name() << " 的工资是 " << p.get_salary() << " 元." << endl;
AssociateProfessor ap("好小伙", 20);
cout << "副教授 " << ap.get_name() << " 的工资是" << ap.get_salary() << " 元." << endl;
Lecturer l("好家伙", 30);
cout << "讲师 " << l.get_name() << " 的工资是 " << l.get_salary() << " 元." << endl;
system("pause");
}
```