c++_08_操作符重载(操作符重定义) 友元

1  操作符标记 

        单目操作符:        -    ++    --    *    ->    等

        双目操作符:        -    +    >    <    +=    -=    <<    >>    等

        三木操作符:        ? :  

2  操作符函数

2.0  前言

        C++编译器有能力把一个由操作数操作符组成的表达式,

        解释为对一个成员函数的调用,               a + b   -->   a.operator+( b )

        解释为对一个全员函数的调用。               a + b   -->   operator+( a,b )

        该  全员函数  或  成员函数  被称为操作符函数。两个函数不要重复定义。

        通过定义操作符函数,可以实现针对自定义类型的运算法则,并使之与基本类型一样参与各种表达式。

// complex_pre.cpp操作符函数
#include <iostream>
using namespace std;

class Human {
public:
    Human( int age=0, const char* name="无名" ) : m_age(age),m_name(name) {
        //【int m_age=age;】
        //【string m_name(name);】
    }
    void getinfo( ) {
        cout << "姓名: " << m_name << ", 年龄: " << m_age << endl;
    }
    Human sum( /* Human* this */ Human r ) {
        return Human(this->m_age+r.m_age, (this->m_name+"+"+r.m_name).c_str() );
    }
    Human sub( /* Human* this */ Human r ) {
        return Human(this->m_age-r.m_age, (this->m_name+"-"+r.m_name).c_str() );
    }
private:
    int m_age;
    string m_name;
};
// 模拟类的设计者(类库、别人设计的类、自己设计的类)
// --------------------------------
// 模拟用户(使用类的人)
int main( void ) {
    Human a(22,"张飞"), b(20,"赵云"), c(25,"关羽"), d(32,"马超");

    Human res = a.sum(b); // a + b; ==> a.operator+(b)  或  operator+(a,b)
    res.getinfo( );

    res = c.sub(d); // c - d; ==> c.operator-(d)  或 operator-(c,d)
    res.getinfo( );
    return 0;
}

2.1  单目运算符表达式    #O / O#

        成员函数形式:    O.operator# ()

        全局函数形式:    operator# (O)

2.2  双目运算符表达式    L#R

        成员函数形式:    L.operator# (R)    左调右参:左操作数是用对象,操作数是数对象

        全局函数形式:    operator# (L,R)    左一右二:操作数是第参数,操作数是第参数

2.3  三目运算符表达式    F#S#T

        无法重载。

3  典型双目操作符

3.1  运算类:  +  -  *  /  等

        左操作数可以为非常左值常左值右值 

        右操作数可以为非常左值常左值右值 

        表达式的结果为右值 (即,不是引用就行)

// double_operator1.cpp
#include <iostream>
using namespace std;

class Human { // 授权类(授予朋友权利的类)
public:
    Human( int age=0, const char* name="无名" ) : m_age(age),m_name(name) {
        //【int m_age=age;】
        //【string m_name(name);】
    }
    void getinfo( ) {
        cout << "姓名: " << m_name << ", 年龄: " << m_age << endl;
    }
    // 成员形式的操作符函数  万能指针是必要的      万能引用是必要的
//    Human operator+( /* const Human* this */ const Human& that ) const {
//        return Human(this->m_age+that.m_age, (this->m_name+"+"+that.m_name).c_str());
//    }
private:
    int m_age;
    string m_name;
    friend Human operator+( const Human& l, const Human& r ) ; // 友元声明
};
// 全局形式的操作符函数
Human operator+( const Human& l, const Human& r ) { 
    return Human(l.m_age+r.m_age, (l.m_name+"+"+r.m_name).c_str() );
}
// 模拟类的设计者(类库、别人设计的类、自己设计的类)
// --------------------------------
// 模拟用户(使用类的人)
int main( void ) {
    Human a(22,"张飞"), b(20,"赵云"); // 非常左值
    const Human c(25,"关羽"), d(32,"马超"); // 常左值

    Human res =  a + b; // ==> a.operator+(b)  或  operator+(a,b)
    res.getinfo( );
    
    res = c + d; // ==> c.operator+(d)  或  operator+(c,d)
    res.getinfo();

    res= Human(45,"黄忠")+ Human(35,"刘备");//Human(45,"黄忠").operator+(Human(35,"刘备"))
                                         //operator+( Human(45,"黄忠"), Human(35,"刘备"))
    res.getinfo();
    /*
    int a=10, b=20; // 非常左值
    const int c=30, d=40; // 常左值

    |30| a + b;
    a + c;
    a + 5;
    c + b;
    5 + b;
    */
    return 0;
}

