12
返回列表 发新帖
楼主: lqlayy

[讨论] 子类肯定可以访问父类的public和protected的变量?

[复制链接]
论坛徽章:
131
乌索普
日期:2017-09-26 13:06:30马上加薪
日期:2014-11-22 01:34:242014年世界杯参赛球队: 尼日利亚
日期:2014-06-17 15:23:23马上有对象
日期:2014-05-11 19:35:172014年新春福章
日期:2014-04-04 16:16:58马上有对象
日期:2014-03-08 16:50:54马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14
11#
发表于 2010-9-24 00:01 | 只看该作者
原帖由 网络咖啡 于 2010-9-22 23:51 发表
public和protected仅限在子类内部直接访问

关键lz没理解啥叫在之类内部访问

使用道具 举报

回复
论坛徽章:
0
12#
发表于 2010-10-11 08:10 | 只看该作者
classa不是子类,是父类,使用classc c= new classc()应该可以的

使用道具 举报

回复
论坛徽章:
0
13#
发表于 2010-10-19 21:38 | 只看该作者
楼上正解~呵呵~
package Pakage1;

public class Classa {
        public int varPub;
        int varDefault;
        protected int varProtect;
        private int varPrivate;
       

}

package Pakage2;
import Pakage1.Classa;
class ClassC extends ClassX{
       
}
public class ClassX extends Classa{

        /**
         * @param args
         */
        public static void main(String[] args) {
                // TODO Auto-generated method stub
                Classa a = new Classa();
                ClassX b= new ClassX();
                ClassC c = new ClassC();
                a.varPub=1;
                b.varProtect=2;
                b.varPub=3;
                c.varProtect=4;
                c.varPub=4;
                System.out.println(a.varPub);
                System.out.println(b.varProtect);
                System.out.println(b.varPub);
                System.out.println(c.varProtect);
                System.out.println(c.varPub);
        }

}

这样是可以访问的。。因为已经继承过来了。。不可以直接访问。。a.varProtect...试下~

使用道具 举报

回复
论坛徽章:
7
月度精华徽章
日期:2008-08-01 02:15:55奥运会纪念徽章:现代五项
日期:2008-08-08 14:56:30奥运会纪念徽章:跳水
日期:2008-08-15 09:58:482009新春纪念徽章
日期:2009-01-04 14:52:28生肖徽章2007版:蛇
日期:2009-01-17 14:39:46生肖徽章2007版:猪
日期:2009-01-31 16:18:19生肖徽章2007版:猴
日期:2009-08-30 22:57:30
14#
发表于 2010-10-21 10:24 | 只看该作者
所谓在类中访问,是指有this指针的作用域内访问,static方法是没有this指针的,所以不可能访问。如果要通过对象访问,那就是在外部访问了

使用道具 举报

回复
论坛徽章:
131
乌索普
日期:2017-09-26 13:06:30马上加薪
日期:2014-11-22 01:34:242014年世界杯参赛球队: 尼日利亚
日期:2014-06-17 15:23:23马上有对象
日期:2014-05-11 19:35:172014年新春福章
日期:2014-04-04 16:16:58马上有对象
日期:2014-03-08 16:50:54马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14
15#
发表于 2010-10-23 00:02 | 只看该作者
原帖由 X二 于 2010-10-21 10:24 发表
所谓在类中访问,是指有this指针的作用域内访问,static方法是没有this指针的,所以不可能访问。如果要通过对象访问,那就是在外部访问了

使用道具 举报

回复
论坛徽章:
0
16#
发表于 2010-11-5 02:36 | 只看该作者
protected variables and methods can only accessed as Hieritage other than reference
if you create an object of parent class, you have a reference access to parent class object, then you can not access the protected .
if you override or overload methods or hide variables or call parent's in the child , it will take heritage from the parent. then you can access the protected.

使用道具 举报

回复
论坛徽章:
131
乌索普
日期:2017-09-26 13:06:30马上加薪
日期:2014-11-22 01:34:242014年世界杯参赛球队: 尼日利亚
日期:2014-06-17 15:23:23马上有对象
日期:2014-05-11 19:35:172014年新春福章
日期:2014-04-04 16:16:58马上有对象
日期:2014-03-08 16:50:54马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14
17#
发表于 2010-11-5 23:13 | 只看该作者
原帖由 ifuleyou 于 2010-11-5 02:36 发表
protected variables and methods can only accessed as Hieritage other than reference
if you create an object of parent class, you have a reference access to parent class object, then you can not access the protected .
if you override or overload methods or hide variables or call parent's in the child , it will take heritage from the parent. then you can access the protected.

使用道具 举报

回复
论坛徽章:
56
2010年世界杯参赛球队:瑞士
日期:2010-02-26 11:04:012012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:23版主2段
日期:2012-05-31 02:10:00版主2段
日期:2012-05-31 02:10:00ITPUB 11周年纪念徽章
日期:2012-10-09 18:05:07现任管理团队成员
日期:2012-10-18 18:22:36
18#
发表于 2012-6-23 08:37 | 只看该作者
已经超出作用域了

使用道具 举报

回复
论坛徽章:
0
19#
发表于 2013-9-4 17:16 | 只看该作者
学习了

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表