| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 752 人关注过本帖
标题:package 2; static ***************谢!比如创建对象,最好能稍做讲解。
只看楼主 加入收藏
yinmingzheng13
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2008-4-13
结帖率:100%
收藏
 问题点数:0 回复次数:0 
package 2; static ***************谢!比如创建对象,最好能稍做讲解。
package p1;


public class Original
{
    int n_friendly = 1;
    private int n_private = 2;
    protected int n_protected = 3;
    public int n_public = 4;
    
    void Access()
    {
        System.out.println("**** In same class,you can access...");
        System.out.println("friendly member" +n_friendly);
        System.out.println("private member" +n_private);
        System.out.println("protected member" +n_protected);
        System.out.println("public member" +n_public);
    }
    
    
    class Derived extends Original
    {
        void Access()
        {
            System.out.println("**** 相同包的子类 ****");
            System.out.println("friendly member" +n_friendly);
            //System.out.println("private member" +n_private); 不能访问
            System.out.println("protected member" +n_protected);
            System.out.println("public member" +n_public);
            
            Original o = new Original();
            System.out.println("**** 相同包的子类的其他对象****");
            System.out.println("friendly member" +o.n_friendly);
            //System.out.println("private member" +o.n_private); 不能访问
            System.out.println("protected member" +o.n_protected);
            System.out.println("public member" +o.n_public);
        }
    }
   
   
    class SamePackageClass
    {
        void Access()
        {
            Original o = new Original();                    //?
            System.out.println("**** 相同包的其他类 ****");
            System.out.println("friendly member" +o.n_friendly);
            //System.out.println("private member" +o.n_private); 不能访问
            System.out.println("protected member" +o.n_protected);
            System.out.println("public member" +o.n_public);
        }
    }
   
    class AccessControl
    {
        public static void main(String[] args)
        {
            Original o = new Original();
            o.Access();
            Derived d = new Derived();
            d.Access();
            SamePackageClass s = new SamePackageClass();
            s.Access();
        }
    }
   
   
   
 
package p2;

    class Derived extends p1.Original
    {
        void Access()
        {
            System.out.println("**** 不同包中的子类 ****");
            //System.out.println("friendly member" +n_friendly);
            //不能访问
            //System.out.println("private member" +n_private);
            System.out.println("protected member" +n_protected);
            //子类可以访问父类
            System.out.println("public member" +n_public);
        
        p1.Original O = new Original();
        System.out.println("**** 访问在不同包中的父类 ****");
        //System.out.println("friendly member" +o.n_friendly);
        //System.out.println("private member" +o.n_private); 不能访问
        //System.out.println("protected member" +o.n_protected);
        System.out.println("public member" +o.n_public);
        }
    }
   
   
    class AnotherPackageClass
    {
        void Access()
        {
            p1.Original o = new p1.Original();
            System.out.println("**** 另一包中的其他类 ****");
            //System.out.println("friendly member" +o.n_friendly);
            //System.out.println("private member" +o.n_private); 不能访问
            //System.out.println("protected member" +o.n_protected);
            System.out.println("public member" +o.n_public);
        }
    }
   
   
    public class AccessControl
    {
         public static void main (String [] args)
            {
                Derived d = new Derived();
                d.Access();
                AnotherPackageClass a =new AnotherPackageClass();
                a.Access();
        }
    }
}
搜索更多相关主题的帖子: package static 对象 讲解 
2008-04-14 19:56
快速回复:package 2; static ***************谢!比如创建对象,最好能稍做讲解 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.050269 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved