Java: class , objects
1 Inher itance(继承)的关键字extends
class MountainBike extends Bicycle {
}
但是不能多重继承。不过可以通过implements多个interface来实现类似的东西
2 interface
interface Bicycle {
void changeCadence(int newValue); // wheel revolutions per minute
void changeGear(int newValue);
void speedUp(int increment);
void applyBrakes(int decrement);
}
class ACMEBicycle implements Bicycle {
// remainder of this class implemented as before
}
可实现(implements)多个interface
class MyClass extends MySuperClass implements YourInterface {
//field, constructor, and method declarations
}
means that MyClass
is a subclass of MySuperClass
and that it implements the YourInterface
interface
3 package
A package is a namespace that organizes a set of related classes and interfaces.
4 Access Modifier
public, private, protected
public class Bicycle {
private int cadence;
public int aag;
public void test();
}
每个field或method都必须单独修饰
Modifier | Class | Package | Subclass | World |
---|---|---|---|---|
public | Y | Y | Y | Y |
protected | Y | Y | Y | N |
no modifier | Y | Y | N | N |
private | Y | N | N | N |
约定:
class 名字的第一个字母大写; method第一个word是动词
5 Constructors
The compiler automatically provides a no-argument, default constructor for any class without constructors.
This default constructor will call the no-argument constructor of the superclass.
6 method & parameter
The Java programming language doesn't let you pass methods into methods. But you can pass an object into a method and then invoke the object's methods.
can Returning a Class or Interface??
7 this : current object
public class Point { public int x = 0; //field可以这样初始化 public int y = 0; //constructor public Point(int x, int y) { this.x = x; this.y = y; }
public Point(int a, int b) { this(a,b); }
}
this通常指当前对象,super则指父类的
8 const:
Thestatic
modifier, in combination with thefinal
modifier, is also used to define constants
static final double PI = 3.141592653589793;
9 member初始化
static initialization block :
static { // whatever code is needed for initialization goes here }A class can have any number of static initialization blocks, and they can appear anywhere in the class body.The runtime system guarantees that static initialization blocks are called in the order that they appear in the source code.Alternative:class Whatever { public static varType myVar = initializeClassVariable(); private static varType initializeClassVariable() { //initialization code goes here } }10 GCThe garbage collector automatically cleans up unused objects. An object is unused if the program holds no more references to it.You can explicitly drop a reference by setting the variable holding the reference tonull
11 nested classes
Static Nested Classes: a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience.
a static nested class cannot refer directly to instance variables or methods defined in its enclosing class — it can use them only through an object reference
Inner Classes
class OuterClass {
...
class InnerClass {
...
}
}
inner classes have access to other members of the enclosing class, even if they are declared private.
You can declare an inner class within the body of a method. Such a class is known as a local inner class.
You can also declare an inner class within the body of a method without naming it. These classes are known as anonymous inner classes.
Type | Scope | Inner |
---|---|---|
static nested class | member | no |
inner [non-static] class | member | yes |
local class | local | yes |
anonymous class | only the point where it is defined | yes |
12 final
当你在类中定义变量时,在其前面加上final关键字,那便是说,这个变量一旦被初始化便不可改变,这里不可改变的意思对基本类型来说是其值不可变,而对于对象变量来说其引用不可再变。其初始化可以在两个地方,一是其定义处,也就是说在final变量定义时直接给其赋值,二是在构造函数中。
将方法声明为final,那就说明你已经知道这个方法提供的功能已经满足你要求,不需要进行扩展,并且也不允许任何从此类继承的类来覆写这个方法,但是继承仍然可以继承这个方法,也就是说可以直接使用。
final类是无法被任何人继承的,那也就意味着此类在一个继承树中是一个叶子类,并且此类的设计已被认为很完美而不需要进行修改或扩展。
1, Interface可以多重extend
2,
subclass inherits all of the public and protected members of its parent, no matter what package the subclass is in.
If the subclass is in the same package as its parent, it also inherits the package-private members of the parent.
3.
use the super
keyword to invoke a superclass's constructor
super(); --or-- super(parameter list);
public MountainBike(int startHeight, int startCadence, int startSpeed, int startGear) { super(startCadence, startSpeed, startGear); seatHeight = startHeight; }
4 An abstract class is a class that is declared abstract
—it may or may not include abstract methods.
Abstract classes cannot be instantiated, but they can be subclassed.
Abstract Classes versus Interfaces
Unlike interfaces, abstract classes can contain fields that are not static
and final
,
and they can contain implemented methods.
abstract class X implements Y { // implements all but one method of Y } class XX extends X { // implements the remaining method in Y }