构造方法那点事!

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Yes_butter/article/details/79238147

Java构造方法测试

class Child extends Father {//子类
    public int ID = 0;
    public Child()
    {
        super();//默认是一定会调用父类的构造方法的,不可以不调用
        ID=1;//这个是自己的ID,父类的ID可以通过super来访问
        System.out.println("我是子类的构造方法");
        System.out.println("我的ID是:"+ID+"\n我父类的ID:"+super.ID);
    }

    public Child(Child child)
    {
        //默认调用了super();
        this.ID=child.ID;
        System.out.println("我是子类的拷贝方法");
        System.out.println("我的ID是:"+ID+"\n我父类的ID:"+super.ID);
    }


    public int GetFatherID()//获取父类ID的方法
    {
        return super.ID;
    }
}


public class Father {

    public int ID = 0;

    public Father() {
        System.out.println("我是父类的构造方法");
    }

    public Father(Father father) {
        this.ID=father.ID;
        System.out.println("我是父类的拷贝构造方法");
    }

    static void Roleplay()
    {
        Father father1=new Father();
        System.out.println("------------------------我是华丽的分割线------------------");
        Father father2=new Father(father1);
        System.out.println("------------------------我是华丽的分割线------------------");
        Father father3;
        father3=father1;//调用了浅拷贝构造方法,好像是类似起了一个别名的方法。类似与c++的引用
        father3.ID=3;
        System.out.println("Father3ID:"+father3.ID+"\nFtaher1ID:"+father1.ID);

        System.out.println("------------------------我是华丽的分割线------------------");

        Child child1=new Child();
        System.out.println("------------------------我是华丽的分割线------------------");
        Child child2=new Child(child1);
        System.out.println("------------------------我是华丽的分割线------------------");
        Child child3;
        child3=child1;
        child3.ID=18;//同样的也是调用浅拷贝构造方法,类似起了一个别名。
        System.out.println("Child1的ID是:"+child1.ID+"\tChild3的父类的ID是:"+child1.GetFatherID());

    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Roleplay();
    }
}

运行结果:

关于父类的构造方法结论:

  • 1.调用那个构造方法使用那个构造方法
  • 2.用对象初始化对象的时候Java不会调用拷贝构造方法,而是采用了一种引用方法,给father1起了了名字叫father3,没有新的对象生成。

关于子类的构造方法结论:

  • 1.默认调用父类的构造方法,会父类同名参数和,同名函数。可以通过super来进行对父类的内容访问。

  • 2.用对象初始化对象的时候Java不会调用拷贝构造方法,而是采用了一种引用方法,给child1起了了名字叫child3,没有新的对象生成。

Java equals补充:

    public boolean equals(Father father)
    {
        if(this.ID==father.ID)
            return true;
        else
            return false;   
    }

    public boolean equals(Object obj)
    {
            return true;
    }

    static void Equalplay()
    {
        Father father1=new Father();
        Father father2=new Father();
        System.out.println(father1==father2);
        if(father1.equals(father2))
            System.out.println("Yes");
    }

调用之后发现实际没有覆盖==运算符,如果单纯调用.equals实现了覆盖!!!

运行结果是:
false
Yes

C++


#include<iostream>
using namespace std;

class Father
{
public:
    int ID = -1;

    Father()
    {
        ID = 0;
        cout << "我是父类无参的构造函数" << endl;
    }

    Father(int ID)
    {

            this->ID = ID;
            cout << "我是父类有参的构造函数" << endl;

    }
    Father(const Father &father)//需要传来一个引用
    {
        ID = father.ID;
        cout << "我是父类拷贝构造方法" << endl;
    }
    ~Father()
    {
        cout << ID<<":我是父类析构函数" << endl;
        cout << "-------------------我是华丽的分割线--------------------" << endl;
    }
};


class Child :public Father
{
public:
    int ID = -2;
    Child(int ID=0):Father(188)
    {
        if (ID == 0) {
            ID = 0;
            cout << "我是子类无参的构造函数" << endl;
        }
        else
        {
            this->ID = ID;
            cout << "我是子类有参的构造函数" << endl;
        }
    }
    Child(const Child &child)//默认调用无参数的父类构造函数
    {
        ID = child.ID;  
        cout << "我是子类的拷贝构造函数" << endl;
    }
    ~Child()
    {
        cout <<ID<< ":我是子类的析构函数" << endl;
    }
};

void RolePlay()
{
    Father father1(1);//调用有参构造函数
    cout << "-------------------我是华丽的分割线--------------------" << endl;
    Father father2(father1);//调用拷贝构造函数
    father2.ID = 2;
    cout << "-------------------我是华丽的分割线--------------------" << endl;
    Father father3 = father1;//调用拷贝构造函数
    cout << "Father3ID:" << father3.ID << endl;
    father3.ID = 3;
    cout << "-------------------我是华丽的分割线--------------------" << endl;
    Father father4;//调用无参构造函数,如果不写无参构造函数,则会默认生成
    cout << "Father4ID:" << father4.ID << endl;
    father4 = father1;//调用 运算符重载的 ‘=’,实现赋值,详细向后看!!!
    cout << "Father4ID:" << father4.ID << endl;
    father4.ID = 4;
    cout << "-------------------我是华丽的分割线--------------------" << endl;

    Child child1(11);//先调用父类的构造函数,如果不写调用父类构造函数,默认调用无参构造函数,然后调用子类有参构造函数
    cout << "-------------------我是华丽的分割线--------------------" << endl;
    Child child2(child1);//调用父类构造函数,同上,然后调用子类构拷贝构造函数
    child2.ID = 12;
    cout << "-------------------我是华丽的分割线--------------------" << endl;
    Child child3 = child1;//调用拷贝构造函数。同Child child3(child1)
    cout << "Child3ID:" << child3.ID << endl;
    child3.ID = 13;
    cout << "-------------------我是华丽的分割线--------------------" << endl;
    Child child4;//调用父类构造函数,调用子类无参构造函数。
    cout << "Child4ID:" << child4.ID << endl;
    child4 = child1;//调用 运算符重载的 '=',实现赋值,详细向后看!!!
    cout << "Child4ID:" << child4.ID << endl;
    child4 .ID= 14;
    cout << "-------------------我是华丽的分割线--------------------" << endl;
    /*根据构造的顺序,逆序析构生成对应的对象*/

}
int main()
{
    RolePlay();
    system("pause");
    return 0;
}

C++运行结果

关于基(父)类构造函数结论:

  • 1.调用那个构造方法使用那个构造方法
  • 2.用对象初始化对象的时候c++调用拷贝构造方法,等同使用拷贝构造函数!!!

关于派生(子)类构造函数结论:

  • 1.默认调用父类的构造方法,会覆盖父类同名参数和,同名函数。也可以通过 作用域名字来访问基类的内容。

  • 2.用对象初始化对象的时候c++调用拷贝构造方法,等同使用拷贝构造函数!!!

C++神秘的运算符重载

//父类写一个等号重载,可以实现连等赋值调用!!!
    Father& operator=(Father &father)
    {
        this->ID = 9999;
        cout << "我是运算符=重载" << endl;
        cout << "----------------------------------------------------------" << endl;
        return *this;
    }

c++结果

结论:

  • 相当于实现了等号的重新定义,可以增加相对应的功能。同时又增加了作用域的概念。相当于扩大等号的威力!!!

猜你喜欢

转载自blog.csdn.net/Yes_butter/article/details/79238147