3.2  赋值类  =  +=  -=  *=  /=  等

        左操作数必须为非常左值 

        右操作数可以为非常左值常左值右值 

        表达式结果为左操作数本身(而非副本)

        operator=()就是拷贝赋值函数,编译器会默认给。

// double_operator2.cpp
#include <iostream>
using namespace std;

class Human { // 授权类(授予朋友权利的类)
public:
    Human( int age=0, const char* name="无名" ) : m_age(age),m_name(name) {
        //【int m_age=age;】
        //【string m_name(name);】
    }
    void getinfo( ) {
        cout << "姓名: " << m_name << ", 年龄: " << m_age << endl;
    }
    // 成员形式的操作符函数
    Human& operator+=( /* Human* this */ const Human& that ) {
        this->m_age = this->m_age + that.m_age;
        this->m_name = this->m_name+"+"+that.m_name;
        return *this;
    }
private:
    int m_age;
    string m_name;
};
// 全局形式的操作符函数(自己课下写)

// 模拟类的设计者(类库、别人设计的类、自己设计的类)
// --------------------------------
// 模拟用户(使用类的人)
int main( void ) {
    Human a(22,"张飞"), b(20,"赵云"); // 非常左值
    const Human c(25,"关羽"), d(32,"马超"); // 常左值

    ((a+=b)+=c)+=Human(45,"黄忠");
    a.getinfo();
    /*
    a += b; // a.operator+=(b)  或  operator+=(a,b)
    a.getinfo();

    a += c; // a.operator+=(c)  或  operator+=(a,c)
    a.getinfo();

    a += Human(45,"黄忠"); //   a.operator+=(Human(45,"黄忠")) 
                          // 或 operator+=(a,Human(45,"黄忠"))
    a.getinfo();
    */

    /*
    int a=10, b=20; // 非常左值
    const int c=30, d=40; // 常左值

    a = b; 
    a = c;
    a = 5;
    c = b; // error
    5 = b; // error
    */
    return 0;
}

3.3  比较类   >   <   ==   <=   >=   等

        左操作数为非常左值常左值右值 

        右操作数为非常左值常左值右值

        表达式结果为bool 

// double_operator3.cpp
#include <iostream>
using namespace std;

class Human { // 授权类(授予朋友权利的类)
public:
    Human( int age=0, const char* name="无名" ) : m_age(age),m_name(name) {
        //【int m_age=age;】
        //【string m_name(name);】
    }
    void getinfo( ) {
        cout << "姓名: " << m_name << ", 年龄: " << m_age << endl;
    }
    // 成员形式的操作符函数
    bool operator==( /* const Human* this */ const Human& that ) const {
        return this->m_age==that.m_age && this->m_name==that.m_name;
    }
    bool operator!=( /* const Human* this */ const Human& that ) const {
//      return this->m_age!=that.m_age || this->m_name!=that.m_name;
        return !(*this==that);
    }
private:
    int m_age;
    string m_name;
};
// 全局形式的操作符函数(自己课下写)

