A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 王德升 中级黑马   /  2012-6-27 21:41  /  1684 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 王德升 于 2012-6-30 10:20 编辑

第一段。
abstract class Student
{
        public abstract void study();

        public void sleep()
        {
                System.out.println("躺着睡觉");
        }
}

class DoStudent
{
        public void funcation(Student stu)
        {
                stu.study();

                stu.sleep();
        }
}

class BaseStudent extends Student
{
        public void study()
        {
                System.out.println("base study");
        }

        public void sleep()
        {
                System.out.println("坐着睡觉");
        }
}

class AdvanceStudent extends Student
{
        public void study()
        {
                System.out.println("advance stuay");
        }
}

class DuoTaiDemo
{
        public static void main(String[] args)
        {        
                /*
                new BaseStudent().study();// 1,
                new BaseStudent().sleep();//这里这样调用的话和1,是不是一个new BaseStudent()???
                */

                new DoStudent().funcation(new BaseStudent());

                //DoStudent ds = new DoStudent();

                //ds.funcation(new BaseStudent());
        }
}




第二段。
class InnerTest
{
        public static void main(String[] args)
        {
                Test.function().method();
        }
}


interface Inter
{
        void method();
}

class Test
{
        //补足代码。通过匿名内部类。
        /*
        static class Inner implements Inter
        {
                public void method()
                {
                        System.out.println("method run");
                }
        }
        */

        static Inter function()
        {
                return new Inter()//为什么这里返回的是new Inter?上面的Inner不是静态的吗?那怎么还可以new呢?好,我明白了,但是
                                //为什么不可以返回Inner呢?静态不是可以用类名直接调用吗?内部类局部不可以定义静态?
                {
                        public void method()
                        {
                                System.out.println("method run");
                        }
                };
        }

}

第三段。

class Fu
{
        boolean show(char a)
        {
                System.out.println(a);
                return true;
        }
}
class Demo extends Fu
{
        public static void main(String[] args)
        {
                int i=0;
                Fu f=new Demo();
                Demo d=new Demo();
                for(f.show('A'); f.show('B')&&(i<2);f.show('C'))
                {
                        i++;
                        d.show('D');
                }        
        }
        boolean show(char a)
        {
                System.out.println(a);
                return false;//这里return fasle对该程序的运行没有什么阻碍吗?  在打印一个A之后独到return false没有影响? 然后在独到f.show('B')
                                打印个B之后,是假所以程序结束了。我的问题是为什么在打印A之后程序读到false是怎么一回事呢?希望具体点。
        }
}


第四段。

interface A{}
class B implements A
{
        public String test()
        {
                return "yes";
        }
}
class Demo
{
        static A get()
        {
                return new B();
        }
        public static void main(String[] args)
        {
                A a=get();//这是怎么调用的,求详细解释。直接可以指向???
                System.out.println(a.test());
        }
}



第五段。
class Demo
{
         int show(int a,int b){return 0;}
}
下面那些函数可以存在于Demo的子类中。        


private int show(int a,long b){return 0;}//可以,和父类不是一个函数。没有覆盖,相当于重载。

这个可以?? private修饰,权限够??如果这个可以下面的又算什么呢。

B.private int show(int a,int b){return 0;}//不可以,权限不够。  

2 个回复

倒序浏览
本帖最后由 周兴中 于 2012-6-28 03:14 编辑

1. new BaseStudent().study();// new 了一个BaseStudent()对象,在方法调用完后立即将其销毁.
                new BaseStudent().sleep();//又new 了一个BaseStudent(),匿名对象在调用方法后,new的对象立即销毁,所以当对方法仅进行一次调用的时候,可以使用匿名对象.

2.static Inter function() //看返回值类型
        {
                return new Inter()//此处没有分号,要连着后面的大括号一起看,它实现了Inter的method方法,他返回的是一个Inter的匿名内部类的实例.
                                
                {
                        public void method()
                        {
                                System.out.println("method run");
                        }
                };
        }

return后的语句:   new Inter(){
                        public void method()
                        {
                                System.out.println("method run");
                        }
              }; //这就是匿名内部类


3. for(f.show('A'); f.show('B')&&(i<2);f.show('C')) //
                {
                        i++;
                        d.show('D');
                }   
  boolean show(char a)
        {
                System.out.println(a);
                return false;//这个示例是为了演示继承的关系,当子类覆盖父类的方法后,通过父类的引用也将执行子类的方法.
        }
   
4.不允许创建接口的实例(实例化),但允许定义接口类型的引用变量,该引用变量引用实现了这个接口的类的实例
A a=get();//由于B实现了A,并且B的方法get()返回的是的B的实例  等同于与 A a = new B(); //引用变量a被定义为A接口类型,引用了B实例,成立

5. 重载只关心参数列表,不关心权限.
   重写则不能降低原方法的"可见度"。例如:被重写方法为protected void do(int i,double d),则重写方法可以为protected void do(int i,double d),或者public void do(int i,double d),但是不可以是private void do(int i,double d)。



评分

参与人数 1技术分 +2 收起 理由
刘蕴学 + 2

查看全部评分

回复 使用道具 举报
周兴中 发表于 2012-6-28 02:55
1. new BaseStudent().study();// new 了一个BaseStudent()对象,在方法调用完后立即将其销毁.
             ...

非常感谢!!!!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马