public class StaticTest {
private static String name = "javaJohn";
private String id = "X001";
static class Person{
private String address = "swjtu,chenDu,China";
public String mail = "[email protected]";//内部类公有成员
public void display(){
//System.out.println(id);//不能直接访问外部类的非静态成员
System.out.println(name);//只能直接访问外部类的静态成员
System.out.println("Inner "+address);//访问本内部类成员。
}
}
public void printInfo(){
Person person = new Person();
person.display();
//System.out.println(mail);//不可访问
//System.out.println(address);//不可访问
System.out.println(person.address);//可以访问内部类的私有成员
System.out.println(person.mail);//可以访问内部类的公有成员
}
public static void main(String[] args) {
StaticTest staticTest = new StaticTest();
staticTest.printInfo();
}
}
public class Outer{
int outer_x = 100;
class Inner{
public int y = 10;
private int z = 9;
int m = 5;
public void display(){
System.out.println("display outer_x:"+ outer_x);
}
private void display2(){
System.out.println("display outer_x:"+ outer_x);
}
}
void test(){
Inner inner = new Inner();
inner.display();
inner.display2();
//System.out.println("Inner y:" + y);//不能访问内部内变量
System.out.println("Inner y:" + inner.y);//可以访问
System.out.println("Inner z:" + inner.z);//可以访问
System.out.println("Inner m:" + inner.m);//可以访问
InnerTwo innerTwo = new InnerTwo();
innerTwo.show();
}
class InnerTwo{
Inner innerx = new Inner();
public void show(){
//System.out.println(y);//不可访问Innter的y成员
//System.out.println(Inner.y);//不可直接访问Inner的任何成员和方法
innerx.display();//可以访问
innerx.display2();//可以访问
System.out.println(innerx.y);//可以访问
System.out.println(innerx.z);//可以访问
System.out.println(innerx.m);//可以访问
}
}
public static void main(String args[]){
Outer outer = new Outer();
outer.test();
}
}
public class FunOuter {
int out_x = 100;
public void test(){
class Inner{
String x = "x";
void display(){
System.out.println(out_x);
}
}
Inner inner = new Inner();
inner.display();
}
public void showStr(String str){
//public String str1 = "test Inner";//不可定义,只允许final修饰
//static String str4 = "static Str";//不可定义,只允许final修饰
String str2 = "test Inner";
final String str3 = "final Str";
class InnerTwo{
public void testPrint(){
System.out.println(out_x);//可直接访问外部类的变量
//System.out.println(str);//不可访问本方法内部的非final变量
//System.out.println(str2);//不可访问本方法内部的非final变量
System.out.println(str3);//只可访问本方法的final型变量成员
}
}
InnerTwo innerTwo = new InnerTwo();
innerTwo.testPrint();
}
public void use(){
//Inner innerObj = new Inner();//此时Inner己不可见了。
//System.out.println(Inner.x);//此时Inner己不可见了。
}
public static void main(String[] args) {
FunOuter outer = new FunOuter();
outer.test();
}
}
import java.applet.*;
import java.awt.event.*;
public class AnonymousInnerClassDemo extends Applet{
public void init(){
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent me){
showStatus("Mouse Pressed!");
}
})
}
public void showStatus(String str){
System.out.println(str);
}
}
public class Layer {
//Layer类的成员变量
private String testStr = "testStr";
//Person类,基类
class Person{
String name;
Email email;
public void setName(String nameStr){
this.name = nameStr;
}
public String getName(){
return this.name;
}
public void setEmail(Email emailObj){
this.email = emailObj;
}
public String getEmail(){
return this.email.getMailStr();
}
//内部类的内部类,多层内部类
class Email{
String mailID;
String mailNetAddress;
Email(String mailId,String mailNetAddress){
this.mailID = mailId;
this.mailNetAddress = mailNetAddress;
}
String getMailStr(){
return this.mailID +"@"+this.mailNetAddress;
}
}
}
//另一个内部类继承外部类本身
class ChildLayer extends Layer{
void print(){
System.out.println(super.testStr);//访问父类的成员变量
}
}
//另个内部类继承内部类Person
class OfficePerson extends Person{
void show(){
System.out.println(name);
System.out.println(getEmail());
}
}
//外部类的测试方法
public void testFunction(){
//测试第一个内部类
ChildLayer childLayer = new ChildLayer();
childLayer.print();
//测试第二个内部类
OfficePerson officePerson = new OfficePerson();
officePerson.setName("abner chai");
//注意此处,必须用 对象.new 出来对象的子类对象
//而不是Person.new Email(...)
//也不是new Person.Email(...)
officePerson.setEmail(officePerson.new Email("josserchai","yahoo.com"));
officePerson.show();
}
public static void main(String[] args) {
Layer layer = new Layer();
layer.testFunction();
}
}
本文地址:http://com.8s8s.com/it/it15946.htm