class EvenNumber { private: int value; public: EvenNumber() { value = 0;
时间: 2024-05-17 12:13:40 浏览: 15
} EvenNumber(int v) { if (v % 2 == 0) { value = v; } else { value = 0; } } int getValue() { return value; } EvenNumber getNext() { return EvenNumber(value + 2); } EvenNumber getPrevious() { return EvenNumber(value - 2); } };
以上是一段 C++ 代码,它定义了一个名为 EvenNumber 的类,该类表示偶数。该类有三个公共方法:getValue、getNext 和 getPrevious,分别用于获取当前偶数值、获取下一个偶数和获取上一个偶数。请问,如果我想创建一个 EvenNumber 对象并将其值设置为 8,应该如何操作?
相关问题
public final class int { AS3 native function toFixed(p:* = 0):String; AS3 native function valueOf():int; AS3 native function toPrecision(p:* = 0):String; public static const length:int; AS3 native function toString(radix:* = 10):String; public static const MAX_VALUE:int; public static const MIN_VALUE:int; public native function int(value:* = 0); AS3 native function toExponential(p:* = 0):String; }转成TS
在 TypeScript 中,`int` 是一个关键字,不能用作类名,因此需要改为其他名称。此外,TypeScript 中没有 `AS3 native function` 这样的语法,需要使用其他方式表示。下面是将该类转换为 TypeScript 的代码:
```typescript
class MyInt {
public static readonly length: number;
public static readonly MAX_VALUE: number;
public static readonly MIN_VALUE: number;
public constructor(value: any = 0) {
// 实现略
}
public toFixed(p: any = 0): string {
// 实现略
return "";
}
public valueOf(): number {
// 实现略
return 0;
}
public toPrecision(p: any = 0): string {
// 实现略
return "";
}
public toString(radix: any = 10): string {
// 实现略
return "";
}
public toExponential(p: any = 0): string {
// 实现略
return "";
}
}
```
class C { public: C(int a) { x = a; } private: int x; }; class D :public C { public: D(int a) :y(a){} private: int y; };指出错误并说明原因
在类D的构造函数中,应该显式地调用其基类C的构造函数来初始化从C继承的成员变量x。否则,编译器会默认调用基类C的默认构造函数来初始化x,但是在C中并没有默认构造函数,因此编译会失败。正确的代码应该是:
```
class C {
public:
C(int a) { x = a; }
private:
int x;
};
class D : public C {
public:
D(int a) : C(a), y(a) {}
private:
int y;
};
```
这里在D的构造函数中显式地调用了C的构造函数来初始化x,同时也初始化了y。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)