And the combination of parameters by value object

1. The transmission parameter values, intended Guming Si, the value of a variable is passed to another variable.
such as:

int x = 10,y;
 y = x;

Wherein the value of x y passed, the value of y is 10, but the value is still 10 x.
Which in the end is how to achieve it?
In fact, here by value (i.e., assigned) to the present value of x is not used, but its copy.
It will not affect the value of x. Y parameter value obtained is equivalent to life in the "original" "and" copy. "
Operation on the x-y will not be affected, this is the principle parameters of traditional values. Similarly, the reference value of the parameter type of pass It is also a copy to the address number.

2. The combination of object
First, we know that a class can own it as an object a member variable, if you create an object with such a class, then this object will have other objects, which means that this object will other subject as part of their own, which is Has-a.
If the object is a combination of the object b, then the object can be a delegate object b calls their method, the object is a multiplexing method in combination with the object b.
Other class methods we can cite this way.

example:

package shaman4_2;

public class Family {
    TV homeTV;
    void buyTV(TV tv)
    {
        homeTV = tv;
    }
    void remoteControl(int m)
    {
        homeTV.setChannel(m);

    }
    void seeTV()
    {
        homeTV.showProgram();
    }
}
package shaman4_2;

public class TV {
    int channel;
    void setChannel(int m)
    {
        if(m>=1) {
            channel = m;
        }
    }
    int getChannel()
    {
        return channel;

    }
    void showProgram()
    {
        switch(channel)
        {
            case 1 : System.out.println("综合频道");
                break;
            case 2 : System.out.println("经济频道");
                break;
            case 3 : System.out.println("文艺频道");
                break;
            case 4 : System.out.println("国际频道");
                break;
            case 5 : System.out.println("体育频道");
                break;
            default : System.out.println("不能收看"+channel+"频道");
        }
    }
}
package shaman4_2;

public class MainClass {
    public static void main(String[] args) {
        TV haierTV = new TV();
        haierTV.setChannel(5);
        System.out.println("haierTV的频道是"+haierTV.getChannel());
        Family zhangSanFamily = new Family();
        zhangSanFamily.buyTV(haierTV);
        System.out.println("zhangSanFamily开始看电视节目");
        zhangSanFamily.seeTV();
        int m = 2;
        System.out.println("zhangSanFamily将电视更换到"+m+"频道");
        zhangSanFamily.remoteControl(m);
        System.out.println("haierTV的频道是"+haierTV.getChannel());
        System.out.println("zhangSanFamily在看电视节目");
        zhangSanFamily.seeTV();

    }
}

This example illustrates the combination of a good object reuse
study summarized above includes personal, such as unreasonable exist, welcome to point out, learn from each other.

Published 35 original articles · won praise 0 · Views 1318

Guess you like

Origin blog.csdn.net/c1776167012/article/details/102720546