// 模拟类的设计者(类库、别人设计的类、自己设计的类)
// --------------------------------
// 模拟用户(使用类的人)
int main( void ) {
    Human a(22,"张飞"), b(20,"赵云"); // 非常左值
    const Human c(25,"关羽"), d(32,"马超"); // 常左值

    cout << (a == b) << endl; // a.operator==(b)  或 ...
    cout << (a != b) << endl; // a.operator!=(b)  或 ...

    cout << (c == d) << endl; // c.operator==(d)  或 ...
    cout << (c != d) << endl; // c.operator!=(d)  或 ...

    cout << (Human(45,"黄忠")==Human(35,"刘备")) << endl; 
                              // Human(45,"黄忠").operator==(Human(35,"刘备"))
    cout << (Human(45,"黄忠")!=Human(35,"刘备")) << endl; 
                              // Human(45,"黄忠").operator!=(Human(35,"刘备"))

    /*
    int a=10, b=20; // 非常左值
    const int c=30, d=40; // 常左值

    a == b;
    a == c;
    a == 5;
    c == b;
    5 == b;

    */
    return 0;
}

4  典型单目操作符

4.1  运算类  -    ~    !   等

        操作数为非常左值常左值右值 

        表达式的结果为右值 

// single_operator1.cpp
#include <iostream>
using namespace std;

class Human { // 授权类(授予朋友权利的类)
public:
    Human( int age=0, const char* name="无名" ) : m_age(age),m_name(name) {
        //【int m_age=age;】
        //【string m_name(name);】
    }
    void getinfo( ) {
        cout << "姓名: " << m_name << ", 年龄: " << m_age << endl;
    }
    // 成员形式的操作符函数
    Human operator-( /* const Human* this */ ) const  {
        return Human(-this->m_age, ("-"+this->m_name).c_str() );
    }
private:
    int m_age;
    string m_name;
};
// 全局形式的操作符函数(自己课下写)

// 模拟类的设计者(类库、别人设计的类、自己设计的类)
// --------------------------------
// 模拟用户(使用类的人)
int main( void ) {
    Human a(22,"张飞"), b(20,"赵云"); // 非常左值
    const Human c(25,"关羽"), d(32,"马超"); // 常左值

    Human res = -a; // a.operator-()  或 ... 
    res.getinfo();

    res = -c; // c.operator-()  或 ...
    res.getinfo();

    res = -Human(45,"黄忠"); // Human(45,"黄忠").operator-()  或 ...
    res.getinfo();

    /*
    int a=10, b=20; // 非常左值
    const int c=30, d=40; // 常左值

    |-10| -a; // ok
    |-30| -c; // ok
    |-40| -40;// ok
    */
    return 0;
}

4.2  前自增减类  ++O  --O

        操作数为非常左值 

        表达式结果为操作数本身(而非副本)

4.3  后自增减类  O++  O--

        操作数为非常左值 

        表达式的结果为右值,且为自增减以前的值

// single_operator2.cpp
#include <iostream>
using namespace std;

class Human { // 授权类(授予朋友权利的类)
public:
    Human( int age=0, const char* name="无名" ) : m_age(age),m_name(name) {
        //【int m_age=age;】
        //【string m_name(name);】
    }
    void getinfo( ) {
        cout << "姓名: " << m_name << ", 年龄: " << m_age << endl;
    }
    // 成员形式的操作符函数
    Human& operator++( /* Human* this */ ) {
        this->m_age += 1; // 直接就加1
        return *this;
    }
    Human operator++( /* Human* this */ int ) {
        Human old = *this; // 克隆一份b原来的值
        this->m_age += 1; // 直接就加1
        return old; // 返回的为克隆的b原来的值
    } 
private:
    int m_age;
    string m_name;
};
// 全局形式的操作符函数(自己课下写)

// 模拟类的设计者(类库、别人设计的类、自己设计的类)
// --------------------------------
// 模拟用户(使用类的人)
int main( void ) {
    Human a(22,"张飞"), b(20,"赵云"); // 非常左值
    const Human c(25,"关羽"), d(32,"马超"); // 常左值

    (++a).getinfo(); // a.operator++()  或  operator++(a)

    (/*|...|*/b++).getinfo(); // b.operator++(0) 或  operator++(b,0) 
    b.getinfo();
    /*
    int a=10, b=20; // 非常左值
    const int c=30, d=40; // 常左值

    ++a; // ok
    ++c; // error
    ++5; // error

    b++; // ok
    c++; // error
    5++; // error
    */
    return 0;
}

