美文网首页
Head First Java 笔记

Head First Java 笔记

作者: Wilbur_ | 来源:发表于2020-01-27 20:10 被阅读0次

Encapsulation

Exposed means reachable with the dot operator, as in:
theCat.height = 27;
Think about this iead of using our remote control to make a direct change to the Cat object's size instance variable.
就是说如果不控制好猫的身高,随意让别人改动的话,猫的身高有可能变成0,这不是我们想要的,所以要保护好。

Hide the data

Yes it is that simple to go from an implementation that's just begging for bad data to one that protects your data and protects your right to modify your implementation later.
OK, so how exactly do you hide the data? with the public and private access modifiers. You are familiar with public- we use it with every main method.
Here's an encapsulation starter rule of thumb (all standarad disclaimers about rules of thumb are in effect): mark your instance variables private and provide public getters and setters for access control.

Instance variables are declared inside a class but not within a method. It is what the object knows. 说白了instance就是对象知道的数据,比如猫知道自己的身高,体重等等。
Local variables are declared within a method. 就是说local variables只是在执行任务时候用的,比如算两个数的总和,那用x和y就可以了,不用身高和体重。
Local variables MUST be initialized before use! Local variables do NOT get a default value! THe compiler complains if you try to use a local variable before the variable is initialized.

x++ 和++x不是一回事

THe placement of the operator(either before or after the variable) can affect the result. Putting the operator before the variable (for example, ++x), means, "first, increment x by 1, and then use this new value of x." This only matters when the ++x is part of some larger expression rather than just in a single statement.
int x = 0; int z = ++x;
produces: x is 1, z is 1
But putting the ++after the x give you a different result:
int x = 0; int z = x++;
produces: x is 1, but z is 0! z gets the value of x and then x is incremented.

相关文章

  • readme

    《Head First Java》私人阅读笔记 欢迎讨论、私信和补充

  • 《Head First Java》笔记

    对象与变量的创建与销毁 1、生存空间 2、构造函数 构造函数会在对象创建的时候执行程序代码,一般用来初始化被创建对...

  • Head First Java 笔记

    Encapsulation Exposed means reachable with the dot operat...

  • Head First Java(一)基本概念

    从今天开始读《Head First Java》一书,并开设了同名专题 Head First Java。计划在 1 ...

  • Head First Java 笔记3

    Instance Variables Instance variables are declared inside...

  • Head First Java 笔记2

    在写代码之前一般都要写一下自己的想法,书里就叫做prep code。 是为了把自己的框架列出来,然后能够清晰的把每...

  • Head First C 学习之K&R C 、ANSI

    @(C语言)[学习笔记, Head First C, C语言]起于Head First C 第2页 下, 书中简介...

  • Head First Java

    变量类型 变量类型有两种:一种是清凉的 primitive 主数据类型,一种是香辣的对象引用。变量必须拥有类型,另...

  • 学习java的书籍

    Java基础部分 [JAVA核心技术 Head First Java 重构 Effective java 中文版(...

  • java漫谈-Java只有值传递

    本文首发WindCoder:java漫谈-Java只有值传递 《Head First Java》中关于 Java ...

网友评论

      本文标题:Head First Java 笔记

      本文链接:https://www.haomeiwen.com/subject/sijozctx.html