Академический Документы
Профессиональный Документы
Культура Документы
AVA
Class
Class
Class
Class Class
Class
Class Class
protected
package sportinggoods;
class DownhillSki extends Ski {
void tuneup() {
applywax();
// other tuneup functionality here
}
}
package adifferentpackage; // class Ski now in a
// different package
Class Ski {
protected void applymax() { ... }
}
package sportinggoods;
Class DownhillSki extends Ski {
void tuneup() {
applywax();
// other tuneup functionality here
}
}
Subclasses and Method privacy
1. class Mammal {
2. final void getAround() { }
3. }
Compiler error:
“Final methods can’t be overriden”
abstract
class Ecstatic {
static int x=0;
Ecstatic() { x++; }
}
Two ways to reference a static variable
Bad form
Ecstatic e1 = new Ecstatic();
Ecstatic e2 = new Ecstatic();
e1.x = 100;
e2.x = 200;
reallyImportantVariable=e1.x;
2. Via the class name:
class SomeClass {
static int i=48;
int j=1;
Class Xyz {
int w;
void bumpW() {
w++;
}
}
Abbreviation for
this.w++;
If a static method needs to access a non-static variable or call a
non-static method, it must specify which instance of its
class owns teh variable or executes the method have an
implicit variable named this, which is a reference to the
object executing the method
1. import java.awt.*;
Class Cattle {
static void foo() { }
}
Error:
“Static methods can’t be overrriden”
Summary (static)
static {
System.out.println(“Static code: i= “+ i++ );
}
public static void main( String [] args ) {
System.out.println(“main: i= “+ i++ );
}
}
Static initializer code
: the code inside the curlies is executed exactly
once at the time the class is loaded
: At class-load time, all static initialization and all
free-floating static code are executed in order of
appearance within the class definition
static {
System.out.println(“Static code: i= “+ i++ );
}
public static void main( String[] args ) {
System.out.println(“main: i= “+ i++ );
}
}
native
1. nativeExample natex;
2. natex=new NativeExample();
3. natex.doSomethingLocal (5);
: Callers of native methods do not have to
know that the method is native
: The call is made the same way as if it
were non-native
1. nativeExample natex;
2. natex=new NativeExample();
3. natex.doSomethingLocal (5);
transient