java for impatient_【读书笔记】Core Java for the Impatient 第二章

Object-Oriented Programming

Static variables don’t belong to any objects. Static methods are not

invoked on objects.

An inner class is a nonstatic nested class. Its instances have a reference to the object of the enclosing class that constructed it.

You can never write a method that updates primitive type parameters.

Default Initialization

Instance variable is automatically set to a default value: numbers to 0,boolean values to false,and object references to null.

In this regard,instance variables are very different from local variables. Recall that

you must always explicitly initialize local variable

Static Variables and Methods

Since static methods don’t operate on objects,you cannot access instance variables from a static method. However,static methods can access the static variables in their class

Packages

It is a good idea to run javac with the -d option. Then the class files are generated in a separate directory,without cluttering up the source tree,and they have the correct subdirectory structure.

A source file can contain multiple classes,but at most one of them can be declared public. If a source file has a public class,its name must match the class name.

Nested and Inner Classes

Nested Classes

Use a static nested class when the instances of the nested class don’t

need to know to which instance of the enclosing class they belong. Use an inner class only

if this information is important.

public class Invoice {

private static class Item { // Item is nested inside Invoice

String description;

int quantity;

double unitPrice;

double price() { return quantity * unitPrice; }

}

private ArrayListitems = new ArrayList<>();

}

There is nothing special about the Item class,except for access control.

If you change the keyword private into public,there is essentially no difference between this Invoice.Item class and a class InvoiceItem declared outside any other class.

Nesting the class just makes it obvIoUs that the Item class represents items in an invoice.

Inner Classes

public class Network {

public class Member {

public void leave() {

members.remove(this);

}

}

private ArrayListmembers;

}

A method of an inner class can access instance variables and methods of its outer class. In this case,they are the instance variables of the outer class object that created it

Comments

//TODO

相关文章

总结

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。

如您喜欢交流学习经验,点击链接加入交流1群:1065694478(已满)交流2群:163560250

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值