zoukankan      html  css  js  c++  java
  • JAVA之等号、传类对象参数与c++的区别

    在JAVA中用等号对类对象进行赋值,实际上操作的是对象的地址。

    eg:

    package MyText;
    
    class ClassA
    {
    	int value;
    	public void seta(int value)
    	{
    		this.value = value;
    	}
    	public void show()
    	{
    		System.out.println("the value:" + value);
    	}
    }
    public class MyText {
    	public static void main (String []args)
    	{
    		ClassA a = new ClassA();
    		a.seta(1);
    		a.show();
    		System.out.println("a:" + a);//a的地址
    		ClassA b = new ClassA();
    		b.seta(2);
    		b.show();
    		System.out.println("b:" + b);//b的地址
    		System.out.println("======================");
    		b = a;
    		a.show();
    		b.show();
    		System.out.println("a:" + a + ", b:" + b);
    		b.seta(3);
    		a.show();
    		b.show();
    		System.out.println("a:" + a + ", b:" + b);
    	}
    }
    

    运行结果:

    the value:1
    a:MyText.ClassA@14a55f2
    the value:2
    b:MyText.ClassA@15093f1
    ======================
    the value:1
    the value:1
    a:MyText.ClassA@14a55f2, b:MyText.ClassA@14a55f2
    the value:3
    the value:3
    a:MyText.ClassA@14a55f2, b:MyText.ClassA@14a55f2

    在java中向函数传入类对象参数实际上操作的也是地址

    eg:

    package MyText;
    
    class ClassA
    {
    	int value;
    	public void seta(int value)
    	{
    		this.value = value;
    	}
    	public void show()
    	{
    		System.out.println("the value:" + value);
    	}
    }
    public class MyText {
    	public static void showClassA(ClassA a)
    	{
    		a.show();
    		System.out.println("a:" + a);
    		a.seta(5);
    		a.show();
    		System.out.println("a:" + a);
    	}
    	public static void main (String []args)
    	{
    		ClassA a = new ClassA();
    		a.seta(1);
    		a.show();
    		System.out.println("a:" + a);
    		showClassA(a);
    		a.show();
    		System.out.println("a:" + a);
    	}
    }
    

    运行结果:

    the value:1
    a:MyText.ClassA@5e55ab
    the value:1
    a:MyText.ClassA@5e55ab
    the value:5
    a:MyText.ClassA@5e55ab
    the value:5
    a:MyText.ClassA@5e55ab
    而在C++中向函数传递类对象参数时,是按值传递的,即实参与形参间进行成员变量赋值操作,而不是地址

    eg:

    # include <iostream>
    using namespace std;
    
    class ClassA
    {
    private:
        int value;
    public:
        void seta(int value)
        {
            this->value = value;
        }
        void show()
        {
            cout<<"the value : "<<value<<endl;
        }
    };
    void show(ClassA a)
    {
        a.show();
        cout<<"a:"<<&a<<endl;
        a.seta(5);
        a.show();
    }
    int main ()
    {
        ClassA a;
        a.seta(3);
        a.show();
        cout<<"a:"<<&a<<endl;
        show(a);
        a.show();
    }
    

    运行结果:

    the value : 3
    a:0x22fefc
    the value : 3
    a:0x22fea0
    the value : 5
    the value : 3


    Process returned 0 (0x0)   execution time : 0.130 s
    Press any key to continue.
    对于利用等号对对象进行赋值,实际上也是对对象成员的值按值传递,而不是传递地址

    eg:

    # include <iostream>
    using namespace std;
    
    class ClassA
    {
    private:
        int value;
    public:
        void seta(int value)
        {
            this->value = value;
        }
        void show()
        {
            cout<<"the value : "<<value<<endl;
        }
    };
    int main ()
    {
        ClassA a;
        a.seta(3);
        a.show();
        cout<<"a:"<<&a<<endl;
        ClassA b;
        b.seta(4);
        b.show();
        cout<<"b:"<<&b<<endl;
        b = a;
        a.show();
        b.show();
        cout<<"a:"<<&a<<", b"<<&b<<endl;
        b.seta(6);
        a.show();
        b.show();
        cout<<"a:"<<&a<<", b"<<&b<<endl;
    }
    


    运行结果:

    the value : 3
    a:0x22fefc
    the value : 4
    b:0x22fef8
    the value : 3
    the value : 3
    a:0x22fefc, b0x22fef8
    the value : 3
    the value : 6
    a:0x22fefc, b0x22fef8


    Process returned 0 (0x0)   execution time : 0.132 s
    Press any key to continue.

  • 相关阅读:
    solution for "cannot find vcvarsall.bat" in Python
    fast debug a program with GDB
    pLSA 浅析
    argsort in Python
    how to create html from tex source file
    google云输入法以及在chrome中的插件
    贝叶斯估计浅析
    JavaScript学习篇(5)
    JavaScript学习篇(6)
    JavaScript学习篇(2)
  • 原文地址:https://www.cnblogs.com/keanuyaoo/p/3299312.html
Copyright © 2011-2022 走看看