- 讲师:刘萍萍 / 谢楠
- 课时:160h
- 价格 4580 元
特色双名师解密新课程高频考点,送国家电网教材讲义,助力一次通关
配套通关班送国网在线题库一套
百度广告
//access privilege level: default
}
//access privilege level: private
//define a nested inner class in another inner class
}
private static void staticMethod() {
/*public static class Inner4() {
}
private void instanceMethod() {
Inner3 inner3 = new Inner3();
Inner3.Inner4 inner4 = new Inner3.Inner4();
}
class Test {
//Test and Outer are in the same package, so Inner2 can be accessed here
//Compile error: Inner3 cannot be accessed here
}
1.1 静态成员类特性
Entry(E element, Entry next, Entry previous) {
this.next = next;
}
…;
2 Member class(成员类)一个静态成员类,若去掉"static"关键字,就成为成员类。如下示例代码,Inner1/Inner2/Inner3/Inner4就是Outer的四个成员类
//just like instance method, member class has public/private/default access privilege levels
//access privilege level: public
private int data;
public Inner1() {
data1 = 1;
data = 1;
Outer.this.data = 1;
}
//access privilege level: default
//can not define static filed, method, class in member class
//but, "static final" compound is allowed
}
//access privilege level: private
public class Inner4 {
}
//in fact, Inner5 is not a member class but a static member class
}
private static void staticMethod() {
//Inner1 inner1 = new Inner1();
private void instanceMethod() {
Inner1 inner1 = new Inner1();
}
class Test {
//cannot create member class instance directly in class other than outer class
//create a member class instance outside it's outer class
Outer.Inner1 inner1 = outer.new Inner1();
}2.1 成员类特性· 类似于外部类的实例函数,成员类有public/private/default权限修饰符· 一个成员类实例必然所属一个外部类实例,成员类可访问外部类的任一个实例字段和实例函数。2.2 成员类约束
OuterClass.InnerClass innerClass = outerClass.new InnerClass(); |
public Iterator iterator() {
}
3 Local class(局部类)对一个静态成员类,去掉其声明中的"static"关键字,将其定义移入其外部类的静态方法或静态初始化代码段中就成为了局部静态成员类。对一个成员类,将其定义移入其外部类的实例方法或实例初始化代码中就成为了局部成员类。局部静态成员类与静态成员类的基本特性相同。例如,都只能访问外部类的静态字段或方法,但不能访问外部类的实例字段和实例方法等。局部成员类与成员类的基本特性相同。例如,局部成员类实例必属于其外部类的一个实例,可通过OuterClass.this引用其外部类实例等。另外,局部类也有其自己的特性,如以下代码所示:
public class Outer {
private static int staticField;
//define a local member class in instance code block
int localVirable1 = 0;
class Inner1 {
//can access its outer class' field and method directly
//use OuterClass.this to get its corresponding outer class instance
//can not access the not final local virable in its containing code block
//can access the final local virable in its containing code block
}
//local class can not have privilege modifier
}*/
// define a local static member class in static code block
class Inner2 {
staticField = 1;
//intanceField = 2;
}
public void intanceMethod() {
class Inner3 {
//local class is visible only in its containning code block
}
private static void staticMethod() {
class Inner4 {
staticField = 2;
}
//can not define a interface as a local class
}*/
}
3.1 局部类特性如示例代码所示,局部类能且只能访问其所属代码段中的声明为final的局部变量。为什么只能访问声明为final的局部变量呢?我们知道,局部变量在其所属的代码段(譬如某个函数)执行完毕后就会被回收,而一个局部类的实例却可以在其类定义所属代码段执行完毕后依然存在,如果它可操控非final的局部变量,用户就可以通过该实例修改已不存在的局部变量,无意义。3.2 局部类约束
//define a nonoymous class which extends BaseClass and create an instance of it
public void printData(){
}
}
interface Action {
}
class BaseClass {
public BaseClass (int data) {
}
public int getData() {
}
匿名类是一种特殊的局部类。局部类的特性与约束都适用与它。4.2 新增语法4.2.1 继承自某个基类的匿名类
new class-name ( [ argument-list ] ) { class-body } |
new interface-name () { class-body } |
// Now call the list() method with a single FilenameFilter argument
// as part of the method invocation expression.
public boolean accept(File f, String s) { return s.endsWith(".java"); }
责编:罗莉
课程专业名称 |
讲师 |
课时 |
查看课程 |
---|
课程专业名称 |
讲师 |
课时 |
查看课程 |
---|
点击加载更多评论>>