Java-InnerClass-01

本文深入探讨Java内部类的特性与使用方法,通过实例讲解内部类如何访问外部类成员,以及如何从外部创建内部类对象。同时,展示了通过改进类的设计提高代码可维护性的技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 内部类可以直接访问外部类的成员,包括private成员,但内部类的成员却不能被外部类直接访问(很好理解,和类中方法体规则一样).
 在内部类对象保存了一个对外部类对象的引用.当内部类的成员方法中访问某一变量时,如果在该方法和内部类中都没有定义过这个变量,内部类中对this的引用会被传递给那个外部类对象的引用.
/************************简单举例*************************/
class Outer{
    
private int outer_i = 100;
    
public void test(){
        Inner inner 
= new Inner();
        inner.display();
    }

    
class Inner{
        
public void display(){
            System.out.println(
"outer_i = " + outer_i);
            
/*先在display()方法中找,然后在Inner类中找,再在Outer类中找,直至找到outer_i(很好理解,变量覆盖也是如此实现的).*/
        }

    }

    
public static void main(String[] args){
        Outer outer 
= new Outer();
        outer.test();
    }

}

/*外部类生成内部类对象的方法*/
class TestOuter{
    
public static void main(String[] args) {
        Outer outer 
= new Outer();
        Outer.Inner inner 
= outer.new Inner();
        inner.display();
    }

}

/************************class Outer的改进*************************/
class Outer_opt{
    
int outer_i = 100;
    
class Inner{
        
public void display(){
            System.out.println(
"outer_i = " + outer_i);
        }

    }

    
/*在外部类设置一个方法,该方法返回一个指向内部类的引用.这种模式很常见,有利于代码维护.*/
    
public Inner getInner(){
        
return new Inner();
    }

    
public void test(){
        Inner inner 
= getInner();
        inner.display();
    }

    
public static void main(String[] args) {
        Outer outer 
= new Outer();
        outer.test();
    }

}


/************************简单举例*************************/
/*内部类允许你把一些逻辑相关的类组织在一起,比如下例*/
class Parcel{
    
class Contents{
        
private int value;
        Contents(
int value){
            
this.value = value;
        }

        
public int readValue(){
            
return value;
        }

    }

    
class Destination{
        
private String label;
        Destination(String toWhere)
{
            
this.label = toWhere;
        }

        
public String readLabel(){
            
return label;
        }

    }

    
public void ship(int value,String destination){
        Contents con 
= new Contents(value);
        Destination des 
= new Destination(destination);
        System.out.println(con.readValue());
        System.out.println(des.readLabel());
    }

    
public static void main(String[] args) {
        Parcel parcel 
= new Parcel();
        parcel.ship(
15,"ShangHai");
    }

}


/************************class Parcel的改进*************************/
class Parcel_opt{
    
class Contents{
        
private int value;
        Contents(
int value){
            
this.value = value;
        }

        
public int readValue(){
            
return value;
        }

    }

    
class Destination{
        
private String label;
        Destination(String toWhere)
{
            label 
= toWhere;
        }

        
public String readLabel(){
            
return label;
        }

    }

    
public Contents getContents(int value){
        
return new Contents(value);
    }

    
public Destination getDestination(String label){
        
return new Destination(label);
    }

    
/*在外部类设置一个方法,该方法返回一个指向内部类的引用.这种模式很常见,有利于代码维护.*/
    
public void ship(int value,String label){
        Contents con 
= getContents(value);//类的内部调用非静态方法不需要object.method(),其他类中需要object.method()
        Destination des = getDestination(label);
        System.out.println(con.readValue());
        System.out.println(des.readLabel());
    }

    
public static void main(String[] args) {
        Parcel_opt parcel_1 
= new Parcel_opt();
        parcel_1.ship(
25,"Beijing");
        Parcel_opt parcel_2 
= new Parcel_opt();
        parcel_2.ship(
30,"TaiYuan");
        Parcel_opt.Contents contents 
= parcel_2.getContents(40);
        Parcel_opt.Destination destination 
= parcel_2.getDestination("DaLian");
        
/*如果想从外部类的非静态方法之外(静态方法内or方法体外)的任意位置创建内部类的对象,那么必须具体指明这个对象的类型:OuterClassName.InnerClassName.*/
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值