[Core Java® for the Impatient]重载Java2

Chapter 2. Object-Oriented Programming

  1.     Set(Mutator Methods)方法改变对象的状态,Get(accessor methods)方法则不;

  2. Java中变量不持有对象,他们引用对象;

  3. 变量的实例和方法的实现在类的声明部分声明;

  4. 对象调用自身实例化的方法,靠this引用

    5.    构造方法与类同名,一个类可以拥有多个构造方法;

    6.    静态变量不属于任何对象,静态方法不通过对象调用

    7.    类之间通过包来组织,使用import声明使得你不需要在程序里写完整的包名;

    8.    类可以嵌套;

    9.    内部类是非静态的嵌套类,其实例拥有构造过的嵌入类的引用;

    10.    Java doc工具处理源文件产生HTML格式的代码注释;


基本概念:

encapsulation 封装

inheritance 继承

polymorphism 多态


mutator => setter

accessor => getter

Java8 多了一种参数方式,receiver parameter

我们可以使用this作为参数,但是要保证JDK版本,否则

095706_NScV_921876.png

jls有详细说明:

http://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.4.1

Here are some examples of receiver parameters in instance methods and inner classes' constructors:
class Test {
    Test(/* ?? ?? */) {}
      // No receiver parameter is permitted in the constructor of
      // a top level class, as there is no conceivable type or name.

    void m(Test this) {}
      // OK: receiver parameter in an instance method

    static void n(Test this) {}
      // Illegal: receiver parameter in a static method                         

    class A {
        A(Test Test.this) {}
          // OK: the receiver parameter represents the instance
          // of Test which immediately encloses the instance
          // of A being constructed.

        void m(A this) {}
          // OK: the receiver parameter represents the instance 
          // of A for which A.m() is invoked.

        class B {
            B(Test.A A.this) {}
              // OK: the receiver parameter represents the instance 
              // of A which immediately encloses the instance of B 
              // being constructed.

            void m(Test.A.B this) {}
              // OK: the receiver parameter represents the instance 
              // of B for which B.m() is invoked.
        }
    }
}
B's constructor and instance method show that the type of the receiver parameter may be denoted with a qualified TypeName like any other type; but that the name of the receiver parameter in an inner class's constructor must use the simple name of the enclosing class.


内部类不可以拥有静态变量和静态方法,但是可以拥有常量。

public class Test {
    class TestClass {
        private final static int num = 0;// right
        private static int id = 0;//Error:(17, 28) java: Illegal static declaration in inner class com.scb.smarttrans.utils.generator.Test.TestClass
  modifier 'static' is only allowed in constant variable declarations

        public static int addOne() {
            return ++id;
        } //Error:(19, 27) java: Illegal static declaration in inner class com.scb.smarttrans.utils.generator.Test.TestClass
  modifier 'static' is only allowed in constant variable declarations
    }
}

作者趁机小黑了一把Java Designer

Inner classes cannot declare static members other than compile-time constants. There would be an ambiguity about the meaning of “static.” Does it mean there is only one instance in the virtual machine? Or only one instance per outer object? The language designers decided not to tackle this issue.

文档注释有些不常见的用法:

/**
* An <code>Invoice</code> object represents an invoice with
* line items for each part of the order.
*
* @param byPercent the percentage by which to raise the salary (e.g., 10 means 10%)
* @author Fred Flintstone
* @author Barney Rubble
* @version 1.1
*          <p>
*          Raises the salary of an employee.
* @return the amount of the raise
* @see com.horstmann.corejava.Employee#raiseSalary(double)
* @see <a href="http://en.wikipedia.org/wiki/Leap_year">Leap years</a>.
* @see "Core Java for the Impatient"
* {@link package.class#feature label}
* <img src="doc-files/uml.png" alt="UML diagram"/>.
* @since version 1.7.1
* @deprecated Use <code>setVisible(true)</code> instead
*/



转载于:https://my.oschina.net/u/921876/blog/663227

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值