简体   繁体   English

具有 getter 和 setter 方法的 Java 对象

[英]Java objects with getter and setter methods

Hi can someone please help me,I am a novice programmer and I don't understand the following code.嗨,有人可以帮助我,我是一个新手程序员,我不明白下面的代码。

How does one.bark() automatically returns the statement under the first if condition. one.bark()如何自动返回第一个if条件下的语句。 How does the compiler know which if statement to display (because we are not passing the size while calling bark() )?编译器如何知道要显示哪个 if 语句(因为我们在调用bark()时没有传递大小)? I know the object calls the function setSize and passes the argument 70 to it.我知道该对象调用函数setSize并将参数70传递给它。 Does that mean that the value 70 becomes an attribute of the object one ?这是否意味着值70成为对象one一个属性?

Code:代码:

class GoodDog {
    private int size;

    public void setSize(int s) {
        size = s;
    }

    public int getSize() 
    {
        return size;
    }

    void bark()
    {
        if (size > 60)
        {
            System.out.println("Wooof! Wooof!");
        } 
        else if (size > 14) 
        {
            System.out.println("Ruff! Ruff!");
        }
        else
        {
            System.out.println("Yip! Yip!");
        }
    }

}

class GoodDogTestDrive
{
    public static void main (String[] args) 
    {
        GoodDog one = new GoodDog();
        one.setSize(70);
        GoodDog two = new GoodDog();
        two.setSize(8);

        System.out.println("Dog one: " + one.getSize());
        System.out.println("Dog two: " + two.getSize());
        one.bark();
        two.bark();
    }

}

Yes.是的。 When you call one.setSize(70) , the size variable is saved as 70 in the one object.当您调用one.setSize(70)size变量在one对象中保存为70 When you call one.bark() , size is still 70当你调用one.bark()size仍然是70

When creating an object of a class , the object receives a copy of all non-static members of the class.创建类的对象时,该对象会收到该类的所有非静态成员的副本。

GoodDog one = new GoodDog(); GoodDog 一 = new GoodDog();

object one contains a special copy of size.对象包含一个特殊的大小副本。 Like wise object two will have its own copy of member variable size.像明智的对象将拥有自己的成员变量大小副本。

When you set the size using setSize() function , the one object's size variable is modified and will contain the same value until another value is assigned.当您使用 setSize() 函数设置大小时,一个对象的大小变量将被修改,并将包含相同的值,直到分配另一个值。

When you call the bark function using one function , it's own copy of size variable is taken and used inside the bark function.当您使用一个函数调用 bark 函数时,它自己的 size 变量副本被获取并在 bark 函数中使用。

Actually, 70 is not a new attribute, but the size attribute takes the value 70 .实际上, 70不是一个新属性,但size属性采用值70

And since you have two distinct instances ( Dog one and Dog two ) of GoodDog, each one has it's own attribute size , which has it's own value (in this case 70 and 8 ).由于您有两个不同的 GoodDog 实例( Dog oneDog two ),每个实例都有自己的属性size ,它有自己的值(在本例中为708 )。

From there, when you call the bark method, each instance ( one / two ) will check the if/else statement according to it's own attribute's value (respectively 70 / 8 ).从那里,当您调用bark方法时,每个实例( one / two )将根据其自己的属性值(分别为70 / 8 )检查if/else语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM