美文网首页
Head First Java 笔记3

Head First Java 笔记3

作者: Wilbur_ | 来源:发表于2020-02-06 22:14 被阅读0次

Instance Variables

Instance variables are declared inside a class but not inside a method. They represent the "fields" that each individual object has (which can be filled with different values for each instance of the class). Instance variables live inside the object they belong to.

Local variables local variables are devlared inside a method, including method parameters. They're temporary, and live only as long as the method is on the stack (in other words, as long as the method has not reached the closing curly brace).

Head first java p236

Constructor

A constructor does look and feel a lot like a method, but it's not a method. It's got the code that runs when you say new. In other words. the code that runs when you instantiate an object.

The only way to invoke a constructor is with the keyword new followed by the class name. The JVM finds that class and invokes the constructor in that class. (OK, technically this isn;t the only way to invoke a constructor. But it's the only way to do it from outside a constructor. You can call a constructor from within another constructor, with restrictions, but we'll get into all that later in the chapter.)

A constructor has the code that runs when you instantiate an object. in other words, the code that runs when you say new on a class type.
Every class you create has a constructor, even if you don't write it yourself.

The key feature fo a constructor is that it runs before the object can be assigned to a reference. That means you get a chance to step in and do things to get the object ready for use. In other words, before anyone can use the remote control for an object, the object has a chance to help contruct itself. In our Duck constructor, were not doing anything useful, but it still demonstrates the sequence of events.
说白了,constructor就像一个method,只不过跟这个class的名字一样,而且有()在后面,比如public class Duck {}, 它的constructor就是 public Duck() {}。 但是,constructor没有return type,method有,这也是你如何分辨constructor和method。

Overloaded constructor

Overloaded constructors means you have more than one constructor in your class.
To compile, each constructor must have a different argument list!

Bullet points

  • A constructor must have the same name as the class, and must not have a return type.
  • If you don't put a constructor in your class, the compiler will put in a default constructor.
  • If you put a constructor --any constructor-- in your class, the compiler will not build the default constructor.
  • If you want a no-arg constructor, and you've already put in a constructor with arguments, you'll have to build the no-arg constructor yourself.
  • Always provide a no-arg constructor if you can, to make it easy for programmers to make a working object. Supply default values.
  • Overloaded constructors means you have more than one constructor in your class.
  • Overloaded constructors must have different argument lists.
  • You cannot have two constructors with the same arguments lists. An argument list includes the order and/or type of arguments.
  • Instance variables are assigned a default value, even when you don't explicitly assign one. The default values are 0/0.0/false for primitives, and null for references.

相关文章

  • readme

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

  • Head First Java 笔记3

    Instance Variables Instance variables are declared inside...

  • 《Head First Java》笔记

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

  • Head First Java 笔记

    Encapsulation Exposed means reachable with the dot operat...

  • 3/22/16 读书清单

    2016年3月读书清单 Clean Code - LeetCode (复习) Head First Java (...

  • Head First Java(一)基本概念

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

  • 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 中文版(...

网友评论

      本文标题:Head First Java 笔记3

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