inner class & inner static class diff

本文主要讨论了Java中的内嵌类(InnerClasses)和内嵌静态类(InnerStaticClasses)之间的关键区别,包括它们对外层类的关联性、对外部成员的访问权限以及是否可以包含静态成员。

In Java, inner classes and inner static classes are both nested classes, but they have some key differences.

  1. Inner Class:

    • An inner class is a class defined within another class.
    • It has access to the instance variables and methods of the outer class (including private members).
    • An instance of an inner class cannot exist without an instance of the outer class.
    • Inner classes are associated with instances of the outer class, so they have access to instance members of the outer class.
    • Inner classes cannot contain static members (other than compile-time constant fields).
    • Example:
      
      
      public class Outer {
          private int outerField;
          
          public class Inner {
              public void doSomething() {
                  outerField = 10; // Accessing outer class member
              }
          }
      }
      

  2. Inner Static Class (Static Nested Class):

    • An inner static class is a static nested class defined within another class.
    • It does not have access to the instance variables and methods of the outer class.
    • An instance of a static nested class can exist without an instance of the outer class.
    • Static nested classes are associated with the class itself, not with instances of the outer class.
    • Static nested classes can contain static members.
    • Example:
      public class Outer {
          private static int outerStaticField;
          
          public static class StaticInner {
              public void doSomething() {
                  outerStaticField = 10; // Accessing outer class static member
              }
          }
      }
      

In summary, the main differences between inner classes and inner static classes in Java are related to their association with the outer class, access to outer class members, and whether they can have static members.

将如下基于模版的函数类型T改为float型:#ifndef IMAGE_SMOOTHER_H #define IMAGE_SMOOTHER_H #include <vector> #include <cmath> #include <algorithm> #include <limits> #include <type_traits> #include <cstdint> // 可选:开启 OpenMP 加速(需要在编译时添加 -fopenmp / /openmp) #ifdef _OPENMP #include <omp.h> #endif // 像素值裁剪函数:将double值转换为目标类型T并裁剪到有效范围 template<typename T> T clamp_val(double v) { if constexpr (std::is_floating_point<T>::value) { return static_cast<T>(v); // 浮点类型直接转换 } else { // 整数类型:先四舍五入,再裁剪到[0, max] double lo = 0.0; double hi = static_cast<double>(std::numeric_limits<T>::max()); v = std::round(v); if (v < lo) v = lo; if (v > hi) v = hi; return static_cast<T>(v); } } // 高性能中心区域平滑处理函数(0索引版本) // 与原函数行为保持一致:is_horizontal = true 时按行修复(跨列),为 false 时按列修复(跨行)。 // 参数语义与原来一致:nn 过渡宽度,smoothWin 平滑窗口(移动平均),thr1 裁剪阈值。 template<typename T> std::vector<std::vector<T>> smooth_center_step_0idx( const std::vector<std::vector<T>>& A, int nn, int smoothWin, int thr1, bool is_horizontal = true ) { // 快速检查 & 尺寸 int H = static_cast<int>(A.size()); if (H == 0) return {}; int W = static_cast<int>(A[0].size()); if (W == 0) return A; nn = std::max(1, nn); smoothWin = std::max(1, smoothWin); int outer_dim = is_horizontal ? H : W; int inner_dim = is_horizontal ? W : H; int mid = inner_dim / 2; int idx_left = std::clamp(mid - 1, 0, inner_dim - 1); int idx_right = std::clamp(mid + 1, 0, inner_dim - 1); // 将输入转换为连续的 double 缓冲区 Bdata,行主序 (H x W) std::vector<double> Bdata; Bdata.resize(static_cast<size_t>(H) * static_cast<size_t>(W)); { // 拷贝(一次线性遍历) for (int r = 0; r < H; ++r) { const auto &row = A[r]; double* dst = &Bdata[static_cast<size_t>(r) * W]; for (int c = 0; c < W; ++c) dst[c] = static_cast<double>(row[c]); } } // Step 1: 计算 tt 向量(outer_dim 长度),并裁剪到 [-thr1, thr1] std::vector<double> tt(static_cast<size_t>(outer_dim)); { const double thr = static_cast<double>(thr1); if (is_horizontal) { // 行方向:对每行,取列 idx_left / idx_right for (int i = 0; i < outer_dim; ++i) { double v_left = Bdata[static_cast<size_t>(i) * W + idx_left]; double v_right = Bdata[static_cast<size_t>(i) * W + idx_right]; double diff = v_right - v_left; if (diff > thr) diff = thr; else if (diff < -thr) diff = -thr; tt[i] = diff; } } else { // 列方向:对每列,取行 idx_left / idx_right for (int i = 0; i < outer_dim; ++i) { double v_left = Bdata[static_cast<size_t>(idx_left) * W + i]; double v_right = Bdata[static_cast<size_t>(idx_right) * W + i]; double diff = v_right - v_left; if (diff > thr) diff = thr; else if (diff < -thr) diff = -thr; tt[i] = diff; } } } // Step 2: 对 tt 做移动平均(使用前缀和实现 O(1) 查询) if (smoothWin > 1) { int n = outer_dim; std::vector<double> pref(static_cast<size_t>(n + 1)); pref[0] = 0.0; for (int i = 0; i < n; ++i) pref[i + 1] = pref[i] + tt[i]; std::vector<double> t2(static_cast<size_t>(n)); int k = smoothWin / 2; for (int i = 0; i < n; ++i) { int lo = std::max(0, i - k); int hi = std::min(n - 1, i + k); double sum = pref[hi + 1] - pref[lo]; t2[i] = sum / (hi - lo + 1); } tt.swap(t2); } // Step 3: 生成线性权重 w[j] = (j+1)/nn std::vector<double> w(static_cast<size_t>(nn)); for (int j = 0; j < nn; ++j) w[j] = static_cast<double>(j + 1) / nn; // Step 4: 应用修正:左/上增加(mid-nn .. mid-1),右/下减少(mid .. mid+nn-1) // 并行化外循环(每个 outer i 的修改是独立的),如果编译器开启了 OpenMP 则会并行。 #ifdef _OPENMP #pragma omp parallel for schedule(static) #endif for (int i = 0; i < outer_dim; ++i) { double tt_i = tt[i]; // 左/上区域修正 for (int j = 0; j < nn; ++j) { int pos = mid - nn + j; if (pos < 0 || pos >= inner_dim) continue; if (is_horizontal) { // row i, col pos Bdata[static_cast<size_t>(i) * W + pos] += tt_i * w[j]; } else { // row pos, col i Bdata[static_cast<size_t>(pos) * W + i] += tt_i * w[j]; } } // 右/下区域修正 for (int j = 0; j < nn; ++j) { int pos = mid + j; if (pos < 0 || pos >= inner_dim) continue; // 注意原实现中右侧减的是 0.5 * tt * reversed_weight double add = -0.5 * tt_i * w[nn - 1 - j]; if (is_horizontal) { Bdata[static_cast<size_t>(i) * W + pos] += add; } else { Bdata[static_cast<size_t>(pos) * W + i] += add; } } } // Step 5: 转换回原类型并裁剪,输出为 vector<vector<T>> std::vector<std::vector<T>> out(static_cast<size_t>(H), std::vector<T>(static_cast<size_t>(W))); for (int r = 0; r < H; ++r) { T* dstRow = out[r].data(); const double* srcRow = &Bdata[static_cast<size_t>(r) * W]; for (int c = 0; c < W; ++c) { dstRow[c] = clamp_val<T>(srcRow[c]); } } return out; } #endif // IMAGE_SMOOTHER_H
11-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值