别名或参考 [英] Aliasing or reference

查看:98
本文介绍了别名或参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class Toyota
{
    int modelelNumber;
    
    public Car()
    {
        this.modelNumber = 2011;
    }
    
    public void setModelNumber(int modelNumber)
    {
        
        this.modelNumber = modelNumber;
    }
}

class Car
{
    public static void main(String []main)
    {
        Toyota newCar = new Toyota();
        
        newCar.setModelNumber(2012); <--------------- 1
        newCar.modelNumber = 2013;   <-----------------2
        
    }


}





你能解释其中两个之间的区别。我指向的第1行和第2行。他们之间的区别是什么。我知道第一个只是设置型号。第二个怎么样?然后是他们之间的区别。我们在这里假设丰田级的成员可以在Car类中访问。

谢谢



Can you explain the difference between two of them. The line 1 and 2 that I am pointing to. Whats the difference between them. I know the first one is simply setting the model number. What about the second one. And whats the difference between them. We assume here that the member of the Toyota class is accessible in the Car class.
Thanks

推荐答案

您的代码仍然是错误的。 Car 类应该是所有其他汽车类型的基础,因此 Toyota 将继承汽车。请参阅 http://docs.oracle.com/javase/tutorial/java/index.html [ ^ ]详细了解课程和继承。
Your code is still wrong. The Car class should be the base of all the other car types, so Toyota would inherit Car. See http://docs.oracle.com/javase/tutorial/java/index.html[^] for full details abot classes and inheritance.


一个是字段访问,另一个是方法调用(属性也可以有方法调用set和get)。



不同之处在于,如果您需要做其他事情而不是设置值,例如验证或计算其他值,同时更改相关值。
One is field access, the other is a method call (properties also can have method calls for set and get).

The difference is if you need to do something else other than setting values like validation or computing other values while changing the one in question.


这篇关于别名或参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