5  其他操作符

5.1  输出流操作符  <<

        左操作数(cout)为  非常左值  形式的输出流( ostream )对象

        右操作数为  左值或右值

        表达式的结果为左操作符本身(而非副本)

        

        左操作数的类型为ostream,

        (若以成员函数形式重载该操作符,就应将其定义为ostream类的成员),

        但该类为标准库提供,无法添加新的成员,因此只能以全局函数形式重载该操作符:

        ostream&  operator<< ( ostream& os,  const RTGHT& right ) { ... } 

5.2  输入流操作符  >>

        左操作数为  非常左值  形式的输入流( istream )对象

        右操作数为  非常左值 

        表达式的结果为左操作符本身(而非副本)

        左操作数的类型为istream,

        (若以成员函数形式重载该操作符,就应将其定义为istream类的成员),

        但该类为标准库提供,无法添加新的成员,因此只能以全局函数形式重载该操作符:

        istream&  operator>> ( istream& is,  RIGHT& right ) { ... } 

// io.cpp
#include <iostream>
using namespace std;

class Human { 
public:
    Human( int age=0, const char* name="无名" ) : m_age(age),m_name(name) {
        //【int m_age=age;】
        //【string m_name(name);】
    }
    void getinfo( ) {
        cout << "姓名: " << m_name << ", 年龄: " << m_age << endl;
    }
private:
    int m_age;
    string m_name;
    friend ostream& operator<<( ostream& os, const Human& that ) ;
    friend istream& operator>>( istream& is, Human& that ) ;
};
// 全局形式的操作符函数(自己课下写)
ostream& operator<<( ostream& os, const Human& that ) {
    os << "姓名:" << that.m_name << ", 年龄:" << that.m_age;
    return os;
}
istream& operator>>( istream& is, Human& that ) {
    is >> that.m_name >> that.m_age;
    return is; 
}
// 模拟类的设计者(类库、别人设计的类、自己设计的类)
// --------------------------------
// 模拟用户(使用类的人)
int main( void ) {
    Human a(22,"张飞"), b(20,"赵云"); // 非常左值
    const Human c(25,"关羽"), d(32,"马超"); // 常左值

//    a.getinfo();
    cout << a << endl; // operator<<(cout, a)
    cout << c << endl; // operator<<(cout, c)
    cout << Human(45,"黄忠") << endl; // operator<<(cout, Human(45,"黄忠") )

    cin >> a; //  operator>>(cin,a)
    cout << a << endl;

    /*
     
    int a=10, b=20; // 非常左值
    const int c=30, d=40; // 常左值

    cout << a;
    cout << c;
    cout << 5;

    */
    return 0;
}

5.3  下标操作符  []

        一般用于在容器类型中以下标方式获取数据元素

        非常容器的元素为非常左值

        常容器的元素为常左值

// stack.cpp 
// 简易的栈容器 -- 先进后出
#include <iostream>
using namespace std;

class Stack {
public:
    Stack() : m_s(0) {
        //【int arr[20];】
        //【int m_s=0;】
    }
    void push(int data) {  arr[m_s++] = data; } // 判满操作,自己课下写
    int pop() { return arr[--m_s];  } // 判空操作,自己课下写
    int size() {  return m_s; }
    const int& operator[]( /* const Stack* this */ size_t i) const { // 常函数
        return this->arr[i];
    }
    int& operator[]( /* Stack* this */ size_t i) { // 非常函数
        return this->arr[i];
    }
private:
    int arr[20]; // 保存数据
    int m_s;     // 保存数据个数
};

// 模拟类的设计者(类库、别人设计的类、自己设计的类)
// --------------------------------
// 模拟用户(使用类的人)
int main( void ) {
    Stack s; // 非常容器
    for( int i=0; i<20; i++ ) {
        s.push( 1000+i );
    }
    cout << "压栈后s容器中数据的个数:" << s.size() << endl;
    s[5] = 888; // 非常容器的元素,就是非常左值   s.operator[](5)=888
    for( int i=0; i<20; i++ ) {
        cout << s[i] << ' ';
    }
    cout << endl;
    cout << "读数据后s容器中数据的个数:" << s.size() << endl;

    const Stack cs = s; // cs是常容器
    cs[5] = 999; // cs.operator[](5)=999    应该让编译器报readonly错误
    
    /*
    int s[20] = {...}; // s是非常容器,s的元素就是非常左值
    s[5] = 888;

    const int cs[20] = {...}; // cs是常容器,cs的元素就是常左值
    cs[5] = 999; // 报告readonly错误
    */
    return 0;
}

5.4  类型转换操作符

        1)若源类型是基本类型,目标类型是类型,则

        只能通过类型转换 构造 函数实现自定义类型转换:

                class 目标类型 {

                        目标类型 ( const 源类型& src ) {...}

                };

        2)若源类型是类型,目标类型是基本类型,则

        只能通过类型转换 操作符 函数实现自定义类型转换:

                class 源类型 {

                        operator 目标类型 (void) const {...}

                };

        3)若源类型和目标类型都是类型,则

        既优先通过类型转换 构造 函数,其次通过类型转换 操作符 函数实现自定义类型转换。

        但两者没必要同时使用。

        4)若源类型和目标类型都是基本类型,则

        无法实现自定义类型转换,基本类型之间的转换规则是由编译器内置的隐式转换。

// cast1.cpp
// 类型转换构造函数 和 类型转换操作符函数
#include <iostream>
using namespace std;
class Integer {
public:
    Integer(int i):m_i(i) {
        //【int m_i=i;】
        cout << "Integer类的类型转换构造函数被调用" << endl;
    }
    operator int( /* const Integer* this */ ) const {
        cout << "Integer类的类型转换操作符函数被调用" << endl;
        return this->m_i;
    }
private:
    int m_i;
};
// 模拟类的设计者(类库、别人设计的类、自己设计的类)
// --------------------------------
// 模拟用户(使用类的人)
int main( void ) {
    int m = 100;
    
    // int-->Integer (基本类型-->类类型)
    Integer ix = m; // 定义 匿名Integer类对象,利用 匿名Integer类对象.Integer(m)
                       ->类型转换构造函数
                    // Integer ix = m.operator Integer()
                       -->int类中绝对没有一个operator Integer成员函数(走不通)

    // Integer-->int (类类型-->基本类型)
    int n = ix; 
    // 定义 匿名int类对象,利用 匿名int类对象.int(ix)
       -->int类中绝对没有一个形参为Integer的构造函数(走不通)
    // int n = ix.operator int() --> 类型转换操作符函数
    return 0;
}
// cast2.cpp
// 类型转换构造函数/类型转换操作符函数 -- 指定 源类型 到 目标类型 的 转换规则
#include <iostream>
using namespace std;
class Dog; // 短式声明
class Cat {
public:
    Cat( const char* name ) : m_name(name) { 
        //【string m_name(name);】
    }
    void talk( ) {
        cout << m_name << ": 喵喵~~~" << endl;
    }
    operator Dog( /* const Cat* this */ ) const; // 声明
private:
    string m_name;
    friend class Dog; // 友元声明
};

