“Polynomial::coefficients”: 无法访问 private 成员(在“Polynomial”类中声明); “Polynomial::count”: 无法访问 private 成员(在“Polynomial”类中声明)
时间: 2023-11-19 16:05:43 浏览: 34
这个错误提示是因为你在访问私有成员变量或者私有成员函数。私有成员只能在类内部被访问,类的外部不能直接访问私有成员。
解决这个问题的方法就是将私有成员改为公有成员或者提供公有的接口函数来访问私有成员。如果你的类中有一些变量或者函数只需要在类内部使用,那么就应该将它们声明为私有成员。这样可以避免在类外部对这些成员的误操作。
如果你需要在类外部访问私有成员,可以通过提供公有的接口函数来实现。例如,如果你需要访问一个私有的成员变量,可以提供一个公有的get函数来获取成员变量的值,或者提供一个set函数来设置成员变量的值。
另外,如果你需要访问一个私有的成员函数,可以提供一个公有的接口函数来调用私有函数。这个公有接口函数可以访问私有成员变量,然后调用私有函数完成操作。
总之,私有成员只能在类内部被访问,如果需要在类外部访问私有成员,就需要提供公有的接口函数来实现。
相关问题
AttributeError: ‘function’ object has no attribute 'coefficients’
非常抱歉,我之前的代码有误,`'function' object has no attribute 'coefficients'` 错误提示是因为在 `generate_polynomial` 函数中没有定义 `coefficients` 属性。以下是修正后的代码:
```python
class Polynomial:
def __init__(self, coefficients):
self.coefficients = coefficients
def __call__(self, x):
res = 0
for i, c in enumerate(self.coefficients):
res += c * x ** i
return res
def distributed_polynomial_calculation(polynomial, node_count, x):
"""
分布式多项式计算
"""
sub_polynomials = np.array_split(polynomial.coefficients, node_count)
results = []
for sub_polynomial in sub_polynomials:
sub_p = Polynomial(sub_polynomial)
results.append(sub_p(x))
return sum(results)
# 生成多项式
coefficients = np.random.rand(100000)
polynomial = Polynomial(coefficients)
# 测试单节点计算时间和内存
start_time = time.time()
res = polynomial(0.1)
end_time = time.time()
print(f"单节点计算时间:{end_time - start_time}")
print(f"单节点内存消耗:{polynomial.coefficients.nbytes}")
# 测试分布式多项式计算时间和内存
node_counts = [2, 4, 8]
x = 0.1
for node_count in node_counts:
start_time = time.time()
res = distributed_polynomial_calculation(polynomial, node_count, x)
end_time = time.time()
print(f"{node_count} 个节点计算时间:{end_time - start_time}")
print(f"{node_count} 个节点内存消耗:{polynomial.coefficients.nbytes / node_count}")
```
在这个修正后的代码中,我们定义了一个多项式类 `Polynomial`,并将 `coefficients` 属性存储在类的实例变量中。然后,我们使用 `Polynomial` 类来生成多项式,并在 `distributed_polynomial_calculation` 函数中使用 `Polynomial` 类来计算子多项式的结果。这样就可以避免 `'function' object has no attribute 'coefficients'` 的错误。
polynomial类
Polynomial类是一个表示多项式的数据类型,可以用于多项式的存储、计算和操作。在该类中,通常使用一个数组来表示多项式的系数,每个元素对应于某个幂次项的系数。
下面是一个简单的Polynomial类的示例:
```java
public class Polynomial {
private double[] coefficients;
public Polynomial(double... coefficients) {
this.coefficients = coefficients;
}
public int degree() {
return coefficients.length - 1;
}
public double getCoefficient(int degree) {
return coefficients[degree];
}
public double evaluate(double x) {
double result = 0;
for (int i = coefficients.length - 1; i >= 0; i--) {
result = result * x + coefficients[i];
}
return result;
}
public Polynomial add(Polynomial other) {
int degree = Math.max(this.degree(), other.degree());
double[] result = new double[degree + 1];
for (int i = 0; i <= degree; i++) {
result[i] = this.getCoefficient(i) + other.getCoefficient(i);
}
return new Polynomial(result);
}
public Polynomial subtract(Polynomial other) {
int degree = Math.max(this.degree(), other.degree());
double[] result = new double[degree + 1];
for (int i = 0; i <= degree; i++) {
result[i] = this.getCoefficient(i) - other.getCoefficient(i);
}
return new Polynomial(result);
}
public Polynomial multiply(Polynomial other) {
double[] result = new double[this.degree() + other.degree() + 1];
for (int i = 0; i <= this.degree(); i++) {
for (int j = 0; j <= other.degree(); j++) {
result[i + j] += this.getCoefficient(i) * other.getCoefficient(j);
}
}
return new Polynomial(result);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (int i = this.degree(); i >= 0; i--) {
double coefficient = this.getCoefficient(i);
if (coefficient != 0) {
if (coefficient > 0 && sb.length() > 0) {
sb.append("+");
}
if (coefficient != 1 || i == 0) {
sb.append(coefficient);
}
if (i > 0) {
sb.append("x");
if (i > 1) {
sb.append("^").append(i);
}
}
}
}
return sb.toString();
}
}
```
在上述代码中,Polynomial类包含了以下操作:
- 构造函数:使用一个可变长度参数来表示多项式的系数。
- degree()方法:返回多项式的最高次项次数。
- getCoefficient(int degree)方法:返回多项式某个幂次项的系数。
- evaluate(double x)方法:计算多项式在某个值x处的值。
- add(Polynomial other)方法:将当前多项式与另一个多项式other相加,返回一个新的多项式。
- subtract(Polynomial other)方法:将当前多项式减去另一个多项式other,返回一个新的多项式。
- multiply(Polynomial other)方法:将当前多项式与另一个多项式other相乘,返回一个新的多项式。
- toString()方法:返回多项式的字符串表示。
使用Polynomial类可以方便地进行多项式的表示和计算,例如:
```java
Polynomial p1 = new Polynomial(1, 2, 3); // 3x^2 + 2x + 1
Polynomial p2 = new Polynomial(-1, 0, 1); // x^2 - 1
Polynomial sum = p1.add(p2); // 4x^2 + 2x
Polynomial product = p1.multiply(p2); // -x^4 + x^2 + 2x - 3
System.out.println(sum); // "4x^2+2x"
System.out.println(product); // "-x^4+x^2+2.0x-3.0"
```
相关推荐
![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)