java编程思想 第二章 (一切都是对象)练习 2.11 练习11

练习11:将AllTheColorsOfTheRainbow这个示例改写成一个程序,然后编译、运行。

该示例在本书第36页

示例为:

class AllTheColorOfTheRainbow
{
    int anIntegerRerRepresentingColors;
    
    void changeTheHueOfTheColor(int newHue)
    {
           //...
    }
}

改写为:

class AllTheColorsOfTheRainbow
{
    int anIntegerRerRepresentingColors=9;
    int Hue=6;

    void changeTheHueOfTheColor(int newHue)
    {
        this.Hue=anIntegerRerRepresentingColors;

    }

}

public class Main
{
    public static void main(String[] args) {

        AllTheColorsOfTheRainbow atcotr = new AllTheColorsOfTheRainbow();
        System.out.println(atcotr.Hue);
        atcotr.changeTheHueOfTheColor(atcotr.Hue);
        System.out.println(atcotr.Hue);
    }
}

猜你喜欢

转载自blog.csdn.net/QiuBika_061/article/details/84205139