class Dog {
public:
    Dog( const char* name ) : m_name(name) {
        //【string m_name(name);】
    }
    Dog( const Cat& c ) : m_name(c.m_name) { //类型转换构造函数(Cat-->Dog的转换规则)
        //【string m_name=c.m_name;】
        cout << "Dog类的类型转换构造函数被调用" << endl;
    }
    void talk( ) {
        cout << m_name << ": 汪汪~~~" << endl;
    }
private:
    string m_name;
};
Cat::operator Dog( /* const Cat* this */ ) const { // 定义
    cout << "Cat类的类型转换操作符函数被调用" << endl;
    return Dog( this->m_name.c_str() );
}
// 模拟类的设计者(类库、别人设计的类、自己设计的类)
// --------------------------------
// 模拟用户(使用类的人)
int main( void ) {
    Cat smallwhite("小白"); 
    // Cat-->Dog (类类型-->类类型)
    Dog bigyellow = smallwhite; //定义 匿名Dog类对象,利用 匿名Dog类对象.Dog(smallwhite)
                                   ->类型转换构造函数 
                                //Dog bigyellow = smallwhite.operator Dog()
                                   ->类型转换操作符函数
    return 0;
}

6  操作符重载的局限

        1)不是所有的操作符都能重载,以下操作符就不能重载:

                作用域限定操作符        ::  

                直接成员访问操作符        .  

                条件操作符        ? :  

                字节长度操作符        sizeof  

                类型信息操作符        typeid  

        2)所有操作数均为基本类型的操作符也不能重载:

                1 + 1 = 8 ?

7  友元

        可以通过friend关键字,把

        一个全局函数、另一个类的成员函数、另一个整体,

        声明为授权类的友元。

        友元拥有访问授权类任何非公有成员的特权。(即访问所有成员)

        友元声明可以出现在授权类的公有、私有、保护等任何区域,且不受  访问控制限定符  的约束。

        友元是成员,其作用域并隶属于授权类,也拥有授权类类型的this指针。

// twoDimensional_08.cpp 设计一个二维坐标系的 类
#include <iostream>
using namespace std;

class TwoDimensional {
public:
    TwoDimensional( int x=0, int y=0 ) {
        // 在this指向的内存空间中 定义m_x初值为随机数
        // 在this指向的内存空间中 定义m_y初值为随机数
        m_x = x;
        m_y = y;
    }
    TwoDimensional operator+( /* const TwoDimensional* this */ const TwoDimensional& that ) const {
        return TwoDimensional( this->m_x+that.m_x, this->m_y+that.m_y );
    }
    TwoDimensional& operator=( /* TwoDimensional* this */ const TwoDimensional& that ) {
        this->m_x = that.m_x;
        this->m_y = that.m_y;
        return *this;
    }
    bool operator>( /* const TwoDimensional* this */ const TwoDimensional& that ) const {
        return this->m_x*this->m_x+this->m_y*this->m_y > that.m_x*that.m_x + that.m_y*that.m_y;
    }
    TwoDimensional operator-( /* const TwoDimensional* this */ ) const {
        return TwoDimensional( -this->m_x, -this->m_y );
    }
    TwoDimensional& operator++( /* TwoDimensional& this*/ ) {
        ++this->m_x;
        ++this->m_y;
        return *this;
    }
private:
    int m_x; // 横坐标
    int m_y; // 纵坐标
    friend ostream& operator<<( ostream& os, const TwoDimensional& that );
};
ostream& operator<<( ostream& os, const TwoDimensional& that ) {
    os << "横坐标:" << that.m_x << ", 纵坐标:" << that.m_y;
    return os;
}

class ThreeDimensional {
public:
    ThreeDimensional( int x=0, int y=0, int z=0 ) : m_x(x),m_y(y),m_z(z) {}
    operator TwoDimensional( /* const ThreeDimensional* this */ ) const {
        return TwoDimensional( this->m_x, this->m_y );
    }
private:
    int m_x;
    int m_y;
    int m_z;
};
int main( void ) {
    TwoDimensional a(1,2), b(3,4);

    cout << "a-->" << a  << ", b-->" << b << endl; // cout 打印 坐标对象
    
    TwoDimensional res = a + b; // 坐标间求和
    cout << res << endl;
    a = b; // 坐标间赋值
    cout << a << endl;
    cout << (a > b) << endl; // 坐标间比较大小
    res = -a; // 坐标取反
    cout << res << endl;
    cout << ++a << endl; // 坐标自增

    ThreeDimensional c(100,200,300);

    TwoDimensional d = c; // TwoDimensional d = c.operator TwoDimensional();
    cout << d << endl;
    return 0;
}

