《JAVA编程思想》学习备忘(p61:Everything Is an Object--3)

来源:互联网 发布:女性受教育程度数据 编辑:程序博客网 时间:2024/05/22 15:07
(接Everything Is an Object--2)
Methods,arguments,and return values
Methods in Java determine the message an object can receive.The fundamental parts of a method are the name,the arguments,the return type and the body.
   方法的构成:
| ReturnType(返回类型) methodName(方法名)(Argument list(形参列表)){
     Method body(方法主体)
  }
The method name and argument list(which is called the signature of the method)uniquely identify that method
    Java中方法仅做为类的部分而创建,一个方法仅以对象来调用(静态的例外),而此对象必须能够实现调用方法。
You call a method for an object by naming the object followed by a period(dot),followed by the name of the method and its argument list,like this:
| objectName.methodName(arg1,arg2,arg3);
 
The argument list
    方法的形参列表指定了你要给方法传递的信息,且形参名称要与类型匹配
例子:
| int storage(String s){
     return s.length()*2;
  }
    int 指定此例返回一个整形值:字符串s的字符数。
    你可以指定使其返回你要的任何类型,无论数据类型或引用类型。如果你不想其返回任何类型,只需用一个关键字来批定:void(空) 。
看几个例子:
| boolean flag() {return true;}
| double naturalLogBase(){returen 2.718;}
| void nothing(){return;}
| void nothing2(){};
    当返回类型为空时,关键字return仅用来退出方法,且可用其在任意指定点退出方法。但如果是非空类型,编译器将用错误提示你不管从何处退出一定要返回一个合适的类型。
 
Building a Java program
    构建程序前要了解的几个问题。
Name visibility
    如果你在一个程序模块中使用一个名称,而别的程序员在同一程序的另一个模块中使用了同样的名称,如何区别并防止冲突呢?
Java是这样解决的:使用倒序的互联网域名。这会确保其是独一无二的。
Using other components
    无论何时当你在程序中需要使用一个预定义类时,编译器必须知晓如何找到它。当然,如果此类在同样的源代码文件中,你只需调用即可。但如果不在同一文件中怎么办?
    解决此问题,使用关键字:import 。它告诉编译器导入了一个类库中的包(在别的语言中,一个库可以包含函数和数据,但要记住在Java中,所有代码必须写入一个类中)。
The static keyword
    通常情况下,当你创建一个类时,你描述那个类的对象具有的外观和行为。实际上,“new”之前你不会得到一个对象,且基于此点(也不会)开辟存储空间和使方法可用。
    两种情况下以上考虑不够充分。其一是不管创建了多少那个类的对象,或没有创建对象,你仅想要一个特有属性(类型变量)有单一的存储空间;另一种是,如果你需要一个不与任何特定的类的对象有联系的方法,就是说,你需要一个即使类的对象没被创建,你也可以调用的方法。
    你可以用关键字static(静态的)来达到目的。当你说某些是静态的,意味着特有的属性和方法与任何实例化的类对象没有联系。这样即使你没有创建类的对象也可调用一个静态的方法或对静态的属性进行存取。
    当定义属性或方法时,把关键字static置前即可使其静态化:
| class StaticTest{
      static int i = 47;
  }
Now even if you make two StaticTest objects,there will still be only one piece of storage for StaticTest.i.Both objects will share the same i.
共享单一的静态存储空间。举例如下:
|StaticTest st1 = new StaticTest();
|StaticTest st2 = new StaticTest();
    st1.i和st2.i因选择了同一内存空间而具有同样的值:47。
    有两种方法选择一个静态的变量,如前例所指,你可通过对象例如:st2.i ;你也可以通过它的类名直接选择(非静态做不到):
| StaticTest.i++;
    后者可给编译器更多的优化机率。
    静态的方法同此。你可通过对象来选择一个静态的方法,或者用特殊的条件语法:ClassName.method().你用同样的方法定义一个静态的方法:
| class Incrementable {
      static void increment(){StaticTest.i++;}
  }
    你可通过对象用典型的方式调用increment():
| Incrementable sf = new Incrementable();
| sf.increment();
    因为它是静态的方法,你可直接通过类来调用:
| Incrementable.increment();
    方法的静态化应用一个重要之处是允许你不用创建对象就直接调用。这是一个要点。
 
Your first Java program
类名要与文件名相同。
类必须包含主方法main()。
(其余略)
 
Compiling and running
    关于下载JDK与环境变量配置。(略)
Comments and embedded documentation
    关于两种注释方式(略)
Comment documentation
    注释文档及其提取。(略)
Syntax
    关于注释文档的语法。
There are three "types" of comment documentation,which correspond to the element the comment precedes:class,field,or method.That is,a class comment appears right before the definition of a class,a field comment appears right in front of the definition of a field,and a method comment appears right in front of the definition of a mentod.
例子:(略)
Note that Javadoc will process comment documentation for only public and protected members.
 
Embedded HTML
(以后补充)
Some example tags
(以后补充) 
Documentation example
(略)
Coding style
    编码风格。
    类名起始字母要大写。
    如果类名包含几个单词,不要分开,每个单词的首字母都要大写:
| class AllTheColorsOfTheRainbow{//...
    这叫做“驼式编码”。
    其它几乎所有标识符首字母则需小写:
| class AllTheColorsOfTheRainbow{
|    int anIngegerRepresentingColors;
|    void changeTheHueOfTheColor(int newHue){
|        //...
|    }
|    //...
| }
   
 (本章结束)  
原创粉丝点击