属性与方法

方法只有调用的时候才占用内存;

state affects behavior,behavior affects state;

字段与method

类字段:也称为静态字段,被static修饰,此字段与该类本身相关,而不是与该类的实例相关,使用格式:类名.类字段;

实例字段:无static修饰,使用格式:对象名.实例字段名,该字段的值使得不同于其它对象。

Method operate on an object's internal state and serve as the primary mechanism for object-to-object communication.Hiding internal state and requiring all interaction to be performed through an object's methods is known as data encapsulation.

The fields represent the object's state,and the methods define its interation with the outside world.

Similar to how an object stores its state in field,a method will often store its temporary state in local variables,but there is no special keyword desinating a variable as local,that determination comes entirely from the location in which the variable is declared.Which is  between the opening and closing braces of a method.As such,local variables are only visible to the methods in which they are declared;they are not accessible from the rest of the class.

返回值

You declare a method's return type in its method declaration.Within the body of the method,you use the return statement to return the value.

And method declared void doesn't return a value.It does not need to contain a return statement,but it may do so.In such a case,a return statement can be used to branch out of a control flow block and exit the method and is simply used like this:return;

Any method that is not declared void must contain a return statement with a corresponding return value.

The data type of the return value must match the method's declared return type;you can't return an integer value from a method declared to return a boolean.

实例方法和类方法

实例方法既能对类变量操作也能对实例变量操作,而类方法只能对类变量进行操作;

一个类中的方法可以相互调用,实例方法可以调用该类中的其他方法,类中的类方法只能调用该类的类方法,不能调用实例方法;

Java的方法可以分为实例方法和类方法,实例方法专门用于处理实例变量的计算,所以通过对象来调用。类方法则是专门用来处理类变量的的计算,所以通过类名称来调用。

实例方法专门处理实例变量

类方法专门处理类变量;

类方法不能放入this,super或实例变量,因为类方法早在对象还没有产生就已存在,而this,super所代表的对象根本还没有产生。

类方法:也用static修饰,也是与类相关,也不是与实例相关;

类方法不能放入this,super或实例变量,因为类方法早在对象还没产生前就已存在,而this,super或实例所代表的对象还没有产生,既然没有对象,就不会有实例变量;