内容概要:本文档详细介绍了在三台CentOS 7服务器(IP地址分别为192.168.0.157、192.168.0.158和192.168.0.159)上安装和配置Hadoop、Flink及其他大数据组件(如Hive、MySQL、Sqoop、Kafka、Zookeeper、HBase、Spark、Scala)的具体步骤。首先,文档说明了环境准备,包括配置主机名映射、SSH免密登录、JDK安装等。接着,详细描述了Hadoop集群的安装配置,包括SSH免密登录、JDK配置、Hadoop环境变量设置、HDFS和YARN配置文件修改、集群启动与测试。随后,依次介绍了MySQL、Hive、Sqoop、Kafka、Zookeeper、HBase、Spark、Scala和Flink的安装配置过程,包括解压、环境变量配置、配置文件修改、服务启动等关键步骤。最后,文档提供了每个组件的基本测试方法,确保安装成功。 适合人群:具备一定Linux基础和大数据组件基础知识的运维人员、大数据开发工程师以及系统管理员。 使用场景及目标:①为大数据平台建提供详细的安装指南,确保各组件能够顺利安装和配置;②帮助技术人员快速掌握Hadoop、Flink等大数据组件的安装与配置,提升工作效率;③适用于企业级大数据平台的建与维护,确保集群稳定运行。 其他说明:本文档不仅提供了详细的安装步骤,还涵盖了常见的配置项解释和故障排查建议。建议读者在安装过程中仔细阅读每一步骤,并根据实际情况调整配置参数。此外,文档中的命令和配置文件路径均为示例,实际操作时需根据具体环境进行适当修改。
在无线通信领域,天线阵列设计对于信号传播方向和覆盖范围的优化至关重要。本题要求设计一个广播电台的天线布局,形成特定的水平面波瓣图,即在东北方向实现最大辐射强度,在正东到正北的90°范围内辐射衰减最小且无零点;而在其余270°范围内允许出现零点,且正西和西南方向必须为零。为此,设计了一个由4个铅垂铁塔组成的阵列,各铁塔上的电流幅度相等,相位关系可自由调整,几何布置和间距不受限制。设计过程如下: 第一步:构建初级波瓣图 选取南北方向上的两个点源,间距为0.2λ(λ为电磁波波长),形成一个端射阵。通过调整相位差,使正南方向的辐射为零,计算得到初始相位差δ=252°。为了满足西南方向零辐射的要求,整体相位再偏移45°,得到初级波瓣图的表达式为E1=cos(36°cos(φ+45°)+126°)。 第二步:构建次级波瓣图 再选取一个点源位于正北方向,另一个点源位于西南方向,间距为0.4λ。调整相位差使西南方向的辐射为零,计算得到相位差δ=280°。同样整体偏移45°,得到次级波瓣图的表达式为E2=cos(72°cos(φ+45°)+140°)。 最终组合: 将初级波瓣图E1和次级波瓣图E2相乘,得到总阵的波瓣图E=E1×E2=cos(36°cos(φ+45°)+126°)×cos(72°cos(φ+45°)+140°)。通过编程实现计算并绘制波瓣图,可以看到三个阶段的波瓣图分别对应初级波瓣、次级波瓣和总波瓣,最终得到满足广播电台需求的总波瓣图。实验代码使用MATLAB编写,利用polar函数在极坐标下绘制波瓣图,并通过subplot分块显示不同阶段的波瓣图。这种设计方法体现了天线阵列设计的基本原理,即通过调整天线间的相对位置和相位关系,控制电磁波的辐射方向和强度,以满足特定的覆盖需求。这种设计在雷达、卫星通信和移动通信基站等无线通信系统中得到了广泛应用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值